Results 1 to 5 of 5

Thread: Validating Event

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Tacoma, WA
    Posts
    13

    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

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Tacoma, WA
    Posts
    13

    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.

  4. #4
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    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

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    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:
    1. [Color=Blue]Private[/COLOR] [Color=Blue]WithEvents[/COLOR] txt_box [Color=Blue]As[/COLOR] TextBox
    2.  
    3.  
    4.     [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
    5.         [Color=Blue]Dim[/COLOR] tb [Color=Blue]As[/COLOR] Control
    6.         [Color=Blue]For[/COLOR] [Color=Blue]Each[/COLOR] tb [Color=Blue]In[/COLOR] [Color=Blue]Me[/COLOR].Controls
    7.             [Color=Blue]If[/COLOR] [Color=Blue]TypeOf[/COLOR] tb [Color=Blue]Is[/COLOR] TextBox [Color=Blue]Then
    8. [/COLOR]                [Color=Blue]AddHandler[/COLOR] tb.Validating, [Color=Blue]AddressOf[/COLOR] txt_box_Validating
    9.             [Color=Blue]End[/COLOR] [Color=Blue]If
    10. [/COLOR]        [Color=Blue]Next
    11. [/COLOR]    [Color=Blue]End[/COLOR] [Color=Blue]Sub
    12.  
    13. [/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
    14.         [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]
    15. [/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
  •  



Click Here to Expand Forum to Full Width