473,320 Members | 1,945 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,320 software developers and data experts.

Replacing control arrays in .NET

Hi everyone,

I've just started using vb.net (vs.net 2005) after programming in VB6. I've
noticed that control arrays are no longer used and need a little help. In VB6
I would have done this to loop through my recordset and set some values for
text boxes on my form:

Dim i as integer
i = 0
for i = 0 to rstMyRecordSet.Recordcount -1
txtMyTextBox(i).text = rstMyRecordSet.Fields(i).Value
Next i

Obviously I can't do that anymore so can anyone help?

I have a form with 30 text boxes. Each textbox is called txtMyTextbox1,
txtMyTextbox2, txtMyTextbox3 etc etc to 30.

I have a datareader with my data in it and I would like to put the values in
it into my text boxes without having to write out txtMyTextbox1.text = etc
for all 30. Or maybe a better way I can bind my text boxes to my datareader?

Can anyone suggest a way to do it in code?

Any help, greatly appreciated.

Many thanks
Paul

Nov 13 '07 #1
5 2220
On Nov 13, 1:24 pm, Paul <P...@discussions.microsoft.comwrote:
Hi everyone,

I've just started using vb.net (vs.net 2005) after programming in VB6. I've
noticed that control arrays are no longer used and need a little help. In VB6
I would have done this to loop through my recordset and set some values for
text boxes on my form:

Dim i as integer
i = 0
for i = 0 to rstMyRecordSet.Recordcount -1
txtMyTextBox(i).text = rstMyRecordSet.Fields(i).Value
Next i

Obviously I can't do that anymore so can anyone help?

I have a form with 30 text boxes. Each textbox is called txtMyTextbox1,
txtMyTextbox2, txtMyTextbox3 etc etc to 30.

I have a datareader with my data in it and I would like to put the values in
it into my text boxes without having to write out txtMyTextbox1.text = etc
for all 30. Or maybe a better way I can bind my text boxes to my datareader?

Can anyone suggest a way to do it in code?

Any help, greatly appreciated.

Many thanks
Paul
You can use either Me.Controls.Find("txtMyTextBox" & i.ToString()) to
locate the control by it's name. One other option is to use a Generic
dictionary(Of Integer, TextBox) to hold the index number and the
reference to the textbox. You could build this dictionary when the
form loads and then grab the necessary textbox by key.

Let me know if you need some sample code - I'm out of the IDE at the
moment so my description may be a bit scrambled.

Thanks,

Seth Rowe

Nov 13 '07 #2
Thanks Seth, that sounds great. Some sample code would be really good, if you
don't mind?

Thanks very much,
Paul

"rowe_newsgroups" wrote:
On Nov 13, 1:24 pm, Paul <P...@discussions.microsoft.comwrote:
Hi everyone,

I've just started using vb.net (vs.net 2005) after programming in VB6. I've
noticed that control arrays are no longer used and need a little help. In VB6
I would have done this to loop through my recordset and set some values for
text boxes on my form:

Dim i as integer
i = 0
for i = 0 to rstMyRecordSet.Recordcount -1
txtMyTextBox(i).text = rstMyRecordSet.Fields(i).Value
Next i

Obviously I can't do that anymore so can anyone help?

I have a form with 30 text boxes. Each textbox is called txtMyTextbox1,
txtMyTextbox2, txtMyTextbox3 etc etc to 30.

I have a datareader with my data in it and I would like to put the values in
it into my text boxes without having to write out txtMyTextbox1.text = etc
for all 30. Or maybe a better way I can bind my text boxes to my datareader?

Can anyone suggest a way to do it in code?

Any help, greatly appreciated.

Many thanks
Paul

You can use either Me.Controls.Find("txtMyTextBox" & i.ToString()) to
locate the control by it's name. One other option is to use a Generic
dictionary(Of Integer, TextBox) to hold the index number and the
reference to the textbox. You could build this dictionary when the
form loads and then grab the necessary textbox by key.

Let me know if you need some sample code - I'm out of the IDE at the
moment so my description may be a bit scrambled.

Thanks,

Seth Rowe

Nov 13 '07 #3
On Nov 13, 1:54 pm, Paul <P...@discussions.microsoft.comwrote:
Thanks Seth, that sounds great. Some sample code would be really good, if you
don't mind?

Thanks very much,
Paul

