Mouse can select other widgets when Validating returns cancel

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an editbox that returns e.Cancel = true in the Validating event (when its value is invalid). The user can click on other widgets (e.g. Button) that have CausesValidation = true. Shouldn't the e.Cancel = true prevent the click event from happening on the other widgets (so user can't leave invalid cell)? Or am I interpreting wrongly. I seem to remember that VB 6.0 would block click events on other widgets until e.Cancel returned false.

Conclusion: How can I block mouse click events on other widgets during the time that another widget is invalid (preferably without writing code for each widget to be aware of the other widgets)?
 
Hi Mark,

Thanks for posting in the community!

I checked the Validating event in a short test program, however it seems
works as you wish on my system, if the e.Cancel = true, you could not
focust other controls, you could click the button button the button_click
event will not fire, event you could not close the form.
have you checked if the CauseValidation on that textBox is true?
You may also add a break point inside Validating event handler to check if
the Validating event handler is executed in your program.

Here is my test code:
<code>
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TextBox_Validating
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 96);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.Validating += new
System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(24, 136);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
//
// button1
//
this.button1.Location = new System.Drawing.Point(176, 184);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (textBox1.Text != "")
e.Cancel = true;
}

private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Button_Click");
}
}
}
</code>

Does it solve your problem?
Please let me know if the problem still persists, and provide more
information on thisi issue, if you could repro this problem in a simple
project, please mail the sample to my e-mail box to let me look at it. If
it could not be reproed, please let me know more about the background of
this issue.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
Back
Top