473,698 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic creation of controls in vb.net code

Hi,

I have to create a variable amount of controls with events for a windows
form in my vb code.

Private WithEvents T_Label As Label
Private WithEvents T_ButtonDo As Button

I create the controls in a loop in load event of the form:

For i = 0 to NumberOfControl s

T_Label = New Label
T_Label.Name = "LabelRecor d_" & Format(i, "000")
T_Label.Tag = i
AddHandler T_Label.DoubleC lick, AddressOf DoSomething
AddHandler T_Label.Click, AddressOf DoSomething
.....
' add button
T_ButtonDo = New Button
T_ButtonDo.Name = "ButtonDo_" & Format(i, "000")

....

Next

Later in code in a another event I want to access the properties of the
previously created controls again:

For i = 0 to NumberOfControl s

' for exambple
T_ButtonDo.Text = "Some value"

Next

The problem is that the code above of course always adresses the last
created control of the Loop in the Load event! How can I adress all controls
created in the loop with their properties and methods ?

Building an array does not solve the problem as I cannot use events together
with an array of controls! And I need the events.

Thanks a lot for help in advance!!!
--
Best regards

Henry
Nov 21 '05 #1
2 11492
Hi,

You are creating one control over and over again. You dont need
withevents if you use addhandler. Why dont you try something like this

Dim arLabels as new arraylist
dim arButtons as new arraylist

For i = 0 to NumberOfControl s
dim t_Label as label
T_Label = New Label
T_Label.Name = "LabelRecor d_" & Format(i, "000")
T_Label.Tag = i
AddHandler T_Label.DoubleC lick, AddressOf DoSomething
AddHandler T_Label.Click, AddressOf DoSomething
arLabels.add(T_ Label)
.....
' add button
dim T_Button as button
T_ButtonDo = New Button
T_ButtonDo.Name = "ButtonDo_" & Format(i, "000")
arButton.Add(T_ Button)
....

Next

Later in code in a another event I want to access the properties of the
previously created controls again:

For i = 0 to NumberOfControl s

' for example
directcast(arBu tton(i),Button) .Text = "Some value"

Next
Ken
--------------------
"Henry" <he********@nos pam.nospam> wrote in message
news:FC******** *************** ***********@mic rosoft.com...
Hi,

I have to create a variable amount of controls with events for a windows
form in my vb code.

Private WithEvents T_Label As Label
Private WithEvents T_ButtonDo As Button

I create the controls in a loop in load event of the form:

For i = 0 to NumberOfControl s

T_Label = New Label
T_Label.Name = "LabelRecor d_" & Format(i, "000")
T_Label.Tag = i
AddHandler T_Label.DoubleC lick, AddressOf DoSomething
AddHandler T_Label.Click, AddressOf DoSomething
.....
' add button
T_ButtonDo = New Button
T_ButtonDo.Name = "ButtonDo_" & Format(i, "000")

....

Next

Later in code in a another event I want to access the properties of the
previously created controls again:

For i = 0 to NumberOfControl s

' for exambple
T_ButtonDo.Text = "Some value"

Next

The problem is that the code above of course always adresses the last
created control of the Loop in the Load event! How can I adress all controls
created in the loop with their properties and methods ?

Building an array does not solve the problem as I cannot use events together
with an array of controls! And I need the events.

Thanks a lot for help in advance!!!
--
Best regards

Henry
Nov 21 '05 #2
Henry,

I have made a new sample for your question (my previous was using an array
of controls).
This is without that. (There is no need because the form holds an collection
itself).

(When you need to access only those controls as well directly, it is easier
to make an array first before we misunderstood each other, because in the
control collection contains all controls on a form)

