Why toolbar buttons doesn't care of CausesValidation
-
Hi All, Is it a Bug or what? whenever we write Onvalidating() event of some contorl. It works fine with all controls but toolbar buttons doesn't receive focus so they doesn't trigger the causes validation as well. Kindly tell me soem way so that these buttons also validate the controls. Thanx in advance.:) sorry for my bad English.
-
Hi All, Is it a Bug or what? whenever we write Onvalidating() event of some contorl. It works fine with all controls but toolbar buttons doesn't receive focus so they doesn't trigger the causes validation as well. Kindly tell me soem way so that these buttons also validate the controls. Thanx in advance.:) sorry for my bad English.
I ran into this problem sometime ago. I have several MDI children, each with a toolbar. So I added this routine to the base class, and call it as the first thing on th
ButtonClick
handler for the toolbar./// /// Causes the validation events for the current focused control, without /// it losing it. /// /// Walks recursively through all the form's controls to find /// the one that currently has the focus. protected void ValidateFocusedControl() { foreach(Control ctl in Controls) if(ValidateFocusedControlRecursive(ctl)) break; } /// /// Helper function for ValidateFocusedControl. Walks recursively the /// children of the Control passed as a parameter. /// /// Control to walk its children. /// True if the focused control was found, false to keep /// searching /// If the focused control is found, a new text box is created, /// the focus set to it, and then back to focused control. bool ValidateFocusedControlRecursive(Control ctl) { if(ctl.Focused) { if(ctl.CausesValidation) { // Make current focused control lose focus, and then get // it back, so we can force its validation TextBox txtBox = new TextBox(); txtBox.Bounds = new Rectangle(-100, -100, 1, 1); txtBox.Parent = ctl.Parent; txtBox.Focus(); ctl.Focus(); txtBox.Parent = null; txtBox.Dispose(); } return true; } // Walk children foreach(Control ctl2 in ctl.Controls) if(ValidateFocusedControlRecursive(ctl2)) return true; return false; }
It basically finds which is the focused control, creates a new
TextBox
, gives it focus, and sets focus back to the original control. Good luck! -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
-
I ran into this problem sometime ago. I have several MDI children, each with a toolbar. So I added this routine to the base class, and call it as the first thing on th
ButtonClick
handler for the toolbar./// /// Causes the validation events for the current focused control, without /// it losing it. /// /// Walks recursively through all the form's controls to find /// the one that currently has the focus. protected void ValidateFocusedControl() { foreach(Control ctl in Controls) if(ValidateFocusedControlRecursive(ctl)) break; } /// /// Helper function for ValidateFocusedControl. Walks recursively the /// children of the Control passed as a parameter. /// /// Control to walk its children. /// True if the focused control was found, false to keep /// searching /// If the focused control is found, a new text box is created, /// the focus set to it, and then back to focused control. bool ValidateFocusedControlRecursive(Control ctl) { if(ctl.Focused) { if(ctl.CausesValidation) { // Make current focused control lose focus, and then get // it back, so we can force its validation TextBox txtBox = new TextBox(); txtBox.Bounds = new Rectangle(-100, -100, 1, 1); txtBox.Parent = ctl.Parent; txtBox.Focus(); ctl.Focus(); txtBox.Parent = null; txtBox.Dispose(); } return true; } // Walk children foreach(Control ctl2 in ctl.Controls) if(ValidateFocusedControlRecursive(ctl2)) return true; return false; }
It basically finds which is the focused control, creates a new
TextBox
, gives it focus, and sets focus back to the original control. Good luck! -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
Oops! sorry about the XML comments. They are the
summary
,param
, andremarks
tags. :-O -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
-
I ran into this problem sometime ago. I have several MDI children, each with a toolbar. So I added this routine to the base class, and call it as the first thing on th
ButtonClick
handler for the toolbar./// /// Causes the validation events for the current focused control, without /// it losing it. /// /// Walks recursively through all the form's controls to find /// the one that currently has the focus. protected void ValidateFocusedControl() { foreach(Control ctl in Controls) if(ValidateFocusedControlRecursive(ctl)) break; } /// /// Helper function for ValidateFocusedControl. Walks recursively the /// children of the Control passed as a parameter. /// /// Control to walk its children. /// True if the focused control was found, false to keep /// searching /// If the focused control is found, a new text box is created, /// the focus set to it, and then back to focused control. bool ValidateFocusedControlRecursive(Control ctl) { if(ctl.Focused) { if(ctl.CausesValidation) { // Make current focused control lose focus, and then get // it back, so we can force its validation TextBox txtBox = new TextBox(); txtBox.Bounds = new Rectangle(-100, -100, 1, 1); txtBox.Parent = ctl.Parent; txtBox.Focus(); ctl.Focus(); txtBox.Parent = null; txtBox.Dispose(); } return true; } // Walk children foreach(Control ctl2 in ctl.Controls) if(ValidateFocusedControlRecursive(ctl2)) return true; return false; }
It basically finds which is the focused control, creates a new
TextBox
, gives it focus, and sets focus back to the original control. Good luck! -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
Hi, I got a solution as well which does same...
toolbarButton_Click(...) { this.toolbar1.Focus(); if(!this.toolbar1.Focused()) return; }
isn't it a smaller and better solution? it simply focuses the toolbar and checks that whether it is focuses or not, if it doesnt get focus then it means that focus is still on the control beign validated. Just try it. sorry for my bad English. -
Hi, I got a solution as well which does same...
toolbarButton_Click(...) { this.toolbar1.Focus(); if(!this.toolbar1.Focused()) return; }
isn't it a smaller and better solution? it simply focuses the toolbar and checks that whether it is focuses or not, if it doesnt get focus then it means that focus is still on the control beign validated. Just try it. sorry for my bad English.When you click on a toolbar button, the control that has the focus doesn't lose it, and thus Validating/Validated events are not generated. My solution goes to great length to ensure that the focus is given back to the control that has it, so that it acts the same as you. Your solution simply takes focus away from the currently focused control, and doesn't give it back. I did try something similar before getting to my recursive solution, but I don't remember the details. I took your code and gave it a try, and this is what I came up with:
private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e) { if(!ValidateFocusedControl()) return; if(e.Button == toolBarButton1) MessageBox.Show(this, "Button 1 clicked!"); else if(e.Button == toolBarButton2) MessageBox.Show(this, "Button 2 clicked!"); }
The heart of if is that call to ValidateFocusedControl:
protected bool ValidateFocusedControl() { bool result = true; Control ctl = ActiveControl; toolBar1.Focus(); if(!toolBar1.Focused) result = false; ctl.Focus(); return result; }
I gave it a try and it works as expected, even with controls inside tabpages for example. -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!