|
-
Oct 29th, 2003, 11:01 PM
#1
Thread Starter
New Member
Validating Event
I have a form with 12 text boxes that require the same input validation. One for each month. How do I get it so that I only use one validating event to handle every text box instead of writing a seperate validating event for each text box? Thanks. This is what I have so far...
Private Sub txtjan_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtJan.Validating, txtFeb.Validating, txtMar.Validating, txtApr.Validating, txtMay.Validating, txtJun.Validating, txtJul.Validating, txtAug.Validating, txtSep.Validating, txtOct.Validating, txtNov.Validating, txtDec.Validating
'Need help here
If txtJan.Text = "" OrElse Not IsNumeric(txtJan.Text) OrElse CSng(txtJan.Text) < 0 Then
MessageBox.Show("You cannot enter a value less than zero.", "Error")
txtJan.SelectionStart = 0
txtJan.SelectionLength = txtJan.Text.Length
e.Cancel = True
Else
e.Cancel = False
End If
End Sub
-
Oct 29th, 2003, 11:30 PM
#2
Frenzied Member
how about using a control array? Since they'll all be a part of the same control array you should be able to validate them together.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Oct 29th, 2003, 11:36 PM
#3
Thread Starter
New Member
urgh..
Honestly, I could use an array, but I don't want to...I want to know how to do this without an array, if it is possible. If it is impossible then I will use an array.
-
Oct 30th, 2003, 10:33 AM
#4
Frenzied Member
how about just writting a validation sub, then sending the text in the textboxes to it?
something like this (C#)
Code:
private void button1_Click(object sender, System.EventArgs e)
{
if(validation(textBox1.Text) != "okay")
MessageBox.Show("TextBox1" + validation(textBox1.Text), "Invalid Text");
if(validation(textBox2.Text) != "okay")
MessageBox.Show("TextBox2" + validation(textBox2.Text), "Invalid Text");
}
private string validation(string data)
{
string info;
if(data == "")
info = " can't be empty";
else if(Information.IsNumeric(data) == false)
info = " must be numeric";
else if(Convert.ToSingle(data) < 0)
info = " can't be less than zero";
else
info = "okay";
return info;
}
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Oct 30th, 2003, 10:46 AM
#5
you can add a handler for all the textboxes , rather than having a long list of them at the end of a sub, eg:
VB Code:
[Color=Blue]Private[/COLOR] [Color=Blue]WithEvents[/COLOR] txt_box [Color=Blue]As[/COLOR] TextBox
[Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] Form1_Load([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] System.Object, [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] [Color=Blue]MyBase[/COLOR].Load
[Color=Blue]Dim[/COLOR] tb [Color=Blue]As[/COLOR] Control
[Color=Blue]For[/COLOR] [Color=Blue]Each[/COLOR] tb [Color=Blue]In[/COLOR] [Color=Blue]Me[/COLOR].Controls
[Color=Blue]If[/COLOR] [Color=Blue]TypeOf[/COLOR] tb [Color=Blue]Is[/COLOR] TextBox [Color=Blue]Then
[/COLOR] [Color=Blue]AddHandler[/COLOR] tb.Validating, [Color=Blue]AddressOf[/COLOR] txt_box_Validating
[Color=Blue]End[/COLOR] [Color=Blue]If
[/COLOR] [Color=Blue]Next
[/COLOR] [Color=Blue]End[/COLOR] [Color=Blue]Sub
[/COLOR] [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] txt_box_Validating([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] [Color=Blue]Object[/COLOR], [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.ComponentModel.CancelEventArgs) [Color=Blue]Handles[/COLOR] txt_box.Validating
[Color=Green]'///[/COLOR] [Color=Green]all[/COLOR] [Color=Green]textboxes[/COLOR] [Color=Green]validating[/COLOR] [Color=Green]events[/COLOR] [Color=Green]now[/COLOR] [Color=Green]handled[/COLOR] [Color=Green]here[/COLOR] [Color=Green].[/COLOR] [Color=Green]
[/COLOR] [Color=Blue]End[/COLOR] [Color=Blue]Sub[/COLOR]
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
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
|