Stupid CheckBox Behavior, please help!!
-
ok, im stuck in a very stupid situation. I have a checkbox, i change it's value through user input AND through runtime code. Problem: When i change the value by code, it fires the checkedchanged event, which, in my opinion, is pretty damn stupid. I know it works for most people, and it may look normal to most people, but for me is very stupid. Example: u have a checkbox that, when pressed, if a certain condition is met, calls a function and changes another checkbox state (or even himself), WITHOUT triggering the changedevent. Y? Cause i want to separate user input from runtime code changes and this stupid control (and others,from what i've seen) mix it all up! Do you know any property to avoid this or another event, to avoid this stupid behavior? Thanks in Advance!
-
ok, im stuck in a very stupid situation. I have a checkbox, i change it's value through user input AND through runtime code. Problem: When i change the value by code, it fires the checkedchanged event, which, in my opinion, is pretty damn stupid. I know it works for most people, and it may look normal to most people, but for me is very stupid. Example: u have a checkbox that, when pressed, if a certain condition is met, calls a function and changes another checkbox state (or even himself), WITHOUT triggering the changedevent. Y? Cause i want to separate user input from runtime code changes and this stupid control (and others,from what i've seen) mix it all up! Do you know any property to avoid this or another event, to avoid this stupid behavior? Thanks in Advance!
I checked in both .Net Framework 1.1 and 2.0, and the programmically changing the checkbox value does not fire the CheckedChanged event. The following example demonstrates this. If you toggle the checkbox, the textbox goes from String.Empty to 0 and, subsequently each time the checkbox gets toggled, the number in the textbox gets incremented. However, when you click on the button, the checkbox gets toggled but the textbox does not display the above described behavior. Framework 2.0 Default.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> Untitled Page
Default.aspx.cs: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { int value = 0; if (Int32.TryParse(TextBox1.Text, out value)) { TextBox1.Text = (++value).ToString(); } else { TextBox1.Text = value.ToString(); } } protected void Button1_Click(object sender, EventArgs e) { CheckBox1.Checked = (CheckBox1.Checked) ? false : true; } }
-
I checked in both .Net Framework 1.1 and 2.0, and the programmically changing the checkbox value does not fire the CheckedChanged event. The following example demonstrates this. If you toggle the checkbox, the textbox goes from String.Empty to 0 and, subsequently each time the checkbox gets toggled, the number in the textbox gets incremented. However, when you click on the button, the checkbox gets toggled but the textbox does not display the above described behavior. Framework 2.0 Default.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> Untitled Page
Default.aspx.cs: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { int value = 0; if (Int32.TryParse(TextBox1.Text, out value)) { TextBox1.Text = (++value).ToString(); } else { TextBox1.Text = value.ToString(); } } protected void Button1_Click(object sender, EventArgs e) { CheckBox1.Checked = (CheckBox1.Checked) ? false : true; } }
Same for Windows Forms: Windows Form Designer: namespace CheckWin { partial class Form1 { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.checkBox1 = new System.Windows.Forms.CheckBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(12, 23); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 20); this.textBox1.TabIndex = 0; // // checkBox1 // this.checkBox1.AutoSize = true; this.checkBox1.Location = new System.Drawing.Point(143, 25); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(80, 17); this.checkBox1.TabIndex = 1; this.checkBox1.Text = "checkBox1"; this.checkBox1.UseVisualStyleBackColor = true; this.checkBox1.Click += new System.EventHandler(this.checkBox1_Click); // // button1 // this.button1.Location = new System.Drawing.Point(22, 81); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 2; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 //
-
ok, im stuck in a very stupid situation. I have a checkbox, i change it's value through user input AND through runtime code. Problem: When i change the value by code, it fires the checkedchanged event, which, in my opinion, is pretty damn stupid. I know it works for most people, and it may look normal to most people, but for me is very stupid. Example: u have a checkbox that, when pressed, if a certain condition is met, calls a function and changes another checkbox state (or even himself), WITHOUT triggering the changedevent. Y? Cause i want to separate user input from runtime code changes and this stupid control (and others,from what i've seen) mix it all up! Do you know any property to avoid this or another event, to avoid this stupid behavior? Thanks in Advance!
George's replies line up with what I've found, but if it's still messing up for you, you could always set a variable with the click function and with the code, and check that variable on check changed before you run anything else. if(clicked == false) //click toggled else //code toggled
-
George's replies line up with what I've found, but if it's still messing up for you, you could always set a variable with the click function and with the code, and check that variable on check changed before you run anything else. if(clicked == false) //click toggled else //code toggled
Meh! Sorry for the inconveniance. I was handling the CheckedChanged Event, not the click event. It's my 1st .Net project, usually i do c++ stuff, and as i am still a bit suspicious about .net, i thought it was another weird VB-like think they had.:-> Thank you all for the quick response. :)
-
Meh! Sorry for the inconveniance. I was handling the CheckedChanged Event, not the click event. It's my 1st .Net project, usually i do c++ stuff, and as i am still a bit suspicious about .net, i thought it was another weird VB-like think they had.:-> Thank you all for the quick response. :)
What he is referring to is a boolean flag variable: Pseudo Code: class { protected: void CheckedChanged() { if (flag) { //Process whatever } } void RunTimeCode() { flag = false; // Change Checkbox via code flag = true; } private: bool flag = true; } -- modified at 20:55 Friday 17th February, 2006
-
ok, im stuck in a very stupid situation. I have a checkbox, i change it's value through user input AND through runtime code. Problem: When i change the value by code, it fires the checkedchanged event, which, in my opinion, is pretty damn stupid. I know it works for most people, and it may look normal to most people, but for me is very stupid. Example: u have a checkbox that, when pressed, if a certain condition is met, calls a function and changes another checkbox state (or even himself), WITHOUT triggering the changedevent. Y? Cause i want to separate user input from runtime code changes and this stupid control (and others,from what i've seen) mix it all up! Do you know any property to avoid this or another event, to avoid this stupid behavior? Thanks in Advance!