Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VB.NET dynamic controls

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use VB.NET dynamic controls", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use VB.NET dynamic controls" this article.

The discussion of this article will also focus on these two situations. The first is when programmers need to dynamically build a control and attach code to it. For example, you may want to create a list of links, but you don't know how many links need to be created or what kind of data will appear in the links. The second is when programmers need to define code to reflect special requirements. For example, you may want to execute code that reflects the user's system configuration.

Things like the above certainly don't happen every day. In fact, they only appear in extraordinary circumstances. However, as a programmer, you should still be aware that .NET provides a solution to dynamic situations. With the right skills, you can write applications that can handle dynamic situations flexibly.

Using VB.NET dynamic controls

Many programmers always come across a time when they need to create VB.NET dynamic controls. In the example we show, the programmer adds LinkLabels to FlowLayoutPanel. Maybe you can use this setting to record and save the values of commonly used URL, files, network addresses, or other resource locations. This example doesn't really save the link, but you can use the XML serialization feature to save it.

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 following shows everything you typically need to do to create such controls and put them into FlowLayoutPanel,lstLabel.

Add a new link to 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 might expect, the code starts by creating a new LinkLabel and assigning some values to it. This example uses the current time. Your code may be able to access a real resource.

Notice that the code also assigns a handler to the linked Click event. You have to use the AddHandler technique in the example, because the normal Handles keyword path doesn't work. On the one hand, you don't know the name of the control when you design the application. Even if you specify a name for the control, you don't know how many controls the user will create, so there is no way to know how many handlers will be created. The code for handlers is similar to control code, so there is no need to create multiple handlers. The processing code used for this example is shown in example 2. Example 2: handle VB.NET dynamic control click event

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

You may have noticed that the event handler in example 1 uses a loose representation-- it doesn't take the ByVal sender as the System.Object, nor does it take ByVal e as the System.EventArgs parameter because it doesn't need both. However, when you create an event handler to create a VB.NET dynamic control, you usually need to take the ByVal sender as the System.Object parameter, which means that both are included.

Some programmers have an error when creating an event handler that does not check the type of the incoming control. The sender object may contain multiple choices, and if the event handler is not set for the event handling type, then you will face more choices. Our sample code starts by checking the type of the incoming control object. In this way, the transmitter will not be like the code shown below:

Private Sub btnTest2_Click () Handles btnTest2.Click 'Create a link. Dim NewButton As Button = NewButton () 'Add some properties to it. NewButton.Text = DateTime.Now.ToLongTimeString () 'Set the click event handler. AddHandler NewButton.Click, AddressOf NewLink_Click 'Place the button on the form. LstLinks.Controls.Add (NewButton) End Sub

This code creates a button in FlowLayoutPanel, which works in most cases unless the event handler does not act as shown by the button. If you are going to serve multiple control types, each control type requires a unique handling. You can use multiple event handlers or provide selection criteria for certain types.

The NewLink_Click () event handler converts the incoming sender to the specified type as usual, in this case LinkLabel. This code can access the LinkLabel property and interact in other ways. In our example, only one dialog box is shown that tells us when the link is created.

The above is all the contents of the article "how to use VB.NET dynamic controls". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report