How can I make textbox(n).text in visualbasic.net ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • popeyb
    New Member
    • Apr 2011
    • 5

    How can I make textbox(n).text in visualbasic.net ?

    I'm a beginner in visualbasic.net
    I've 50 textbox from 1 to 50 and my question is how can get a value o show a value in a specific textbox?
    for example the user enter a number to a text box and I save the user's number to variable X
    now I need to show in textbo(x) the number which enered the user or get the value of textbox(x).
    Do I need a array of 50 textboxes? how?
    please help me on this
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    It would probably be easiest to use an array or generic List(Of TextBoxe) for this project.

    I would not drag/drop 50 TextBoxes onto the window.
    I would declare a private collection of TextBoxes as a member that is part of the VB.NET code-behind. I would initialize it in the the Load event. After initalizing the collection of TextBoxes, I would add them to the window/page/user-control...say in a Panel that is already part of the window/page/user-control so that I know where they'll end up.

    For example:
    Code:
    Class AWindow
    
      Private _textBoxes As List(Of TextBox)
      
      Public Sub AWindow(ByVal sender As Object, ByVal e As EventArgs) Handles AWindow.Loaded
        _textBoxes = new List(Of TextBox)
        For i As Integer = 0 to 49
          Dim aTextBox As New TextBox
          _textBoxes.Add(aTextBox)
         myPanel.Add(aTextBox) 'myPanel is part of the page
        Next
      End Sub
    End Class
    Now when the user types a number, you can enter that number into the corresponding TextBox... eg:_textBoxes(user ProvidedNumber) = value.



    -Frinny

    Comment

    • popeyb
      New Member
      • Apr 2011
      • 5

      #3
      Thanks for ur help
      sorry but as I said I'm a beginner...
      I use visual basic 2010 express
      I've already draged and drop 50 textboxes and renamed them to a1...a50
      Still should I use your code? where should I write them? in button_click or public class or textbox keypress?????
      It would be really helpfull if you could help me more on this...
      thanks mate

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Okay, so you dragged 50 TextBoxes onto the page...

        Your code wont be as neat and tidy as the code I suggested but it will work just the same.

        Instead of accessing the TextBox in the array at the index that the user provided...have a Select Case or If block that checks the number that the user entered and sets the appropriate TextBox.

        The code that I posted would be placed in .vb file that is part of your window. Double click on your window to get to it... but you don't need to use it if you aren't comfortable with it...you could do as I just suggested with the Select Case or If blocks.

        -Frinny
        Last edited by Frinavale; Apr 12 '11, 01:34 PM.

        Comment

        • popeyb
          New Member
          • Apr 2011
          • 5

          #5
          Thanks man

          Finally, I could finish it by select case...it took a lot of work but it works perfectly...nex t time I will take a shorter code.
          Here I've another question if u could help me again
          I need to restart the prgorams from beginig so I can use it for a clear/reset button and in case of error
          is there any command that I can use for example

          If String.IsNullOr Empty(TextBox1. Text) Then 'restart prgorm'
          so I can prevent the visual basic run the next line by restaring program and take it to the begining of form.
          hope u got what I meaning

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            I'm not following you...

            If you want to clear the TextBoxes, then go through each of them and set their Text property to an empty string.

            What I'm not sure of is why you are checking a TextBox's Text property during this process...?

            -Frinny

            Comment

            • popeyb
              New Member
              • Apr 2011
              • 5

              #7
              Sorry dude
              I had some internet problem these days...
              forget about everything let's focus on this:

              I need to restart the program...is ther anyway to do so?

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Well, one approach would be to use the Shell function to open your program....and after it's opened, close the current instance of the program.

                Comment

                • balame2004
                  New Member
                  • Mar 2008
                  • 142

                  #9
                  Better to use LINq query to access controls collection

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    balame2004,

                    Popeyb has chosen not to use collections of controls in the solution ;)

                    -Frinny

                    Comment

                    • popeyb
                      New Member
                      • Apr 2011
                      • 5

                      #11
                      still have this problem and I don't know how to solve it...
                      I nees to restar the prgoram from begeining...

                      Comment

                      Working...