-
Jul 13th, 2013, 08:51 AM
#1
Thread Starter
Member
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"
-
Jul 13th, 2013, 09:11 AM
#2
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
-
Jul 13th, 2013, 10:19 AM
#3
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!
-
Jul 13th, 2013, 10:42 AM
#4
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:
Imports System.ComponentModel Public Class Form1 Private Sub NumericTextBox_Validating(sender As Object, e As CancelEventArgs) Handles TextBox2.Validating, TextBox1.Validating Dim field = DirectCast(sender, TextBox) If Not Double.TryParse(field.Text, Nothing) Then field.SelectAll() field.HideSelection = False MessageBox.Show("Please enter a numeric value.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error) field.HideSelection = False 'Don't let the user leave the field until they enter valid input. e.Cancel = False End If End Sub Private Sub saveButton_Click(sender As Object, e As EventArgs) Handles saveButton.Click If Me.ValidateChildren() Then 'All fields have passed validation so save with confidence. End If End Sub 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|