Toolbar question

  • Thread starter Thread starter Water Cooler v2
  • Start date Start date
W

Water Cooler v2

Besides querying the Text property, how do you find out which button on
the toolbar has been clicked? The Name property doesn't appear in the
code window, but only appears in the design mode.

The problem is that I have added a lot of spaces to the left of the
label of one of my buttons. The actual label text I have entered in the
design mode is " Exit Map".

When I query this:

else if (e.Button.Text.Trim() == "Exit Map")
{
this.Dispose();
this.Close();
}

The code skips this condition altogether. For the other labels, it
enters the construct as it should. I am a bit confused.
 
I couldn't duplicate your problem, my code worked just fine, but to avoid it
you could use the TAG property instead.

HTH
WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
 
Water Cooler v2 said:
Besides querying the Text property, how do you find out which button on
the toolbar has been clicked? The Name property doesn't appear in the
code window, but only appears in the design mode.

The problem is that I have added a lot of spaces to the left of the
label of one of my buttons. The actual label text I have entered in the
design mode is " Exit Map".

When I query this:

else if (e.Button.Text.Trim() == "Exit Map")
{
this.Dispose();
this.Close();
}

The code skips this condition altogether. For the other labels, it
enters the construct as it should. I am a bit confused.

What may be easier is to use code similar to this:

else if (myToolBar.Buttons.IndexOf(e.Button) = 1); // Or whatever the index
of your 'Exit Map' button is.

That way, if you change the text on the button, you won't need to worry
about changing the code.

Cheers,

Chris.
 
Chris is right, but I would disagree. It is far more likely that I would add
a button, thereby having to go and change all my "IndexOf" statements, than
have to change the text of a button a user is already using.

If you use the Tag property you avoid both problems, since the tag won't be
seen by the user, and I can assign a unique value to each and never have to
worry about adding buttons OR changing indexes.

IMHO of course ;)

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
 
else if (e.Button.Text.Trim() == "Exit Map")
{
this.Dispose();
this.Close();
}


Unless you're adding tool bar buttons dynamically at run-time, just
give all of your buttons descriptive names and compare the object
references:

ToolBarButton _button1;
..
..
..

if( e.Button == _button1 )
{
DoStuff();
}
 
Hmmm,

Thinking about it, I have to agree that using Tag is probably the best
maintenance friendly option :-)

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top