JohnJDB

Hiya,

I've got a bound form where I'm trying to validate input before the user moves off the record.

Whats the best way to do this

Looking that the save sub I see it calls me.validate.

Private Sub TblSitesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TblSitesBindingNavigatorSaveItem.Click

Me.Validate()

Me.TblSitesBindingSource.EndEdit()

Me.TblSitesTableAdapter.Update(Me.myDataSetSiteInfo.tblSites)

End Sub

I've added Validation code but it doesnt seem to be called.

Private Sub FldNameTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles FldNameTextBox.Validating

If IsDBNull(Me.FldSiteTextBox.Text) OrElse Me.FldSiteTextBox.Text = "" Then

ErrorProvider1.SetError(FldSiteTextBox, "Please enter a name")

Else

' Clear the error.

ErrorProvider1.SetError(FldSiteTextBox, "")

End If

End Sub

What am I doing wrong!!

Cheers

John



Re: Windows Forms Data Controls and Databinding using me.validate

Luc Morin

Hi,

Me.Validate() will only validate the last control that had focus. If your TextBox is not the curently focused control, then the Validating event will not happen.

The Validating event for your TextBox should be fired when it loses focus. The thing with BindingNavigator buttons is that they are not full fledged controls, so they won't cause another control to lose focus. Thus the need to call Me.Validate() to make sure that the last control will validate.

Hope this helps.

Luc





Re: Windows Forms Data Controls and Databinding using me.validate

JohnJDB

Thanks for your email Luc.

HAs anyone got an idea on how to force the form to validate ALL controls before moving to another record

Many MANY thanks

JOhn





Re: Windows Forms Data Controls and Databinding using me.validate

Luc Morin

John,

You normally wouldn't have to validate ALL controls, as each control will normally validate itself when losing focus (by default). The only problem remaining is when you have "lite controls", such as the BindingNavigator buttons who, as was explained, don't cause "heavy controls" to lose focus, thus not validating.

Unless you have a very specific use case that can't use the standard scenario, I wouldn't worry about it. Maybe you could explain your situation in more details

Luc





Re: Windows Forms Data Controls and Databinding using me.validate

JohnJDB

Hi Luc,

Thanks for your help.

Well if you can imagine a databound form with 10 fields. The Database will not allow a null value for the Name and Phone number field needs to be correctly formatted but the others can have pretty much anything.

When someone creates a new record, but doesnt fill in the name field before saving the record, the event fails with a "database doesnt accept nulls" sort of error.

I want to check the hame field has something in it and the phone field has the right entry, showing a response to the user using the ErrorResponse (cant remember it's proper name) control, when the user navigates away from the current form.

Many thanks

John