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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How do you deal with checkboxes in C#???

How do you deal with checkboxes in C#???

Scheduled Pinned Locked Moved C#
csharpdata-structureshelpquestion
5 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.
  • A Offline
    A Offline
    adonisv
    wrote on last edited by
    #1

    I'm just trying to toggly between two check boxes a draw x values and a draw y values. I found this code but I'm getting a stack overflow error when I click on the check box....:(( private void XvaluescheckBox_CheckedChanged(object sender, System.EventArgs e) { // Toggle boolean switch(this.XvaluescheckBox.CheckState) { case CheckState.Checked: // Code for checked state. this.m_bDrawXValues = false; this.XvaluescheckBox.CheckState = CheckState.Unchecked; break; case CheckState.Unchecked: // Code for unchecked state. this.m_bDrawXValues = true; this.XvaluescheckBox.CheckState = CheckState.Checked; break; case CheckState.Indeterminate: // Code for indeterminate state. break; } } private void YvaluescheckBox_CheckedChanged(object sender, System.EventArgs e) { // Toggle boolean switch(this.YvaluescheckBox.CheckState) { case CheckState.Checked: // Code for checked state. this.m_bDrawXValues = true; this.YvaluescheckBox.CheckState = CheckState.Unchecked; break; case CheckState.Unchecked: // Code for unchecked state. this.m_bDrawXValues = false; this.YvaluescheckBox.CheckState = CheckState.Checked; break; case CheckState.Indeterminate: // Code for indeterminate state. break; } } :doh:

    J D N 3 Replies Last reply
    0
    • A adonisv

      I'm just trying to toggly between two check boxes a draw x values and a draw y values. I found this code but I'm getting a stack overflow error when I click on the check box....:(( private void XvaluescheckBox_CheckedChanged(object sender, System.EventArgs e) { // Toggle boolean switch(this.XvaluescheckBox.CheckState) { case CheckState.Checked: // Code for checked state. this.m_bDrawXValues = false; this.XvaluescheckBox.CheckState = CheckState.Unchecked; break; case CheckState.Unchecked: // Code for unchecked state. this.m_bDrawXValues = true; this.XvaluescheckBox.CheckState = CheckState.Checked; break; case CheckState.Indeterminate: // Code for indeterminate state. break; } } private void YvaluescheckBox_CheckedChanged(object sender, System.EventArgs e) { // Toggle boolean switch(this.YvaluescheckBox.CheckState) { case CheckState.Checked: // Code for checked state. this.m_bDrawXValues = true; this.YvaluescheckBox.CheckState = CheckState.Unchecked; break; case CheckState.Unchecked: // Code for unchecked state. this.m_bDrawXValues = false; this.YvaluescheckBox.CheckState = CheckState.Checked; break; case CheckState.Indeterminate: // Code for indeterminate state. break; } } :doh:

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      You're changin the check state of the checkbox inside the CheckChanged event handler -- i.e. as soon as your check state has changed, your function gets called. Your function then changes the check state, once again calling your function, which goes forever in a loop until the stack overflows. If you need to change the check state of the checkbox inside the check change function, unhook the event handler first. Try something like

      private void YvaluescheckBox_CheckedChanged(object sender, System.EventArgs e)
      {
      // Toggle boolean
      switch(this.YvaluescheckBox.CheckState)
      {
      case CheckState.Checked:
      // Code for checked state.
      this.m_bDrawXValues = true;

      // temporarily unhook the event handler for a moment
      this.YvaluescheckBox.CheckChanged -= new EventHandler(YvaluescheckBox_CheckedChanged);

      // set the check state
      this.YvaluescheckBox.CheckState = CheckState.Unchecked;

      // re-setup the event handler
      this.YvaluescheckBox.CheckChanged += new EventHandler(YvaluescheckBox_CheckedChanged);
      break;

      Or, just don't set the check state of the check box inside your check state change event handler like you're doing there. --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu

      1 Reply Last reply
      0
      • A adonisv

        I'm just trying to toggly between two check boxes a draw x values and a draw y values. I found this code but I'm getting a stack overflow error when I click on the check box....:(( private void XvaluescheckBox_CheckedChanged(object sender, System.EventArgs e) { // Toggle boolean switch(this.XvaluescheckBox.CheckState) { case CheckState.Checked: // Code for checked state. this.m_bDrawXValues = false; this.XvaluescheckBox.CheckState = CheckState.Unchecked; break; case CheckState.Unchecked: // Code for unchecked state. this.m_bDrawXValues = true; this.XvaluescheckBox.CheckState = CheckState.Checked; break; case CheckState.Indeterminate: // Code for indeterminate state. break; } } private void YvaluescheckBox_CheckedChanged(object sender, System.EventArgs e) { // Toggle boolean switch(this.YvaluescheckBox.CheckState) { case CheckState.Checked: // Code for checked state. this.m_bDrawXValues = true; this.YvaluescheckBox.CheckState = CheckState.Unchecked; break; case CheckState.Unchecked: // Code for unchecked state. this.m_bDrawXValues = false; this.YvaluescheckBox.CheckState = CheckState.Checked; break; case CheckState.Indeterminate: // Code for indeterminate state. break; } } :doh:

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        If you have two (or more) checked buttons and only one of them should be set at a time, then you should use option buttons. I see dumb people

        A 1 Reply Last reply
        0
        • A adonisv

          I'm just trying to toggly between two check boxes a draw x values and a draw y values. I found this code but I'm getting a stack overflow error when I click on the check box....:(( private void XvaluescheckBox_CheckedChanged(object sender, System.EventArgs e) { // Toggle boolean switch(this.XvaluescheckBox.CheckState) { case CheckState.Checked: // Code for checked state. this.m_bDrawXValues = false; this.XvaluescheckBox.CheckState = CheckState.Unchecked; break; case CheckState.Unchecked: // Code for unchecked state. this.m_bDrawXValues = true; this.XvaluescheckBox.CheckState = CheckState.Checked; break; case CheckState.Indeterminate: // Code for indeterminate state. break; } } private void YvaluescheckBox_CheckedChanged(object sender, System.EventArgs e) { // Toggle boolean switch(this.YvaluescheckBox.CheckState) { case CheckState.Checked: // Code for checked state. this.m_bDrawXValues = true; this.YvaluescheckBox.CheckState = CheckState.Unchecked; break; case CheckState.Unchecked: // Code for unchecked state. this.m_bDrawXValues = false; this.YvaluescheckBox.CheckState = CheckState.Checked; break; case CheckState.Indeterminate: // Code for indeterminate state. break; } } :doh:

          N Offline
          N Offline
          Nnamdi Onyeyiri
          wrote on last edited by
          #4

          Isnt CheckedChanged called after the checkboxes state is changed? Basically, your recieving the event, and changing a property of the checkbox, that causes the event to be fired again, so its a vicious circle. Instead, try:

          private void XvaluescheckBox_CheckedChanged(object sender, System.EventArgs e)
          {
          this.m_bDrawXValues = this.XvaluescheckBox.Checked;
          }

          private void YvaluescheckBox_CheckedChanged(object sender, System.EventArgs e)
          {
          this.m_bDrawYValues = this.YvaluescheckBox.Checked;
          }

          I havent tested it, but that should work. Strictly speaking, you dont even need the m_bDrawXValues and m_bDrawYValues variables, you can use the Checked property of the checkboxes.


          To those who didn't make it, we will remember you. To those who did :bob: is back. - Megan Forbes in Black Friday
          Another Post by NnamdiOnyeyiri

          1 Reply Last reply
          0
          • D Daniel Turini

            If you have two (or more) checked buttons and only one of them should be set at a time, then you should use option buttons. I see dumb people

            A Offline
            A Offline
            adonisv
            wrote on last edited by
            #5

            So do I, all the time! :laugh: I used the radio buttons, those work nicely..Doh! :sigh:

            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