Usually, VB. NET programmers create a static design that provides everything you need for the application of a program. However, in some cases, programmers may not be able to anticipate each requirement in advance, and there is a need for dynamic code generation.
The discussion in this article will also focus on both. The first is when programmers need to dynamically build a control and attach code to the control. For example, you might want to create a list of links, but you don't know the number of links you need to create or what data will appear in the links. The second is when programmers need to define code to reflect special requirements. For example, you might want to perform code that reflects the user's system configuration.
Such a situation would certainly not be performed every day. In fact, they only appear under extraordinary circumstances. However, as a programmer, you still have to realize. NET provides a solution for dynamic scenarios. With the right skills, you can write applications that can handle dynamic situations flexibly.
Using dynamic controls
Many programmers always encounter the need to dynamically create controls. In the example we showed, programmers added Linklabels to FlowLayoutPanel. Maybe you can set this up to record and save a number of commonly used URLs, files, network addresses, or other resources. This example does not actually save the link, but you can use the XML serialization feature to implement the Save.
Each time the user clicks the test button, the sample code dynamically creates a new LinkLabel control. The real demo code is not complicated. The example shows everything you need to do to create such a control and put the control into Flowlayoutpanel,lstlabel.
Example one: Add a new link to the FlowLayoutPanel
Private Sub btnTest_Click()
Handles btnTest.Click
' Create a link.
Dim NewLink As LinkLabel =
New LinkLabel()
' Add some properties to it.
NewLink.Text = DateTime.Now
.ToLongTimeString()
' Set the click event handler.
AddHandler NewLink.Click,
AddressOf NewLink_Click
' Place the button on the form.
lstLinks.Controls.Add(NewLink)
End Sub
As you expected, the code started with a new LinkLabel and gave it some value. This example uses the current time. Your code may be able to access a real resource.
Note that the code also specifies a handler for the linked click event. You must use the AddHandler technique in the example, because the normal handles keyword path does not work. On the one hand, you don't know the name of the control when designing the application. Even if you specify a name for the control, you do not know how many controls the user will create, so we have no way of knowing how much of the handler will be created. The code for the handler is similar to the control code, so it is not necessary to create more than one handler. The processing code for this example is shown in example two. Example two: Handling Dynamic control Click events
Private Sub NewLink_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs)
' Verify that you actually have a LinkLabel
If Not sender.GetType() Is GetType(LinkLabel) Then
MessageBox.Show("Wrong control type provided!")
Return
End If
' Convert the input sender to a Button.
Dim ThisLink As LinkLabel = sender
' Show that we have the correct button.
MessageBox.Show("You created this link at: " + ThisLink.Text)
End Sub