Bindingnavigator validating event
I have a form with a Data Grid View and a Button, both having Causes Validation = true. Roger Problem with the messagebox showing and the second validate event not fireing... If you want a real brain-teaser involving this I can post an example in which the validating event (and errornous handling of it) will cause two controls to have focus on the same time ( -- if you ever encounter this in a project, your eyes will just pop out your head, untill you come to understand the prob -- ) In fact what is happening with the textboxes can rather easily be displayed if you use the System. Perform this with the messagebox displaying and without (put a checkbox on your form to disable displaying your messagebox, and compare the results) If you monitor this you will see that the focus shifts away before the leave of the old field takes place (you're still in the validating event so the leave can't take place). Active Ce) End Sub Private Sub Button1_Lost Focus(By Val sender As Object, By Val e As System. Thirdly, in your description you say "the messagebox closes, the form gets focus again and the button gets focus a second time. But in my test with the message box, the point is that the click event of the button DOESN'T fire. But I can use the debug output I posted earlier to explain the idea. Click Proxy Click() End Sub Private Sub Proxy Click() Msg Box("Button click") End Sub If the Row Validating Event is called from within the datagridview, or by movement out of the datagridview to some other control, the final test proves False, so the Proxy Click sub is not called.In thr grid's Row Validating and the button's click event, I am showing a Message Box to notify that the event has fired. Row Index) End Sub End Class Here's some debug output from me single-clicking in various cells. the button gets focus, the validating fires, the focus from the button moves to the messagebox (without the button loosing focus), the messagebox closes, the form gets focus again and the button gets focus a second time. As the question says "I have to click the button for a 2nd time for the Button's click event to fire." So it's not, on my analysis, that "Tthe lostfocus which has happened when the button click did it's work has cleaned up remaining focustrash and everything is back to normal": it is that any cleaning up happens BEFORE the button click does its work. You will note from that that, at the end of the sequence of focus transfers that showing the Row Validation Message Box involved, Button1 remained the Active Control even though its Click never functioned. Name = "Button1" Then Proxy Click() End If End Sub Private Sub Button1_Click(By Val sender As System. But if the Row Validating Event is called by a Click (which fails) on the button, the final test produces True, so that Proxy Click sub is called by the Row Validating code.So I tried replicating this problem by putting a separate button on the form. One form, a datagridview - called dgva - and this code. Roger Hi, Sorry for some incorrect details in the explanation I wrote above.Public Class Form3 Private dt As Data Table Private Sub maketable() 'This code is equivalent to loading a table with a dataadapter dt = New Data Table("Table A") Dim dc0 As New Data Column("ID", Get Type(Integer)) dt. Add(dc0) Dim dc1 As New Data Column("Item", Get Type(String)) dt. Add(dc1) Dim dc2 As New Data Column("Time", Get Type(Integer)) dt. Add(dc2) For i As Integer = 0 To 9 Dim dr A As Data Row = dt. The thing is that the Lost Focus and Got Focus always works correct because it's trigger by the OS, it's the Enter and Leave (who surround the validation events) that get mixed in the process.One of a set of tools we're offering as a way of saying thank you for being a part of the community. NET rather than C#, but I don't think translation should be difficult. Data Source = dt Dim bcol As New Data Grid View Button Column dgva. Add(bcol) End Sub Private Sub dgva_Cell Click(By Val sender As Object, By Val e As System. But that is even more "only an idea": brain-storming, really.The actual problem I am trying to debug is that when the user clicks on any Binding Navigator button, the button's click event fires BEFORE the Row Validating event ! And it's a form I had hanging around for another purpose - which explains the data - but I've just added a button column and some debug code to it for the purposes of this demo. Add(dr A) Next End Sub Private Sub Form3_Load(By Val sender As Object, By Val e As System. I haven't tested and, even if the idea was in essence workable, the specifics would depend on what purpose the button was supposed to serve.When the user clicks the Binding Navigator's Add or Delete button, I want to be able to cancel the row addition or deletion depending on some logic. When the user adds a row by clicking on the Add button of the Binding Navigator, and then without typing anything in the new row, clicks the Add button again, then the Row Validating event of the previous added row does not fire.But the Binding Navigator button's event handlers dont have a Cancel property to cancel the event. Or can this be done by trapping some other event handler ? How can I ensure that the Row Validating event fires for the previously added row?
One thing to take care of here is: if validation fails, then you need to notify binding Navigator Add New Item_Click to not call Add New method.
You can do this in two ways: one is to have some class variable (like _Validation Success) that will be set to False if validating fails.
Then you can check this variable's value in binding Navigator Add New Item 1.
Sequence of Events: (Here grid Districts is the Data Grid View, dt Districts is the Data Table) 1.
binding Navigator Add New Item_Click event fires before the Row Validating event, so by the time the validation routine runs, a new row already gets added to the underlying datatable. Later on when the Row Validating event does fire, grid Districts.