RadioButton: the Click event was raised unproperly

G

Guest

Control :RadioButton
Description: the RadioButton's Click event was raised even the Checked
property is changed Programmatically in constructor.

Step 1. Create a new C# Windows Form project.
Step 2. Add some radio buttons.
Step 3. Attach a event handler for one of the radio buttons. Write down sth
just like MessageBox.Show("hi") in it.
Step 4. Add 'radioButton3.Checked = true' immediately after the
'InitializeComponent()' in the constructor.

Run the program and you will see the 'Hi' messagebox was showed event you
have never clicked any radio button.

Can anyone help me about this issue?
I just want to detect the event when the user exactly click it.
 
T

Tim_Mac

hi,
the Click event fires for keyboard changes, or programmatic changes as you
have noticed. you can handle MouseUp or MouseDown if you want mouse-only
functions. don't forget to keep your application accessible if at all
possible, not all users will use a mouse as the input device.

tim
 
G

Guest

If the changing of Checked property ALWAYS fire a Click event,
I will consider to use mousedown, mouseup, keydown, keyup, etc to handle my
issue.
But actually it ONLY hapens during the startup, i mean ONLY when you put the
'radiobutton1.Check = true;' in places such as the constructor, the
Form.Onload event handler, the Click event will be fired.
So evidently this will result in an unpredictablity in the program.
 
G

Guest

I am not just interesting in the mouse; i am trying to show a warning message
just when the user select a option(ie, a radiobutton).
And it is unreasonable to show a warning message even the user did nothing.
 
T

Tim_Mac

hi,
i did some more testing and it appears that the Click event is not raised
when you programatically change the 'Checked' property. (in my original
test i thought it did, but i can't reproduce it now). however the
CheckedChanged event is raised, as expected.
for your case, even if you only use CheckedChanged it is still going to fire
in your FormLoad and pop up the warning. to get around this you can either
choose to add the CheckedChanged eventhandler after all your initialisation
code is done, or you can set a boolean variable called 'Loading' to true and
in the CheckedChanged EventHandler, you have something like:
if(Loading) return;

below is minimal form code to demonstrate how Click is not caused
programatically (in this case at least!). if it is behaving differently for
you maybe you could post your code and we can see if there is an explanation
for the unexpected behaviour.
tim

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

namespace Test
{


public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RadioButton radioButton1;
private System.ComponentModel.IContainer components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 96);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Toggle";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// radioButton1
//
this.radioButton1.Location = new System.Drawing.Point(64, 56);
this.radioButton1.Name = "radioButton1";
this.radioButton1.TabIndex = 3;
this.radioButton1.Text = "radioButton1";
this.radioButton1.Click += new
System.EventHandler(this.radioButton1_Click);
this.radioButton1.CheckedChanged += new
System.EventHandler(this.radioButton1_CheckedChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(216, 182);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
this.radioButton1.Checked = true; // raises a 'CheckChanged' event,
doesn't raise a 'click' event
}

private void radioButton1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Click");
}

private void button1_Click(object sender, System.EventArgs e)
{
this.radioButton1.Checked = !this.radioButton1.Checked;
}

private void radioButton1_CheckedChanged(object sender, System.EventArgs
e)
{
MessageBox.Show("CheckChanged");
}
}
}
 
G

Guest

I wrote a program just the same as you in appearence but got a result that
the Click event raised.
By comparing the code i found the reason.
That is the 'TabIndex' property.
Try to make the 'TabIndex' of radiobutton less than the button, and you will
get the reproduction of my problem.
And i found that this problem only occurs under the circumstance below:
1. The radiobutton's TabIndex is the smallest one in the current
container(form, groupbox, panel ,etc)
OR
2.The controls which have a smaller TabIndex than the current radiobutton is
all radiobuttons. (Which is mean, if you have 3 radiobuttons in a container,
and you attached a handler to the 2nd radiobutton's Click event, and set its
Checked property to true in FormLoad, the Click event will be raised even it
is not the smallest one in TabIndex)

Tim_Mac said:
hi,
i did some more testing and it appears that the Click event is not raised
when you programatically change the 'Checked' property. (in my original
test i thought it did, but i can't reproduce it now). however the
CheckedChanged event is raised, as expected.
for your case, even if you only use CheckedChanged it is still going to fire
in your FormLoad and pop up the warning. to get around this you can either
choose to add the CheckedChanged eventhandler after all your initialisation
code is done, or you can set a boolean variable called 'Loading' to true and
in the CheckedChanged EventHandler, you have something like:
if(Loading) return;

below is minimal form code to demonstrate how Click is not caused
programatically (in this case at least!). if it is behaving differently for
you maybe you could post your code and we can see if there is an explanation
for the unexpected behaviour.
tim

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

namespace Test
{


public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RadioButton radioButton1;
private System.ComponentModel.IContainer components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 96);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Toggle";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// radioButton1
//
this.radioButton1.Location = new System.Drawing.Point(64, 56);
this.radioButton1.Name = "radioButton1";
this.radioButton1.TabIndex = 3;
this.radioButton1.Text = "radioButton1";
this.radioButton1.Click += new
System.EventHandler(this.radioButton1_Click);
this.radioButton1.CheckedChanged += new
System.EventHandler(this.radioButton1_CheckedChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(216, 182);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
this.radioButton1.Checked = true; // raises a 'CheckChanged' event,
doesn't raise a 'click' event
}

private void radioButton1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Click");
}

private void button1_Click(object sender, System.EventArgs e)
{
this.radioButton1.Checked = !this.radioButton1.Checked;
}

private void radioButton1_CheckedChanged(object sender, System.EventArgs
e)
{
MessageBox.Show("CheckChanged");
}
}
}
 
T

Tim_Mac

hi,
i confirm this behaviour, and suggest you repost the problem now that you
have pinpointed it exactly.
hopefully they will at least fix it by the time .net 2.0 becomes mainstream.

tim
 

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