PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Textboxes as array-VBForums
Results 1 to 4 of 4

Thread: Textboxes as array

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    55

    Textboxes as array

    Hello guys once again.
    I have a problem to code textboxes as an array.What I mean.

    I have 3 textboxes and I am trying to group them using some kind of index, so having textbox(j) instead of textbox1, textbox2, textbox3.
    I would like to have something like this because i want to use some loop instructions.

    I have written the following code as an example.
    In brief the code is this
    If textbox1 and textbox2 is numeric then textbox3="done"
    Else
    give a msgbox("blah....")
    ---------
    Dim J As Integer
    Dim txt As TextBox()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For Me.J = 0 To 1
    If Not IsNumeric(txt(J).Text) Then
    MsgBox("PLEASE ENTER ONLY NUMBERS!", MsgBoxStyle.Critical, "WARNING")
    Else
    TextBox3.Text = "done"
    End If
    Next J
    end sub

    -------------

    When i run this code i get a message "NullReferenceException was unhandeled etc"

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,698

    Re: Textboxes as array

    create a list(of textbox) and add each one of your textboxes to it when the form initializes...
    i.e.

    Code:
    dim tbList as new list(of textbox)
    tblist.add(textbox1)
    tblist.add(textbox2)
    tbList.add(textbox3)
    'etc...
    The first textbox added can be referenced using tbList(0), the second tbList(1), and so on
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Textboxes as array

    Or, put the textboxes into a container control (panel, group box, flow layout panel, etc.) and use the containers Controls collection, eg. Panel1.Controls(i)

    Or, use the form's Control's collection with an OfType statement

    Or, abandon textboxes and use NumericUpDown controls so that you don't need to validate and therefore don't need the loop at all!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,599

    Re: Textboxes as array

    If what you're actually trying to achieve is validation then you don't need any looping. Validation is already built into the control and the form, e.g.
    vb.net Code:
    1. Imports System.ComponentModel
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub NumericTextBox_Validating(sender As Object, e As CancelEventArgs) Handles TextBox2.Validating,
    6.                                                                                           TextBox1.Validating
    7.         Dim field = DirectCast(sender, TextBox)
    8.  
    9.         If Not Double.TryParse(field.Text, Nothing) Then
    10.             field.SelectAll()
    11.             field.HideSelection = False
    12.  
    13.             MessageBox.Show("Please enter a numeric value.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error)
    14.  
    15.             field.HideSelection = False
    16.  
    17.             'Don't let the user leave the field until they enter valid input.
    18.             e.Cancel = False
    19.         End If
    20.     End Sub
    21.  
    22.     Private Sub saveButton_Click(sender As Object, e As EventArgs) Handles saveButton.Click
    23.         If Me.ValidateChildren() Then
    24.             'All fields have passed validation so save with confidence.
    25.         End If
    26.     End Sub
    27.  
    28. End Class
    The Validating even is raised when the user tries to shift focus from the TextBox and setting e.Cancel prevents that from happening. Calling ValidateChildren will raise the event on all controls so even those that have never received focus will be validated.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width