Can you cancel the click of a checkbox?

G

Guest

I want to be able to intercept the click of a checkbox to cancel a change in
its checked state under certain conditions to prevent the execution of code
attached to the CheckedChanged event. So when someone clicks on the checkbox
I want to do some validation and on failure throw the click away, leaving the
state of the checkbox unchanged.

I was looking for Cancel or Handled to manage it, but I can't find it. I
suppose I could short-circuit the CheckedChanged event to flip the checked
state back and skip the code, using a Boolean flag to skip the second call to
CheckedChanged when I change the state back, but that's ugly.

Any suggestions would be appreciated.
 
G

Guest

This code prevents checkbox2 from recieving the mouse up event -- which also
prevents the checkbox from changing state.

Private Sub CheckBox2_MouseDown(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles CheckBox2.MouseDown

If CheckBox1.Checked = False Then Me.Focus()

End Sub

Hope that helps.
 
G

Guest

That doesn't seem to work. I used the C# code below as a test to
unconditionally prevent a change to the checkbox and the checked state still
toggles and code in its CheckedChanged still fires.

private void chkIncludeInactive_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.Focus();
}
 
G

Guest

I just tried the same code you posted on my machine - and it works.
I will post the entire file contents. If you are in a usercontrol you may
need to set focus on the parent.

Hope this helps.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace cSharpCheckIt
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox2;
/// <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.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(16, 40);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(120, 24);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
//
// checkBox2
//
this.checkBox2.Location = new System.Drawing.Point(16, 80);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(120, 24);
this.checkBox2.TabIndex = 1;
this.checkBox2.Text = "checkBox2";
this.checkBox2.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.checkBox2_MouseDown);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 262);
this.Controls.Add(this.checkBox2);
this.Controls.Add(this.checkBox1);
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 checkBox2_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.Focus();
}
}
}
 
R

Rodger Constandse

Hi,

It looks like the CheckState change is being performed by the OnClick handler of
the CheckBox. If you want, you can create a derived CheckBox class and override
the OnClick method, provide a new cancellable event (something like
CheckStateChanging) that you call from your OnClick method, and only call the
base OnClick method if the event is not cancelled.

Best regards,

Rodger

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDiagramEditor.com>
 
G

Guest

Here is an example of what rodger was pointing out. The default state for
this is disabled and can be changed by setting the CanCheck property.



using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace cSharpCheckIt
{
/// <summary>
/// Summary description for CustCheck.
/// </summary>


public class CustCheck : System.Windows.Forms.CheckBox
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private bool condition;

public CustCheck()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
condition = false;
}

/// <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 Component 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()
{
components = new System.ComponentModel.Container();
}
#endregion

protected override void OnPaint(PaintEventArgs pe)
{
// TODO: Add custom paint code here

// Calling the base class OnPaint
base.OnPaint(pe);
}

protected override void OnClick(EventArgs e)
{
if(condition==true) base.OnClick (e);
}

public bool CanCheck
{
set
{
condition = value;
}
}

}
}
 
R

Richard

Change the AutoCheck Property of the Checkbox False.
Then you can implement your own condition to check/uncheck it.

RC
 
G

Guest

Thanks ... I did not even look for a property that could control this
behavior. I should have started there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top