Jump to content
Xtreme .Net Talk

anyoneis

Members
  • Posts

    4
  • Joined

  • Last visited

Everything posted by anyoneis

  1. Here is a more complete treatment of this issue from another newsgroup: Amazing. This must mean that the Form.Close() function is causing the validating textbox to regain focus. And, contrary to what one might assume from reading the docs, the CausesValidation property CAN effectively be applied to the control in which it is defined, and not just some other (second) control which was previously in focus. For what it's worth, you can use Validation and provide a clean form exit by 1) Add a Cancel button to your form 2) Set the Cancel button's CausesValidation property to false 3) Add a click event handler for the button, and in this handler set all controls' CausesValidation properties to false. Then call this.Close(). As in: ------------ private void CancelButton_Click(object sender, System.EventArgs e) { minValueTextBox.CausesValidation = false; maxValueTextBox.CausesValidation = false; epMinTextBox.CausesValidation = false; epMaxTextBox.CausesValidation = false; this.Close(); } ------------ If you want the Close box on the title bar to be functional when there is a validation error, then you need to: 1) Add a Form Closing event handler, and in this handler set all controls' CausesValidation properties to false. Then set e.Cancel = false and you are done. As in: ----------- private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { minValueTextBox.CausesValidation = false; maxValueTextBox.CausesValidation = false; epMinTextBox.CausesValidation = false; epMaxTextBox.CausesValidation = false; e.Cancel = false; } ----------- If someone hits your Cancel button,you get a totally clean exit from the form. However, if the Close box on the title bar is pressed, they get a validation error THEN the form is closed. Getting better than this will require a fix from MS or using Clay's suggestion to set all controls' CausesValidation properties to false when the WM_CLOSE message is detected. Thanks Clay! David "ClayB [syncfusion]" <clayb@syncfusion.com> wrote in message news:%23R1gNFoLDHA.3392@tk2msftngp13.phx.gbl... > One way you can do this is to override your form's WndProc method and catch > the click on the System Menu's Close button, and then set the TextBox's > CausesValidation property false. > > public const int SC_CLOSE = 0xF060; > public const int WM_SYSCOMMAND = 0x0112; > protected override void WndProc(ref System.Windows.Forms.Message m) > { > if(m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE) > { > this.textBox1.CausesValidation = false; > } > > base.WndProc(ref m); > } > > > ================== > Clay Burch, .NET MVP > > Visit http://www.syncfusion.com for the coolest tools > >
  2. Are you suggesting that I skip the built in Validating event and roll my own scheme? That is certainly doable, however the problem I am trying to solve is how to use the Validating event and the CausesValidation property as they were designed, BUT also to allow an operational Close box. I think the ultimate right solution is for Microsoft to use the Form's CauseValidation property to handle the case of form level controls such as the close box getting focus. Then, we would have a way to get out of this mess cleanly. David
  3. I'm still trying to figure this one out. Two options so far: 1) Add a control that has the CausesValidation property set to false (Like a Cancel button). Close the form in that click event. Then do #2 below also. or 2) Add a Closing event handler to the form, and set e.Cancel = false in the handler. Note, however, that the error message still displays before the form closes with this technique. Just in case, I want to remind you that the "CausesValidation" property refers to the NEXT cotrol to get focus, and NOT to the control that is having the validating event. Cheers! David
  4. For number 3, I have had luck with closing the design view of the affected form, then reopening it. David
×
×
  • Create New...