tags:

views:

53

answers:

2

Hi, I am trying to validate textbox values during runtime in vb.net I have following code which is validating txtno from database table tblmachines. But i have problem with chartype and stringtype. Is there any other solution to fix that problem?

Private Sub txtno_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtno.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            e.Handled = True
            If (Me.txtno.Text = "") Then
                Interaction.MsgBox("!!!!!!!!Machine number can not be empty. Please Correct.!!!!!!!!", &H40, "Check Machine Number")
                Me.txtno.Focus()
            ElseIf (Me.txtno.Text = "0") Then
                Me.txtturnover.Focus()
            Else
                Dim table As DataTable = Me.DataSet1.Tables.Item("tblmachines")
                Dim defaultView As DataView = table.DefaultView
                defaultView.RowFilter = ("local_no='" & Me.txtno.Text & "'")
                If ((Char.IsLetter(CharType.FromString(Me.txtno.Text)) Or (defaultView.Count = 0)) Or (StringType.StrCmp(Me.txtno.Text, "", False) = 0)) Then
                    Interaction.MsgBox("This machine is not on database. Please correct machine number.", &H40, "Check Machine Number")
                    Me.txtno.Focus()
                Else
                    Me.txtturnover.Focus()
                End If

            End If
        End If
+2  A: 

I think you'd be better off using validation from the Winforms library than in the KeyPress event. KeyPress is going to cause a lot of validation scripts to be run and will bog down your app.

I think you should do the validation in the following steps

  1. Check to ensure that there is data in the texbox
  2. Validate that the data entered in the textbox is in the proper format. You can use regular code, or possibly a RegEx for that.
  3. Validate that the number entered is a part of the database. I made it a habit reset the filters to a blank string before applying any new filters to the table.

The good part about using inbuilt validation is that you do not have to worry about moving focus around.

The control has a property called CausesValidation - if you set it to true (which it already is) then this control will run the validation code. If you do not want the control to run validation code, you can turn the property to False.

The validation happens as Focus shifts - so when you focus off a control, it's validation events are fired.

The events Validating and Validated are commonly used for this purpose. You can put your code validation in there instead of putting it on the KeyPress.

Raj More
Thanks for helping me.
Hakan
+1  A: 

This is not the proper way to let the user input this specific data. You've got a list of valid entries available from your database. Put them in a ComboBox so the user doesn't have to guess and can't get it wrong.

Hans Passant
But for fast data entry purposes I can't use Combobox. Thanks by the way.
Hakan
I like the idea of the combo box - since they can only choose from a list of items, you should set the property `DropDownStyle = DropDownList`. That way, they can autofill and use the keyboard for it, or they can drop down and choose from the list.
Raj More