Home All Groups Group Topic Archive Search About

Replacing control arrays in .NET

Author
13 Nov 2007 6:24 PM
Paul
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

Author
13 Nov 2007 6:41 PM
rowe_newsgroups
On Nov 13, 1:24 pm, Paul <P***@discussions.microsoft.com> wrote:
Show quoteHide quote
> 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
Author
13 Nov 2007 6:54 PM
Paul
Thanks Seth, that sounds great. Some sample code would be really good, if you
don't mind?

Thanks very much,
Paul

Show quoteHide quote
"rowe_newsgroups" wrote:

> On Nov 13, 1:24 pm, Paul <P***@discussions.microsoft.com> wrote:
> > 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
>
>
Author
13 Nov 2007 7:37 PM
rowe_newsgroups
On Nov 13, 1:54 pm, Paul <P***@discussions.microsoft.com> wrote:
Show quoteHide quote
> 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.com> wrote:
> > > 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
Author
14 Nov 2007 11:29 AM
Paul
Thanks Seth, that is exactly what I need.

Thank you very much indeed!

Paul

Show quoteHide quote
"rowe_newsgroups" wrote:

> On Nov 13, 1:54 pm, Paul <P***@discussions.microsoft.com> wrote:
> > 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.com> wrote:
> > > > 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
>
>
Author
14 Nov 2007 2:03 PM
zacks
On Nov 13, 1:24 pm, Paul <P***@discussions.microsoft.com> wrote:
Show quoteHide quote
> 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.