473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 NumberOfControls

T_Label = New Label
T_Label.Name = "LabelRecord_" & Format(i, "000")
T_Label.Tag = i
AddHandler T_Label.DoubleClick, 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 NumberOfControls

' 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 11485
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 NumberOfControls
dim t_Label as label
T_Label = New Label
T_Label.Name = "LabelRecord_" & Format(i, "000")
T_Label.Tag = i
AddHandler T_Label.DoubleClick, 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 NumberOfControls

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

Next
Ken
--------------------
"Henry" <he********@nospam.nospam> wrote in message
news:FC**********************************@microsof t.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 NumberOfControls

T_Label = New Label
T_Label.Name = "LabelRecord_" & Format(i, "000")
T_Label.Tag = i
AddHandler T_Label.DoubleClick, 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 NumberOfControls

' 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(ByVal sender As System.Object, _
ByVal e As System.EventArgs) 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(ByVal sender As Object, _
ByVal e As EventArgs)
MessageBox.Show("Clicked was " & _
DirectCast(sender, Button).Tag.ToString)
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
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...
7
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)...
2
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....
2
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...
2
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...
3
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...
3
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...
7
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...
13
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.