Groups | Blog | Home
all groups > dotnet windows forms databinding > february 2006 >

dotnet windows forms databinding : Validate required fields in Windows Forms


Jeronimo Bertran
2/22/2006 10:32:05 PM
I created a form that include a BindingNavigator. My BindingSource has
some required fields. If I use the bindingNavigator AddNewItem the bound
controls are cleared. If I press the AddNewItem again I get an exception
because som required fields are not set.

I tried using the Validating event on the required field bound controls but
since the focus never gets to these controls, the validating procedure is
never called.

How can I make sure that all fields are valid before the bindingNavigator
tries to write to the dataset?

thanks

Mike Dekarske
2/23/2006 3:21:05 PM
[quoted text, click to view]

I had the exact problem, posted a message three days ago and received no
replies. In those three days I came up with something that works.

The only event that I could find that would fire before the row is attached
to the dataset was the MouseDown event on the Binding Navigator buttons.

So it looks something like this:

Private Sub Validate_Detail(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles _
BindingNavigatorAddNewItem.MouseDown, _
BindingNavigatorMoveFirstItem.MouseDown, _
BindingNavigatorMoveLastItem.MouseDown, _
BindingNavigatorMoveNextItem.MouseDown, _
TblTeamsBindingNavigatorSaveItem.MouseDown
Validate_Stuff()
End Sub

Private Sub BindingNavigatorPositionItem_Enter(ByVal sender As Object, ByVal
e As System.EventArgs) Handles _
BindingNavigatorPositionItem.Enter
Validate_Stuff()
End Sub

Private Sub Validate_Stuff()
If Me.TeamIDTextBox.Text = "" Then
My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Hand)
MessageBox.Show("You have to fill in the team id", "Detail Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.TeamIDTextBox.Focus()
End If
End Sub

If you get an error and the MessageBox shows, the click events from the
BindingNavigator buttons do not fire.

Jeronimo Bertran
2/24/2006 9:40:52 AM
Thanks Mike,

I ended up doing something that is also not very pretty. I created my own
entries for every one of the navigator items which first validates the form
and then calls the BindingSource methods.


jhiggins NO[at]SPAM srobo.com
3/27/2006 7:29:56 PM
Thanks for this, as I am in the same boat. I guess Microsoft got the Binding Navigator 90% right, and forgot to add this rather important functionality, or at least the "Best practice" solution is not easily found.
---
AddThis Social Bookmark Button