How can I Add ToolStripButtons on a Child Form Dynamically with events

G

Garry

I have written vb.net code to add toolstripbuttons to a predefined toolstrip
on my MDI child form.

I can assign them a .bmp file from a global ImageList etc. I used the
'behind code' to get a code sample to do this BUT, I cannot see a way to add
a single event handler to deal with the various buttons on this toolstrip. I
mean using a select statement.

Anyone have any ideas?????

Garry

Example of running code

Dim Button As ToolStripButton = New ToolStripButton

toolstrip.ImageList = gimglst

toolstrip.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Button})
Button.Image = gimglst.Images("SaveAndClose")
Button.Name = "SaveAndClose"
Button.ToolTipText = "Save and Close"
ConferPropertiesToButtons(Button)

Button = New ToolStripButton
toolstrip.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Button})
Button.Image = gimglst.Images("CloseNoSave")
Button.Name = "CloseNoSave"
ConferPropertiesToButtons(Button)

Private Sub ConferPropertiesToButtons(ByVal button As ToolStripButton)

button.DisplayStyle =
System.Windows.Forms.ToolStripItemDisplayStyle.Image
button.ImageTransparentColor = System.Drawing.Color.White
button.Size = New System.Drawing.Size(23, 22)
button.Text = ""

End Sub
 
R

RobinS

Can you add event handlers after defining the buttons?
I don't know what you mean by "I mean using a select statement".
Does this work?

AddHandler SaveAndClose.Click, AddressOf DoSaveAndClose
AddHandler CloseNoSave.Click, AddressOf CloseNoSave

Public Sub DoSaveAndClose(ByVal sender as Object, ByVal e as
System.EventArgs)
'do whatever
End Sub
Public Sub CloseNoSave(ByVal sender as Object, ByVal e as System.EventArgs)
'do whatever
End Sub

Would that work?

Robin S.
 
G

Garry

Thanks for the reply Robin and the effort invested in it.

I found sample code somewhere to instantiate a toolStripContainer on a
virgin form and etc etc.

The AddButton stuff looked like the following

toolStripContainer1 = New System.Windows.Forms.ToolStripContainer()
toolStripContainer1.Dock = DockStyle.Top
toolStrip1 = New System.Windows.Forms.ToolStrip()
toolStrip1.Dock = DockStyle.Top
' Add items to the ToolStrip.
toolStrip1.Items.Add("SaveAndClose", gimglst.Images("SaveAndClose"), New
System.EventHandler(AddressOf Me.toolbarClick))

' Add the ToolStrip to the top panel of the ToolStripContainer.
toolStripContainer1.TopToolStripPanel.Controls.Add(toolStrip1)
toolStripContainer1.Height = toolStrip1.Height 'Fit the container exactly

THEN I went thru each button doing..........

intPos = 0
toolStrip1.Items(intPos).Text = ""
toolStrip1.Items(intPos).ToolTipText = "Save and close"
toolStrip1.Items(intPos).Tag = "SaveAndClose"

etc etc

gimglst holds my application icon/bmp files - (global ImageList)

Then I wrote the handler toolbarClick as a simple Select Sender.Tag property
and this solved my problem nicely.

So now I have good code which will build a tool bar on a form, even from a
data source table which defines the specific toolbar which is what I
wanted.to accomplish.

Of course, the Me.toolbarClick event handler must deal with all the various
possibilites up front but the up and runnung code basis is there. All the
button's clicks are delt with in a single function. Looks nicer

If I understand it correctly, as an experienced programmer coming from VB6,
the toolStripButtons cannot handle an event themselves but the toolStrip
can. This stuff about 'raising' an event is somewhat foreign to me. I never
had need for it in VB6.

Perhaps you have some links where this can be explained.

Garry
 
R

RobinS

If you want to know about raising events, you can search MSDN for it.
Also, if you want a good book that explains all kinds of stuff like
that, complete with examples, check out Francesco Balena's book
"Visual Basic 2005: The Language". This only covers the language,
not forms or data access, and is an excellent book. It not only
explains *how* to raise events, but why you might want to. It
covers everything from data types to events and delegates to
inheritance and classes to reflection.

Robin S.
---------------------------------
 

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