locked
Validation of fields in form RRS feed

  • Question

  • Hi

    I am trying to do the following.

    I have a form (VB visual studio) whis has 70 fields. I have almost 180 validation about to implement on that form. Any one please let me how I can best implement ??

    1. Am I able to create dll file and put all validation, so when validation triggers, the field will check the necessary dll file. ( I am not sure I am right or not also I am new for this language)
    2. Or Do I have to put all validation in the form it self

    Or any other effective way to do it .

    Thanks in advance
    Wednesday, September 20, 2006 6:27 PM

Answers

  • I assume you mean text boxes, not fields.  You don't want to put your validation code in a seperate assembly, you'll have a big problem with circular references and giving the validation code access to the form's text boxes.

    Another approach is to make the text boxes smart about the kind of validation you need to do.  You probably have several simple rules like "should be numeric" and "can't be empty".  You can create your own textbox control that implements those rules, selectively if necessary.  Add a new class to your project and enter this code:

    Public Class NumericTextBox
      Inherits TextBox
      Private mMinValue As Double = 0
      Public Property MinValue() As Double
        Get
          Return mMinValue
        End Get
        Set(ByVal value As Double)
          mMinValue = value
        End Set
      End Property
      Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
        If Not IsNumeric(Me.Text) Then
          e.Cancel = True
        Else
          Dim d As Double = CDbl(Me.Text)
          If d < mMinValue Then e.Cancel = True
        End If
      End Sub
    End Class

    Build your project, now you can drop a NumericTextBox on your form.  Note how the "MinValue" property is now editable in the Properties window, allowing you to set the rules with the designer rather than by using code. 

    Anything is possible of course.  I used the OnValidating() method to do the validation, you can implement your own as needed...
    Wednesday, September 20, 2006 11:33 PM

All replies

  • I assume you mean text boxes, not fields.  You don't want to put your validation code in a seperate assembly, you'll have a big problem with circular references and giving the validation code access to the form's text boxes.

    Another approach is to make the text boxes smart about the kind of validation you need to do.  You probably have several simple rules like "should be numeric" and "can't be empty".  You can create your own textbox control that implements those rules, selectively if necessary.  Add a new class to your project and enter this code:

    Public Class NumericTextBox
      Inherits TextBox
      Private mMinValue As Double = 0
      Public Property MinValue() As Double
        Get
          Return mMinValue
        End Get
        Set(ByVal value As Double)
          mMinValue = value
        End Set
      End Property
      Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
        If Not IsNumeric(Me.Text) Then
          e.Cancel = True
        Else
          Dim d As Double = CDbl(Me.Text)
          If d < mMinValue Then e.Cancel = True
        End If
      End Sub
    End Class

    Build your project, now you can drop a NumericTextBox on your form.  Note how the "MinValue" property is now editable in the Properties window, allowing you to set the rules with the designer rather than by using code. 

    Anything is possible of course.  I used the OnValidating() method to do the validation, you can implement your own as needed...
    Wednesday, September 20, 2006 11:33 PM
  • Hi

    Thank you very much.

    I have created class file (class1) and pasted your code. Built the project. But When I go to form designer and select on of the text box properties, it doesn't show any difference.

    Am I doing anything wrong.?

    Thanks
    Thursday, September 21, 2006 1:23 PM
  • Make sure you drop the "NumericTextBox" control on your form, not a regular TextBox.  After building, you'll find that control on the top of the Toolbox.  Browse the properties of the control, you should see the "MinValue" property with a value of 0.
    Thursday, September 21, 2006 1:27 PM
  • Hi

    Yes!! I found. Thank you very much.

    Sorry to ask another question.
    I have combo box , which is displaying only one field from the table But I want to display TWO field in the combo box.(like in Access database). Any help would be appreciatable

    Thanks in advance
    Thursday, September 21, 2006 2:01 PM
  • You already started a thread for that question.  Hang in there, an answer is bound to show up...
    Thursday, September 21, 2006 2:09 PM
  • Hi
    Thank you very mush your answer is very helpful. So I am going to create class files with validation and USE them in the form.I hope it is not slowing down the application performance.




    Thursday, September 21, 2006 2:40 PM
  •  I'm trying to validate everything at once when the save method is called in the form and got focus to relevant field if validation faild. Do you suggest any best implementation?.


    Thanks in advance
    Thursday, September 21, 2006 4:16 PM
  • I'm not sure what you are asking.  I would use the sub-classed control to only do basic validation; numeric, not blank, stuff like that.  If you use the OnValidating method, the focus automatically stays on the control and the user is forced to type valid data.  Displaying a MessageBox might be helpful.

    More advanced validation, like comparing the value of one textbox with another, should be handled by a separate validation procedure, called by the Save button click event handler.  I'd do something like this:

      Private Function ValidateData() As Boolean
        If CDbl(TextBox1.Text) >= CDbl(TextBox2.Text) Then
          TextBox1.Focus()
          MsgBox("Min weight must be less than max weight", MsgBoxStyle.Exclamation, "Nobugz waz here")
          Return False
        End If
        Return True
      End Function

      Private Sub bntSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not ValidateData() Then Return
        '--- Do your stuff here
      End Sub

    Thursday, September 21, 2006 4:48 PM