\\\Needs only a new windowform and pasting this in
Private Sub Form6_Load(ByVa l sender As System.Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
For i As Integer = 0 To 5
Dim bt As New Button
bt.Location = New Drawing.Point(8 , 8 + i * 24)
bt.TabIndex = i
bt.Text = i.ToString
bt.Tag = i.ToString
AddHandler bt.Click, AddressOf ClickButton
Controls.Add(bt )
Next
End Sub
Private Sub ClickButton(ByV al sender As Object, _
ByVal e As EventArgs)
MessageBox.Show ("Clicked was " & _
DirectCast(send er, Button).Tag.ToS tring)
End Sub
///

I hope this helps a little bit?

Cor
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1664
by: DotNetJunkies User | last post by:
Hie, I create a dynamique HtmlTable, in each cell of this HtmlTable put a new control ( dropdownlist,label,..) and then want to create handler so that if i change the select item in the dop downlist i change the text displayed in the label in the same row. hier is my code: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web;
7
3539
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET) template. Briefly -------------------------------------------------------------------------------------------- I need to create dynamically some controls on the forms, and display these
2
16241
by: charliewest | last post by:
I need to create textboxes in real-time, the actual number of which is determine by a result from a database query. I have been able to create the controls, and then add them to the ASPX page. However, when i submit the page, i am unable to read the values entered in these new textboxes. I have first tried to use System.Web.UI.WebControls.TextBox namespace, however, i was unable to read any value from my textboxes using the following...
2
1648
by: Assaf | last post by:
Hi all, My web form creates & displays dynamic controls when a user clicks a button (code below). No problem. But how do I persist a new control? Every postback destroys the object never to be seen again. Dim MyNewImageButton as New System.Web.UI.WebControls.ImageButton : Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
2
2550
by: Dave Williamson | last post by:
When a ASPX page is created with dynamic controls based on what the user is doing the programmer must recreate the dynamic controls again on PostBack in the Page_Load so that it's events are wired and are called like a static control. Here is the problem that I need to solve. The processing overhead that occurs to determine what dynamic controls need to be added involves business logic and a query or queries of data in a sql server...
3
3976
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which step you're on. This all works fine, but I ran into some trouble when I started creating controls dynamically in my code-behind file. Each panel contains a table which is filled with various radio buttons, text fields and the such which are...
3
1854
by: WebBuilder451 | last post by:
I have a series of dynamic link buttons created based upon a datareader. I've added a click event and it calls the sub ok: example: "while loop through the reader" Dim ltrCtrl As New LiteralControl Dim lbtnCtrl As New LinkButton ltrCtrl.Text = "<br>" lbtnCtrl.Text = "WE: " & dtrCal(10).ToString lbtnCtrl.ToolTip = dtrCal(10).ToString & " these events" & dtrCal(1) lbtnCtrl.ID = "wecc" & i.ToString
7
2555
by: msdev | last post by:
Hello, I am creating my own webbrowser to learn VB .Net. I am stuck on an issue with regards to dynamically-created controls, in this case tabs on a tabcontrol and webbrowsers created within each new tab. Using AddHandler in a class, I can get the delegate Sub to display the name of the webbrowser that has been clicked (by using msgbox(sender.tag), where I set the .tag value when creating the control). What I need to be able to do is...
13
10176
by: rn5a | last post by:
In a shopping cart app, suppose a user has placed 5 orders, I want to show him 5 LinkButtons (one for each order) so that when he clicks the first LinkButton, he would be shown the details of his first order. Likewise if he clicks the second LinkButton, he will be shown the details of the second order he had placed. The Text of the LinkButtons will be 1 2 3 etc. So this user would see 1 2 3 4 5 as the LinkButtons. The problem is...
2
1936
by: germ | last post by:
I am moving a web application from 1.1 to 2.0 This site builds pages dynamically as : PlaceHolder.Controls.Add(LoadControl("~/Controls/Ctl1.ascx")); Everything is working fine as long as the web site is updateable - the .ascx files exist on disk. I would like to use the non-updateable and single assembly options - no ..ascx files. I thought that maybe this would work :...
0
8671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4613
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.