Gregory Williams
Gregory Williams

Reputation: 421

Winforms Prevent DataGridView from losing focus

I am writting code in winforms using vb.net and I am trying to prevent a datagridview from losing focus when certain user business rule error conditions exist. When the errors exist I have code in the leave event which sets the focus back to itself. However this does not seem to work completely because the buttons on the form can still be clicked on. Does anyone know how to keep the focus on the datagridview and not allow the buttons to be clicked on without disabling the button when the user enters the datagridview? I am writting a custom datagridview control and want a general purpose routine for keeping the focus on the datagridview control and not allowing the buttons on the form from being able to be clicked on.

Upvotes: 0

Views: 1653

Answers (2)

JDennis
JDennis

Reputation: 682

To prevent the form from changing focus on validation errors, set the form's AutoValidate property to EnablePreventFocusChange. http://msdn.microsoft.com/en-us/library/system.windows.forms.autovalidate(v=vs.100).aspx.

To prevent buttons from submitting when validation errors exist, set the button's CausesValidation property to true. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation(v=vs.110).aspx

Upvotes: 0

tcshao
tcshao

Reputation: 136

There is a certain chain of events that happen during the transition of focus from one control to another. There is a description of that in this msdn link.

I would recommend finding a way to make your business rules fit into that pattern, as this is the purpose for the validation hooks for the controls. Especially if you are creating your own control, also you may be able to encapsulate some of your validation logic using these events.

Upvotes: 0

Related Questions