Replacing control arrays in .NET

G

Guest

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
 
R

rowe_newsgroups

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
 
G

Guest

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

Thanks very much,
Paul
 
R

rowe_newsgroups

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

Thanks very much,
Paul

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
 
Z

zacks

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Control arrays and FormN.Label31 16
Control Arrays 4
control arrays 10
control arrays 3
Control arrays converted from VB6 4
Control Arrays 3
Control arrays converted from VB6 8
Control Arrays Question 5

Top