Building controls at runtime and responding to their events


E

Eric Sabine

This app will fill a datatable from sql server at load time (and can clear
and reload it again if the user requests it). The contents of the data table
contain program names and program groups. For example
Accounting
ACT123
ACT456
ACT789
Production
PRO123
PRO234
Shipping
SHP432
SHP678
etc.

The data from this datatable will be iterated through and for each entry,
i.e., ACT789 under the Accounting menu, a new menu item will be added to the
context menu.

When the user opens the context menu during run time, in this case they
would see the 3 groups and could then move right on each one and see the
programs under each group. If the user picks say ACT123 then that accounting
program would then open. Simple stuff.

But the data table is user dependent so the list will be different for every
user. Adding menu items to the data table from code seems easy enough, but I
think what will be the hard part is reacting to each click event for the
different menu items. Regardless of which program is clicked, they call call
the same method in the end, i.e., Private Sub RunProgram (ByVal ProgramExe
as String) ....

So how can I react to the click events at run time when I build the menu
list of the context menu dynamically? Basically it seems that somehow
during runtime, I will need to react to each of clicks , i.e., Sub X ()
handles ContextMenuItem.Click, but since the controls are added at runtime,
how to I process each's click event?

Eric
 
Ad

Advertisements

S

Sven Groot

Eric said:
So how can I react to the click events at run time when I build the
menu list of the context menu dynamically? Basically it seems that
somehow during runtime, I will need to react to each of clicks ,
i.e., Sub X () handles ContextMenuItem.Click, but since the controls
are added at runtime, how to I process each's click event?
You can handle events without using Handles. Take a look at the AddHandler
statement in the docs.
 
H

Herfried K. Wagner [MVP]

* "Eric Sabine said:
So how can I react to the click events at run time when I build the menu
list of the context menu dynamically? Basically it seems that somehow
during runtime, I will need to react to each of clicks , i.e., Sub X ()
handles ContextMenuItem.Click, but since the controls are added at runtime,
how to I process each's click event?
Here is a simple example on how to do that with a button control:

\\\
Dim btn As New Button()
AddHandler btn.Click, AddressOf Me.btn_Click
Me.Controls.Add(btn)
///

Add the handler code:

\\\
Private Sub btn_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
)
MsgBox("Clicked!")
End Sub
///

The 'MenuItem' class provides a constructor that will take the event
handler -- the following sample has been taken from the MSDN
documentation on the 'MenuItem' constructor:

\\\
Public Sub CreateMyMenuItem()
' Create an instance of MenuItem with caption and an event
' handler
Dim MenuItem1 As New MenuItem("&New", New _
System.EventHandler(AddressOf Me.MenuItem1_Click))
End Sub
' This method is an event handler for MenuItem1 to use when
' connecting its event handler.
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal _
e as System.EventArgs)
' Code goes here that handles the Click event.
End Sub
///
 
Ad

Advertisements

E

Eric Sabine

Perfect. Thanks for both examples Herfreid and for your example Sven.

Eric
 

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