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/
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