Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tool strip buttons 1

Status
Not open for further replies.

brews

Technical User
Dec 12, 2007
194
US
Have several buttons on a tool strip and also an errant Select Case.
Here is the select case
Code:
        Select Case tbrText.Text
            Case "New"
                Call mnuFileNew_Click()
            Case "Open"
                 Call mnuFileOpen_Click()
            Case "Print"
                Call mnuFilePrint_Click()
            Case "Save"
                Call mnuFileSaveAs_Click()
        End Select
The problem is that tbrText.text is not a replacement for Button.Key as in VB6. Have also tried toolstripaccessibleobject.

Any ideas would be great. thanks
 
Need a little more information about what you are trying to do.

In design view, if you double-click on one of the controls, you will get the default action wired up for you. in the case of ToolStripButtons, that should be the click event. You only have to insert your code and everything should work fine.

If you want to wire the events manually (and there are several reasons you may want to do that), you can use something like this in the Load event for your form:

Code:
mnuFileNew.Click += mnuFileNew_Click

If you want to wire all the controls up to a generic method, then you will need to pass in the calling control and figure out what to do with it when it gets there. (Typed, NOT TESTED)

Code:
Private Sub MainScreen_Load(sender As Object, e As EventArgs)
	mniFileNew.Click += MenuHandler_Click
	mniFileOpen.Click += MenuHandler_Click
End Sub

Protected Sub MenuHandler(sender As Object, e As EventArgs)
	Dim caller As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)
	Select Case caller.Text
		Case "New"
		Case "Open"
		Case Else
	End Select
End Sub

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
Thanks for the response. I am aware of the individual control click properties. Was hoping to replicate the vb6 select case for then all the clicks would be in one spot.

The form has not only a toolstrip but also a menu strip. All of the items on the menu strip use the individual click property. Using the select case seems to bring the code in one place as shown above.

Please speak to mnuFileNew.Click += mnuFileNew_Click. Have not seen that before. You also mention there are reasons to do them all individually. Please speak to that also.

And in your third example when you say figure what to do, would that be nothing more than
Code:
 Select Case caller.Text
        Case "New"
           call mnuFileNew_Click()
 end select

Thank you.
 
mnuFileNew.Click += mnuFileNew_Click

This is a way to assign a method to an event. Basically, this is the same as if you double-clicked on the control in the designer and added your code there. But what it really allows you to do is assign a method to a control's event anytime you want. You can also use this to change a control's event if needed in code. You can use to remove an Event assignment:

mnuFileNew.Click -= mnuFileNew_Click

If you used that, the control would no longer have a Click event. You could then reassign another method to the control as needed.

Why would you want to do this manually versus using the default event assignment? I basically just explained that...If you want to assign the same method to several controls, this is how you would do it. Also, you might need to assign a control's event after some other event happens. As an example, maybe I want to have a combobox's SelectedIndexChanged event do something special. But if I assign the method using the default method, at Form Load, the event would fire because the index of the combobox would be set to 0 immediately. Instead, after I load the combobox with my values, I can then use the above method to assign the SelectedIndexChanged event.

Last, the third example...That should work. I haven't done much VB in a while (been doing a lot of C# lately), but that is still probably not the best method. I think the correct syntax for calling a method from another in VB.Net is this:

Code:
mnuFileNew_Click(sender, New System.EventArgs()) 'If you need to pass the arguements

or

mnuFileNew_Click(Nothing, Nothing) 'If you don't care about the arguements

Hopefully that helps explain a bit more...Please let me know if you need more explanation about any of these items.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
thanks for the info. Seems that rather than reinventing the wheel, I did the double-click thingy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top