|
-
March 16th, 2003, 07:26 PM
#1
HOW TO: dynamically add control in VB.NET?
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/
"Hhmn... You are damned if you do; and you are damned if you don't." -- bart simpson
-
March 16th, 2003, 11:57 PM
#2
Adding control dynamically in VB.Net
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
Enjoy,
Gurdarshan Singh
L.S.E. (Project Lead)
InterGlobe Technologies Pvt. Ltd.
Mobile #: 9891397798 (India)
[email protected]
[email protected]
Always Think Positive whatever may be the Situation.
Please rate my suggestion/response if you find it suitable or fulfill your requirement.
-
March 17th, 2003, 05:22 PM
#3
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.
-
March 18th, 2003, 02:34 AM
#4
TWO THUMBS UP!
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/
"Hhmn... You are damned if you do; and you are damned if you don't." -- bart simpson
-
March 18th, 2003, 05:31 PM
#5
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.
-
March 20th, 2003, 02:17 AM
#6
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.
Enjoy,
Gurdarshan Singh
L.S.E. (Project Lead)
InterGlobe Technologies Pvt. Ltd.
Mobile #: 9891397798 (India)
[email protected]
[email protected]
Always Think Positive whatever may be the Situation.
Please rate my suggestion/response if you find it suitable or fulfill your requirement.
-
March 20th, 2003, 04:03 AM
#7
That's a good suggestion, ITGURU. Thanks.
Thank you Rube74.
Have a nice day to both of you.
Best Regards,
Owen/
"Hhmn... You are damned if you do; and you are damned if you don't." -- bart simpson
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|