Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why toolbar buttons doesn't care of CausesValidation

Why toolbar buttons doesn't care of CausesValidation

Scheduled Pinned Locked Moved C#
databasehelpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Itanium
    wrote on last edited by
    #1

    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.

    L 1 Reply Last reply
    0
    • I Itanium

      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.

      L Offline
      L Offline
      Luis Alonso Ramos
      wrote on last edited by
      #2

      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!

      L I 2 Replies Last reply
      0
      • L Luis Alonso Ramos

        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!

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        Oops! sorry about the XML comments. They are the summary, param, and remarks tags. :-O -- LuisR


        Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

        1 Reply Last reply
        0
        • L Luis Alonso Ramos

          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 Offline
          I Offline
          Itanium
          wrote on last edited by
          #4

          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.

          L 1 Reply Last reply
          0
          • I Itanium

            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.

            L Offline
            L Offline
            Luis Alonso Ramos
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups