VB.net Combo Box Validate - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

VB.net Combo Box Validate

BSueBSue Posts: 2Member
I have created a data input form that updates a table. On my input form I have a combo box that updates a field that does not allow nulls. If I don't access the combo box on an add new record my accept button gives me an error that the table field will not accept nulls. When I validate the combo box and try to force a focus, the combobox.focus does not take the curser back to the combo box. Any suggestions?

Comments

  • JamesJames Posts: 217Member
    : I have created a data input form that updates a table. On my input form I have a combo box that updates a field that does not allow nulls. If I don't access the combo box on an add new record my accept button gives me an error that the table field will not accept nulls. When I validate the combo box and try to force a focus, the combobox.focus does not take the curser back to the combo box. Any suggestions?
    :
    Forget the focus event. All you have to do is set the Cancel property of the CancelEventArgs to true and it automatically sets the focus back to the Combo box. If you used to do data checking in the lost focus event or whatever, in .Net you do it in the Validating event of the combo box.
    [code]Private Sub ComboBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating
    Try
    'call a sub here that checks the data and if wrong, have it throw an exception

    Catch ex As Exception
    ' Cancel the event thus setting focus back to the Combobox
    e.Cancel = True
    End Try
    End Sub [/code]

  • BSueBSue Posts: 2Member
    : : I have created a data input form that updates a table. On my input form I have a combo box that updates a field that does not allow nulls. If I don't access the combo box on an add new record my accept button gives me an error that the table field will not accept nulls. When I validate the combo box and try to force a focus, the combobox.focus does not take the curser back to the combo box. Any suggestions?
    : :
    : Forget the focus event. All you have to do is set the Cancel property of the CancelEventArgs to true and it automatically sets the focus back to the Combo box. If you used to do data checking in the lost focus event or whatever, in .Net you do it in the Validating event of the combo box.
    : [code]Private Sub ComboBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating
    : Try
    : 'call a sub here that checks the data and if wrong, have it throw an exception
    :
    : Catch ex As Exception
    : ' Cancel the event thus setting focus back to the Combobox
    : e.Cancel = True
    : End Try
    : End Sub [/code]
    :
    :
    Did you know that NOT making a selection from a combo box will cause it to NOT execute the validation code. Validation only gets executed if you DO make a combo box selection or if you tab through the form where the curser goes to the box at some point. My problem is catching the error when the user DOES NOT click in the combo box and just ignores that field on the form. I've put code in my text box validation for the combo box but still can't get focus back to the combo box.
  • JamesJames Posts: 217Member
    : : : I have created a data input form that updates a table. On my input form I have a combo box that updates a field that does not allow nulls. If I don't access the combo box on an add new record my accept button gives me an error that the table field will not accept nulls. When I validate the combo box and try to force a focus, the combobox.focus does not take the curser back to the combo box. Any suggestions?
    : : :
    : : Forget the focus event. All you have to do is set the Cancel property of the CancelEventArgs to true and it automatically sets the focus back to the Combo box. If you used to do data checking in the lost focus event or whatever, in .Net you do it in the Validating event of the combo box.
    : : [code]Private Sub ComboBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating
    : : Try
    : : 'call a sub here that checks the data and if wrong, have it throw an exception
    : :
    : : Catch ex As Exception
    : : ' Cancel the event thus setting focus back to the Combobox
    : : e.Cancel = True
    : : End Try
    : : End Sub [/code]
    : :
    : :
    : Did you know that NOT making a selection from a combo box will cause it to NOT execute the validation code. Validation only gets executed if you DO make a combo box selection or if you tab through the form where the curser goes to the box at some point. My problem is catching the error when the user DOES NOT click in the combo box and just ignores that field on the form. I've put code in my text box validation for the combo box but still can't get focus back to the combo box.
    :
    Maybe you could call the validate event of the combo box in the accept button click event, or whatever event it is that begins the process of calling the database, etc. I do know that if you can get to that event somehow, you can set the cancel argument of the eventargs to true and that will bring focus to the combobox.

    Good luck and let me know how you work it out because I'm just now learning how to do this .Net stuff myself.
    James
Sign In or Register to comment.