Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2005

Tip: Looking for answers? Try searching our database.

RadioButton: the Click event was raised unproperly

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Smalldust - 07 Oct 2005 09:57 GMT
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.
Tim_Mac - 11 Oct 2005 15:58 GMT
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

--------------------------
blog: http://tim.mackey.ie 
Smalldust - 12 Oct 2005 07:17 GMT
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.

> hi,
> the Click event fires for keyboard changes, or programmatic changes as you
[quoted text clipped - 6 lines]
> --------------------------
> blog: http://tim.mackey.ie 
Smalldust - 12 Oct 2005 07:25 GMT
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.

> hi,
> the Click event fires for keyboard changes, or programmatic changes as you
[quoted text clipped - 6 lines]
> --------------------------
> blog: http://tim.mackey.ie 
Tim_Mac - 13 Oct 2005 09:48 GMT
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");
 }
}
}
Smalldust - 14 Oct 2005 13:41 GMT
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)

>>>>> You just need to change the TabIndex of button1 from 1 to 4 in your code you sent to me to reproduce the problem.

> hi,
> i did some more testing and it appears that the Click event is not raised
[quoted text clipped - 120 lines]
>  }
> }
Tim_Mac - 18 Oct 2005 13:08 GMT
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
--------------------------
blog: http://tim.mackey.ie 

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.