473,434 Members | 1,820 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,434 software developers and data experts.

Create a function to handle click events of controls created during runtime in VB.NET

hi,

I am new to this forum but i have received a lot of help reading through this forum. I am creating a VB.NET application for Patient Record Management. In this there are various combo boxes and buttons being generated during run time when a particular patient ID is searched for. Say one patient might have 2 entries some may have 10 entries. So the controls are generated dynamically and then the control names are added to the forms collection(Me.Controls.Add(txtDataShow))

I am looking for is generating functions during runtime which can handle say a clicking of the button which was generated during runtime. some thing like

Private Sub btnAddDiag_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles (dynamicbutton).Click

where dynamicbutton will be the name of the button the user clicks.

Appreciate your help.
Mar 26 '08 #1
2 6103
balabaster
797 Expert 512MB
It depends on the object you're handling...but here's the jist of it...

Expand|Select|Wrap|Line Numbers
  1. Private Sub MyEventHandler(ByVal Sender As Object, ByVal e As System.EventArgs) 'The System.EventArgs may change depending upon the event you handle and what type of object...
  2.   'Do stuff
  3. End Sub
  4.  
  5. 'In your sub where you add your object
  6. Dim oObj As New Object 'Replace Object with the type of object you require.
  7. AddHandler oObj.Event, AddressOf MyEventHandler
  8.  
Instead of attaching the Handles clause on the end of your event handler sub, you effectively attach the event handler dynamically at the time the object is instantiated. A for instance - if you're adding a button to your page and wish to handle the click event of that button:
Expand|Select|Wrap|Line Numbers
  1. Private Sub MyClickHandler(ByVal Sender As Object, ByVal e As System.EventArgs) 'Notice there's no handles clause here
  2.   MsgBox "You clicked "& CType(Sender, Button).ID & "."
  3. End Sub
  4.  
  5. Sub Page_Load(ByVal Sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  6.   For n As Integer = 1 To 10
  7.     Dim oBtn As New Button
  8.     oBtn.ID = "Button"& n
  9.     oBtn.Text = "Click me!"
  10.     AddHandler oBtn.Click, Addressof MyClickHandler
  11.     Form1.Controls.Add(oBtn)
  12.   Next
  13. End Sub
Mar 27 '08 #2
It depends on the object you're handling...but here's the jist of it...

Expand|Select|Wrap|Line Numbers
  1. Private Sub MyEventHandler(ByVal Sender As Object, ByVal e As System.EventArgs) 'The System.EventArgs may change depending upon the event you handle and what type of object...
  2.   'Do stuff
  3. End Sub
  4.  
  5. 'In your sub where you add your object
  6. Dim oObj As New Object 'Replace Object with the type of object you require.
  7. AddHandler oObj.Event, AddressOf MyEventHandler
  8.  
Instead of attaching the Handles clause on the end of your event handler sub, you effectively attach the event handler dynamically at the time the object is instantiated. A for instance - if you're adding a button to your page and wish to handle the click event of that button:
Expand|Select|Wrap|Line Numbers
  1. Private Sub MyClickHandler(ByVal Sender As Object, ByVal e As System.EventArgs) 'Notice there's no handles clause here
  2.   MsgBox "You clicked "& CType(Sender, Button).ID & "."
  3. End Sub
  4.  
  5. Sub Page_Load(ByVal Sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  6.   For n As Integer = 1 To 10
  7.     Dim oBtn As New Button
  8.     oBtn.ID = "Button"& n
  9.     oBtn.Text = "Click me!"
  10.     AddHandler oBtn.Click, Addressof MyClickHandler
  11.     Form1.Controls.Add(oBtn)
  12.   Next
  13. End Sub
The code AddHandler oBtn.Click, Addressof MyClickHandler works pefectly and is exactly what i needded. I used it where i was creating the buttons dynamically and it does add the click function during runtime. Thanks a lot. You saved me a lot of trouble.
Mar 27 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
3
by: leon | last post by:
hello friends, i am writing a page aspx and creating controls dinamicaly and then i must to create for each control the events as well. Anybody to know how????? happy day lion
3
by: RSB | last post by:
Hi Every one Having tuff time creating web controls Dynamically. All i am trying to do is read a table and generate a list of ASP TEXT Box. So how do i create this Control dynamically and where...
18
by: eliss.carmine | last post by:
Is it possible to simulate a mouse click in the window I made (it's a Form), but not give it focus? I tried using WinAPI's mouseevent and SendMessage of WM_LBUTTONDOWN/WM_LBUTTONUP as suggested...
8
by: William Foster | last post by:
Good evening all, I am creating a program with the ability to select an option from a ComboBox which will then create a secondary list of options in a CheckedListBox. Previously when I have...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
7
by: Matt | last post by:
Hi all, I'm trying to create a system where it reads a number of records from a database and then creates a row in the GUI that contains a single field from the database and a button that has a...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.