Fuc@#!! ToolBars

R

Romain TAILLANDIER

Hi group,

I manage with toolBars.
This class seems to me as a prehsitorical (if that word exists) class to
me......
I used the class ToolBar, added ToolBarButton in the ToolBar.Buttons, put
images on them etc....

To detect a ToolBarBbutton.Click event, it seems that the only way is to
detect the ToolBar.Click event then to determine the ID of the Button
clicked, and select do what to do in a great switch case.
That mean that if i insert a new button to my ToolBar during a maintenance
developpement, or for a new version, i will have to rewrite my entiere
switch case !

Is there a realist method to get my ToolBarButton.click events ??
In the ToolBarButtonCollection, I can fix the name of myToolBarsButtons, so
why can't i access them with it ?
Must i develop a new ToolBarButton class to do that ?
Or is there a real third part ToolBar class wich improve some realist
functionalities ?
Or do i miss something ?

thanks
ROM
 
S

Scott M.

I haven't used Toolbars in .NET, but in VB 6.0 you could assign each
particular button a "Key" value (for example the "New" button would have a
key of "New", etc.). This way, if/when you modify your toolbar and add a
button, all you have to do in your case statement is add 1 more Case
statement that checks for that new buttons "Key".

Select Case Button.Key
Case "New"
code
Case "Open"
code
Case "Print"
code
End Case

-Scott
 
A

Alan Pretre

Scott M. said:
I haven't used Toolbars in .NET, but in VB 6.0 you could assign each
particular button a "Key" value (for example the "New" button would have a
key of "New", etc.). This way, if/when you modify your toolbar and add a
button, all you have to do in your case statement is add 1 more Case
statement that checks for that new buttons "Key".

It's the same in .NET. But the OP is right. The toolbars shipped with VS
are a joke and prehistoric is a good word for them. If you want the latest
bells and whistles for toolbars there are vendors that sell good solutions.
Dockable, source code, etc.

-- Alan
 
J

Joerg Jooss

Alan Pretre said:
It's the same in .NET. But the OP is right. The toolbars shipped
with VS are a joke and prehistoric is a good word for them. If you
want the latest bells and whistles for toolbars there are vendors
that sell good solutions. Dockable, source code, etc.

A better approach than switching is storing a MenuItem or Button
reference that invokes the same UI action in the ToolBarButton's Tag
property. This reduces the tool bar click handler to:

private void OnToolBarButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e) {
MenuItem menuItem = e.Button.Tag as MenuItem;
if (menuItem != null) {
menuItem.PerformClick();
return;
}
Button button = e.Button.Tag as MenuItem;
if (button != null) {
button.PerformClick()
}
}

Cheers,
 
C

Christopher Kimbell

Hi Rom,

I agree that it is pretty bad.
One way you could get around it is to attach a new event handler to the
ToolBar for each button,
creating multiple subscribers for the event.
Each event handler checks if the repspective button is pressed.

toolBar1.ButtonClick += new
ToolBarButtonClickEventHandler(ToolBarButton1_Clicked);
toolBar1.ButtonClick += new
ToolBarButtonClickEventHandler(ToolBarButton2_Clicked);

private void ToolBarButton1_Clicked(object sender,
ToolBarButtonClickEventArgs e)
{
if(e.Button == ToolBarButton1)
{
// do something here
}
}

private void ToolBarButton2_Clicked(object sender,
ToolBarButtonClickEventArgs e)
{
if(e.Button == ToolBarButton2)
{
// do something here
}
}

Hope this helps.

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

Top