Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Stupid CheckBox Behavior, please help!!

Stupid CheckBox Behavior, please help!!

Scheduled Pinned Locked Moved .NET (Core and Framework)
helptutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Miguel Lopes
    wrote on last edited by
    #1

    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!

    G D P 3 Replies Last reply
    0
    • M Miguel Lopes

      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!

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      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; } }

      G 1 Reply Last reply
      0
      • G George L Jackson

        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; } }

        G Offline
        G Offline
        George L Jackson
        wrote on last edited by
        #3

        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 //

        1 Reply Last reply
        0
        • M Miguel Lopes

          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!

          D Offline
          D Offline
          digiwombat
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • D digiwombat

            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

            M Offline
            M Offline
            Miguel Lopes
            wrote on last edited by
            #5

            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. :)

            G 1 Reply Last reply
            0
            • M Miguel Lopes

              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. :)

              G Offline
              G Offline
              George L Jackson
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • M Miguel Lopes

                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!

                P Offline
                P Offline
                peryMimon
                wrote on last edited by
                #7

                show about "AddMessageFilter" of Aplictaion class maybe is parallel function in Control class

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups