473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Building controls at runtime and responding to their events

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
Nov 20 '05 #1
3 1512
Eric Sabine wrote:
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.

--
Sven Groot
Nov 20 '05 #2
* "Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> scripsit:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
Perfect. Thanks for both examples Herfreid and for your example Sven.

Eric
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:c7***********@ID-208219.news.uni-berlin.de...
* "Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> scripsit:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
1
by: hybrid | last post by:
I have problems in understanding the behavior of the events triggered by dynamically created controls over a webform. Could you help me? In a webform, I have a static PlaceHolder PH containing...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
2
by: Anand Sagar | last post by:
I have a Panel1 and button1 on my webform. At runtime, I create 2 textboxes. I do it at the Page_Load event. I put the code within the " If Not isPostBack" For the button click event, I will do...
4
by: Chuck Ritzke | last post by:
Hi, I've searched the newsgroup and other sources to understand how to handle runtime controls and see I'm not the only one who's confused, but I'm still not quite sure of the best way to handle...
1
by: Mian Mahboob | last post by:
hi At Runtime i add a Form in designing time panel Control. on that form i add couple of user controls. i want to comuncated each of usercontrol's on my Form which is execute on Runtime...
5
by: Steve Moreno | last post by:
Hi all, I've got a web form that I've written code to create an array of DropDownList controls on the page depending on how many records are pulled back. The code to create the controls is...
1
by: Klaus Jensen | last post by:
Subject: Handling events of controls created at runtime I need to render some buttons at runtime based on a database, tie events to them, and handle the events when they fire. In Page_Load I...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.