Replacing control arrays in .NET

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?UGF1bA==?=

    #1

    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.t ext = 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

  • rowe_newsgroups

    #2
    Re: Replacing control arrays in .NET

    On Nov 13, 1:24 pm, Paul <P...@discussio ns.microsoft.co mwrote:
    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.t ext = 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.Fin d("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

    Comment

    • =?Utf-8?B?UGF1bA==?=

      #3
      Re: Replacing control arrays in .NET

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

      Thanks very much,
      Paul

      "rowe_newsgroup s" wrote:
      On Nov 13, 1:24 pm, Paul <P...@discussio ns.microsoft.co mwrote:
      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.t ext = 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.Fin d("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
      >
      >

      Comment

      • rowe_newsgroups

        #4
        Re: Replacing control arrays in .NET

        On Nov 13, 1:54 pm, Paul <P...@discussio ns.microsoft.co mwrote:
        Thanks Seth, that sounds great. Some sample code would be really good, if you
        don't mind?
        >
        Thanks very much,
        Paul
        >
        "rowe_newsgroup s" wrote:
        On Nov 13, 1:24 pm, Paul <P...@discussio ns.microsoft.co mwrote:
        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.t ext = 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.Fin d("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(ByVa l sender As System.Object, ByVal e As
        System.EventArg s) Handles MyBase.Load
        InitializeDicti onaryForMethod2 ()

        '// 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.C ontrols.Find("T extBox" & 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).Te xt = String.Format(" Hello, World. I'm
        TextBox{0}", i)
        Next
        End Sub

        Private Sub InitializeDicti onaryForMethod2 ()
        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

        Comment

        • =?Utf-8?B?UGF1bA==?=

          #5
          Re: Replacing control arrays in .NET

          Thanks Seth, that is exactly what I need.

          Thank you very much indeed!

          Paul

          "rowe_newsgroup s" wrote:
          On Nov 13, 1:54 pm, Paul <P...@discussio ns.microsoft.co mwrote:
          Thanks Seth, that sounds great. Some sample code would be really good, if you
          don't mind?

          Thanks very much,
          Paul

          "rowe_newsgroup s" wrote:
          On Nov 13, 1:24 pm, Paul <P...@discussio ns.microsoft.co mwrote:
          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.t ext = 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.Fin d("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(ByVa l sender As System.Object, ByVal e As
          System.EventArg s) Handles MyBase.Load
          InitializeDicti onaryForMethod2 ()
          >
          '// 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.C ontrols.Find("T extBox" & 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).Te xt = String.Format(" Hello, World. I'm
          TextBox{0}", i)
          Next
          End Sub
          >
          Private Sub InitializeDicti onaryForMethod2 ()
          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
          >
          >

          Comment

          • zacks@construction-imaging.com

            #6
            Re: Replacing control arrays in .NET

            On Nov 13, 1:24 pm, Paul <P...@discussio ns.microsoft.co mwrote:
            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.t ext = 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.

            Comment

            Working...