Click to See Complete Forum and Search --> : HOW TO: dynamically add control in VB.NET?
owenrb
March 16th, 2003, 06:26 PM
Dear all,
I have this question. Is it possible for VB.NET to add or dynamically create control during run-time? And, how would I add event handler for these dynamically created controls.
Please advice me on this. Any help you may have will be very much appreciated. Thanks in advance.
Best Regards,
Owen/
ITGURU
March 16th, 2003, 10:57 PM
Dear Owen,
I think there is nothing impossible in this world and same apply when we programming in any language.
Here below is the solution for your problem:
1. Add any procedure for event handling of the control like as follows:
Protected Sub button1_Click(sender As Object, e As System.EventArgs)
' Go to the previous item in the Customer list.
messagebox.show("Button Pressed")
End Sub
2. Add following line immediately after the following lines
Public Class Form1
Inherits Form
in the form:
Private button1 As Button
2. Add following line of code in InitializeComponent method of the form (you can find InitializeComponent method when you expand #Region " Windows Form Designer generated code "
With Me
.components = New Container
.button1 = New Button
With .button1
.Location = New Point(24, 16)
.Size = New Size(64, 24)
.Text = "<"
AddHandler button1.click, AddressOf button1_Click
End With
With .Controls
.Add(button1)
End With
End With
Hope this will solve your problem.
Regards
Gurdarshan Singh
Rube74
March 17th, 2003, 04:22 PM
Yeah, ITGURU's solution is pretty much how you would want to do it. You can set up functions which will create the widget of your choice and add those to a form, panel, or other container. Then, you can dynamically run those functions at run-time (all you really have to pass is a location and a size). The best place to start would probably be by looking at the ControlCollections, and, in particular, the Add method for these ControlCollections.
Adding the event handler is a little trickier, but I would assume that you should be able to do that in your widget-creation functions as well.
owenrb
March 18th, 2003, 01:34 AM
Thanks ITGURU, it really works. That was very CLEVER.
Then, I implement this array of push button.
But I have problem determining which of those buttons is being pushed, since there were no index variable exposed inside button1_Click() sub module. Any advice?
Again, thank you very much.
Regards,
Owen/
Rube74
March 18th, 2003, 04:31 PM
You could try using the Control Collection index for the button. For example, if it's the button is the first form element on the form, you could say:
AddHandler Me.Controls(0).Click(), AddressOf button1_Click
This index starts at 0 and increases sequentially with each new element you put on the form.
ITGURU
March 20th, 2003, 01:17 AM
hello,
Please try using Text property of the Button control for finiding which control is clicked and you can find the detail by using following code:
Write following line for validating the control to whom user clicked in the Event which is assigned to the control:
if CType(sender, Button).Text) = "Button1" then
else
if CType(sender, Button).Text) = "Button3" then
endif
endif
Hope this will solve your problem.
owenrb
March 20th, 2003, 03:03 AM
That's a good suggestion, ITGURU. Thanks.
Thank you Rube74.
Have a nice day to both of you.
Best Regards,
Owen/
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.