Problem with maskedtextbox validation and keypress

Hi,

I have a problem with masked textboxes. I'm really getting annoyed with this one and can't find any more a solution.

I have a form where you enter inputs and there inputs are recorded to the db. I have a maskedtextbox which is optional. But I want that if someone writes something in it, it checks validation and if it's not valid, it's lbl will become red to indicate user to enter a valid data. Maybe I have a logic or syntax error but other maskedtextboxes validatetext method works!! I have used

If cellphone.Text <> Nothing And cellphone.ValidateText = False Then

lbl_cell.ForeColor = Color.Red

Else

lbl_Cell.ForeColor= Color.Black

End If

and

If cellphone.Text <> Nothing Then

If cellphone.ValidateText = False Then

lbl_cell.ForeColor = Color.Red

End If

ElseIf cellphone.Text = Nothing Then

lbl_cell.ForeColor = Color.Black

Else

lbl_cell.ForeColor = Color.Black

End If

But none of them seems to work and it always change the lbl to red.

Also i have a problem with keypress events. It works when i use in textbox_keypress but it doesnt when i use in form1_keypress. I have tried some things i found in msdn and google but it didn't work too. I have tried

Select Case e.KeyChar

Case ChrW(Keys.F1)

MessageBox.Show("OK")

Case ChrW(Keys.F2)

MessageBox.Show("OK")

End Select

and

If e.KeyChar = ChrW(Keys.F1) Then

MessageBox.Show("Aferim")

End If

One more thing. I use tabs. If another textbox is an another tab, when i click for validation button, it shows me that that textbox is empty. But it's not (it's taking data from db). If i click on the tab and open it I don't get anymore validation problems. How can I do to prevent this. Even if I don't click to change the tab.

Thank you for your time...




Answer this question

Problem with maskedtextbox validation and keypress

  • FranciscoJSC

    I'm using tab control container. It has 2 tabs in it, differents textboxes at both side. I use it as a form to enter data to a sqlserver. There are buttons like update and delete and insert. When I click update or insert and if I haven't opened second tab, info in the second tab is not loaded and it updates data as nothing! Is there a way to solve this problem I mean even if I don't click on second tab, can that be loaded and it updates data as textboxes contains

    I try to use datasets but it seems that I messed them up with them.

    code

    sqlcom = "SELECT * FROM Coloumn1 WHERE Name = ' " & textbox1.text & " ' ;"

    Dim adapter As New SqlClient.SqlDataAdapter(sqlcom, dbacces.CariyeConn)
    dim ds as new dataset()
    adapter.Fill(ds,Coloumn1)

    Textbox2.DataBindings.Add("text", ds.Tables(0), "Name")
    Textbox3.Databinding.Add("text", ds.Tables(0), "Age")

    /code

    By this way I can show on textboxes data then i can use insert or update things but when i try to find another row i get exception. about bindings:

    error

    This causes two bindings in the collection to bind to the same property.
    Parameter name: binding

    /error

    To prevent this i have clear all databindings then bind again. It slows down the program when I use this as code:

    code

    if runnedsearch = true then
    ds.Tables.Clear()
    Textbox2.DataBindings.Clear()
    Textbox3.Databinding.Clear()

    /code

    I think the zero in "ds.Tables(0)" indicates the index of the row. So I should change my sqlcom to "SELECT * FROM Coloumn1 ;" from "SELECT * FROM Coloumn1 WHERE Name = ' " & textbox1.text & " ' ;" then i have to write a code to find the index of the data, which I don't know and then I should bind textboxes. But if I do this should I have to clear databindings again Or the parameter about the index saves me from databinding.

    And one more question about dataset, what could happen if it consists of thousands of tables and colounms. Can there be a problem about computer's virtual memory Or speed problem or something like that Any comments or source book or source code or source link is welcome :)

    Thanks



  • Musiconet

    If you have not specified a ValidatingType for the masked text box then the tests you are performing will not work in the way you require.

    User input to a masked text box is restricted by the mask.  Therefore the user cannot enter information which is not permitted by the mask.  However the mask cannot determine whether or not the information entered is valid, e.g. if a date was expected the mask can restrict information such that it must be in the form 11/12/2006 which is valid but it will also accept 99/99/2006 which is clearly not a valid date.

    This is where the validation comes in but to use it you have to tell the text box what sort of data you are expecting by setting the ValidatingType to the type you expect.

    When you do this the ValidateText method then returns an object of this type if the validation succeeds or a Null reference if it fails.

    If you do not set a ValidatingType then it defaults to a null reference.  In this case the second part of your test

    If cellphone.Text <> Nothing And cellphone.ValidateText = False then

    will always be True since the ValidateText method will return a null reference which will be equated to False.

    Strictly speaking this line should read

    If cellphone.Text <> "" And cellphone.ValidateText Is Nothing then

    but it will still not work correctly unless you have specified a ValidatingType.

     

    If you want to capture keypress events in the form_keypress event then you need to set KeyPreview to True in the form properties.  Also be aware that the keypress event is not triggered by all the keys and you may have to use KeyDown or KeyUp.

     

    I cannot replicate your third problem so cannot help there.  Perhaps you could supply a bit more information/code.

     

     

    Dave

     

     

     


  • Lita123

    I'm sorry but I don't understand what you are trying to achieve so I'm afraid I cannot help further.

    Perhaps someone else could assist.

     <edit>

    Just found this in the help file - it may have some relevance to your problem:

    "Controls contained in a TabPage are not created until the tab page is shown, and any data bindings in these controls are not activated until the tab page is shown."

     </edit>


  • Prasanth.P.P

    Now, I get it. but is there a way to pre-load that too. Because it creates error. When i click update if I didn't opened that tab, I lose that record.

  • Problem with maskedtextbox validation and keypress