locked
toolstrip question RRS feed

  • Question

  • I saw a thread regarding the toolstripbutton not causing a validation.  The user asked if there was a way to force it to do so.  The thread remains unanswered so I thought I would revive it. 

    this.validate() does not work.

    I am nervous about using this control considering this problem.  As of now the only control that seems to have a validation problem when used in conjunction with a toolstrip is the DateTimePicker.  Unless a user moves the focus from this control to another (non-toolstrip) control in the form, the datetimepicker control doesn't get validated, and if I have a save button on that toolstrip, the value written to the database is the old value.  This is a very serious problem.  I tested the same scenario with a textbox but that seems to act correctly (writes the changed value to the database).  My primary question: is there something different about the datetime picker that is casuing this, or could this problem arise with any control.  The user in the thread I mentioned above says it was occuring with a textbox, but just the validation event was not being fire.  My impression is that a datetimepicker value, even if you change it, technically does not change until it loses the focus, whereas a textbox value changes immediately.  Can anyone confirm this?

    Tuesday, December 5, 2006 3:35 PM

Answers

  • ok, a simple work around for this issue is to move the focus to another control on the form in the toolstrip button click event prior to saving the data:

    this.somecontrol.focus();

    save data here...

    Tuesday, December 5, 2006 3:48 PM

All replies

  • ok, a simple work around for this issue is to move the focus to another control on the form in the toolstrip button click event prior to saving the data:

    this.somecontrol.focus();

    save data here...

    Tuesday, December 5, 2006 3:48 PM
  • ToolStripItems are not Windows controls.  They cannot receive focus and do not cause the control you want to validate to lose focus and thus trigger the Validating event.  You could use a trick by forcing the focus to change to a button that has a large negative Left property value and is thus invisible to the user.  For example:

        private void textBox1_Validating(object sender, CancelEventArgs e) {
          e.Cancel = textBox1.Text.Length == 0;
        }
        private void toolStripButton1_Click(object sender, EventArgs e) {
          button1.Focus();
          if (button1.Focused) {
            // Do your stuff
            Console.WriteLine("Validate okay");
          }
        }

    Tuesday, December 5, 2006 3:49 PM