"rowe_newsgroups" wrote:
On Nov 13, 1:24 pm, Paul <P...@discussions.microsoft.comwrote:
Hi everyone,
I've just started using vb.net (vs.net 2005) after programming in VB6. I've
noticed that control arrays are no longer used and need a little help. In VB6
I would have done this to loop through my recordset and set some values for
text boxes on my form:
Dim i as integer
i = 0
for i = 0 to rstMyRecordSet.Recordcount -1
txtMyTextBox(i).text = rstMyRecordSet.Fields(i).Value
Next i
Obviously I can't do that anymore so can anyone help?
I have a form with 30 text boxes. Each textbox is called txtMyTextbox1,
txtMyTextbox2, txtMyTextbox3 etc etc to 30.
I have a datareader with my data in it and I would like to put the values in
it into my text boxes without having to write out txtMyTextbox1.text = etc
for all 30. Or maybe a better way I can bind my text boxes to my datareader?
Can anyone suggest a way to do it in code?
Any help, greatly appreciated.
Many thanks
Paul
You can use either Me.Controls.Find("txtMyTextBox" & i.ToString()) to
locate the control by it's name. One other option is to use a Generic
dictionary(Of Integer, TextBox) to hold the index number and the
reference to the textbox. You could build this dictionary when the
form loads and then grab the necessary textbox by key.
Let me know if you need some sample code - I'm out of the IDE at the
moment so my description may be a bit scrambled.
Thanks,
Seth Rowe
No problem!

The below contains two methods, the first (cleverly named Method1) is
very compact and relies of the Controls.Find method. The second is
probably more performant, but requires additional setup and is more
verbose. This code assumes you have 4 textboxes on a form named
TextBox1, TextBox2, TextBox3 and TextBox4.

//////////////////////
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
InitializeDictionaryForMethod2()

'// Uncomment one of the below methods

' Method1()
' Method2()
End Sub

Private Sub Method1()
For i As Integer = 1 To 4
Dim textBox As TextBox =
DirectCast(Me.Controls.Find("TextBox" & i.ToString(), True)(0),
TextBox)
textBox.Text = String.Format("Hello, World. I'm
TextBox{0}", i)
Next
End Sub

Private Sub Method2()
For i As Integer = 1 To 4
textboxes(i).Text = String.Format("Hello, World. I'm
TextBox{0}", i)
Next
End Sub

Private Sub InitializeDictionaryForMethod2()
textboxes = New Dictionary(Of Integer, TextBox)()

textboxes.Add(1, Me.TextBox1)
textboxes.Add(2, Me.TextBox2)
textboxes.Add(3, Me.TextBox3)
textboxes.Add(4, Me.TextBox4)
End Sub

Private textboxes As Dictionary(Of Integer, TextBox)

End Class
//////////////////////

Thanks,

Seth Rowe

Nov 13 '07 #4
Thanks Seth, that is exactly what I need.

Thank you very much indeed!

Paul

"rowe_newsgroups" wrote:
On Nov 13, 1:54 pm, Paul <P...@discussions.microsoft.comwrote:
Thanks Seth, that sounds great. Some sample code would be really good, if you
don't mind?

Thanks very much,
Paul

"rowe_newsgroups" wrote:
On Nov 13, 1:24 pm, Paul <P...@discussions.microsoft.comwrote:
Hi everyone,
I've just started using vb.net (vs.net 2005) after programming in VB6. I've
noticed that control arrays are no longer used and need a little help. In VB6
I would have done this to loop through my recordset and set some values for
text boxes on my form:
Dim i as integer
i = 0
for i = 0 to rstMyRecordSet.Recordcount -1
txtMyTextBox(i).text = rstMyRecordSet.Fields(i).Value
Next i
Obviously I can't do that anymore so can anyone help?
I have a form with 30 text boxes. Each textbox is called txtMyTextbox1,
txtMyTextbox2, txtMyTextbox3 etc etc to 30.
I have a datareader with my data in it and I would like to put the values in
it into my text boxes without having to write out txtMyTextbox1.text = etc
for all 30. Or maybe a better way I can bind my text boxes to my datareader?
Can anyone suggest a way to do it in code?
Any help, greatly appreciated.
Many thanks
Paul
You can use either Me.Controls.Find("txtMyTextBox" & i.ToString()) to
locate the control by it's name. One other option is to use a Generic
dictionary(Of Integer, TextBox) to hold the index number and the
reference to the textbox. You could build this dictionary when the
form loads and then grab the necessary textbox by key.
Let me know if you need some sample code - I'm out of the IDE at the
moment so my description may be a bit scrambled.
Thanks,
Seth Rowe

No problem!

The below contains two methods, the first (cleverly named Method1) is
very compact and relies of the Controls.Find method. The second is
probably more performant, but requires additional setup and is more
verbose. This code assumes you have 4 textboxes on a form named
TextBox1, TextBox2, TextBox3 and TextBox4.

//////////////////////
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
InitializeDictionaryForMethod2()

'// Uncomment one of the below methods

' Method1()
' Method2()
End Sub

Private Sub Method1()
For i As Integer = 1 To 4
Dim textBox As TextBox =
DirectCast(Me.Controls.Find("TextBox" & i.ToString(), True)(0),
TextBox)
textBox.Text = String.Format("Hello, World. I'm
TextBox{0}", i)
Next
End Sub

Private Sub Method2()
For i As Integer = 1 To 4
textboxes(i).Text = String.Format("Hello, World. I'm
TextBox{0}", i)
Next
End Sub

Private Sub InitializeDictionaryForMethod2()
textboxes = New Dictionary(Of Integer, TextBox)()

textboxes.Add(1, Me.TextBox1)
textboxes.Add(2, Me.TextBox2)
textboxes.Add(3, Me.TextBox3)
textboxes.Add(4, Me.TextBox4)
End Sub

Private textboxes As Dictionary(Of Integer, TextBox)

End Class
//////////////////////

Thanks,

Seth Rowe

Nov 14 '07 #5
On Nov 13, 1:24 pm, Paul <P...@discussions.microsoft.comwrote:
Hi everyone,

I've just started using vb.net (vs.net 2005) after programming in VB6. I've
noticed that control arrays are no longer used and need a little help. In VB6
I would have done this to loop through my recordset and set some values for
text boxes on my form:

Dim i as integer
i = 0
for i = 0 to rstMyRecordSet.Recordcount -1
txtMyTextBox(i).text = rstMyRecordSet.Fields(i).Value
Next i

Obviously I can't do that anymore so can anyone help?

I have a form with 30 text boxes. Each textbox is called txtMyTextbox1,
txtMyTextbox2, txtMyTextbox3 etc etc to 30.

I have a datareader with my data in it and I would like to put the values in
it into my text boxes without having to write out txtMyTextbox1.text = etc
for all 30. Or maybe a better way I can bind my text boxes to my datareader?

Can anyone suggest a way to do it in code?

Any help, greatly appreciated.

Many thanks
Paul
You can use control arrays in .NET, just not at design time. You have
to do it completely in code.

Nov 14 '07 #6

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

Similar topics

2
by: Merlin | last post by:
Hi I have a control that allows embeddable editors, so for example I can set a property of controlsEmbeddableEditor =me.TextBox1 on my form, no problem here - what I want to do is the same thing...
3
by: B-Dog | last post by:
I'm capturing the checked radio button to XML file using the name of the radio button. I want to read my xml file to find which button was checked on close and the check the appropriate button...
3
by: Robert | last post by:
How can I declare in VB .NET an array of labels for example and afterwards using a FOR structure load every component of the array? I've used this code but it doesn't work: dim x(10) as label...
20
by: samean | last post by:
Hello, Could you explain me,In VB6 using control array,and how about VB.net. Thanks
8
by: Greg | last post by:
In VB6 I made heavy use of control arrays I see they have been 'deprecated' in vb.Net, with a questionable explanation that they are no longer necessary which just addresses the event issue!...
3
by: Lars Grobe | last post by:
Hi, first hello, I am new to the list, and I guess my question will show that clearly. I want to use some vector operations (at the moment altivec) in existing code. It is a raytracing-based...
9
by: Michael D. Ober | last post by:
In VB 6, you can create control arrays for your option groups and scan with the following code dim opt as OptionButton for each opt in OptionGroup ' Do something next opt I know VB 2005...
4
by: Arne Beruldsen | last post by:
I'm a recent convert to VB.net from VB6...and I can't believe they don't support control arrays. I have an app that uses a ton of Control Arrays...mostly labels and text boxes. I need some...
13
by: Just_a_fan | last post by:
I am adding a bunch of controls with the code below. Problem 1: When program flow passes to "UpperChanged" when I click it, the control name is undefined. When I enter: If udUpperLim1.Value 1...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.