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. C#
  4. Get Checked value of a checkBox from thread different of the UI thread

Get Checked value of a checkBox from thread different of the UI thread

Scheduled Pinned Locked Moved C#
questioncsharpwinformsdesign
4 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.
  • Z Offline
    Z Offline
    Zeokat
    wrote on last edited by
    #1

    Well i know that i can´t play directly with values of winforms from different thread of the UI thread. But i have a thread and i need to obtain the value of a checkbox from that thread... how can i obtain the value?? Thanks in advance. :)

    L L H 3 Replies Last reply
    0
    • Z Zeokat

      Well i know that i can´t play directly with values of winforms from different thread of the UI thread. But i have a thread and i need to obtain the value of a checkbox from that thread... how can i obtain the value?? Thanks in advance. :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Create a Class "cls" :

      class MyCls
      {
      public static bool MyCheckBoxValue;
      }

      and change this variable from your forms by call it like this :

      MyCls.MyCheckBoxValue = true;

      or

      bool mybal = MyCls.MyCheckBoxValue;

      I know nothing , I know nothing ...

      1 Reply Last reply
      0
      • Z Zeokat

        Well i know that i can´t play directly with values of winforms from different thread of the UI thread. But i have a thread and i need to obtain the value of a checkbox from that thread... how can i obtain the value?? Thanks in advance. :)

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, for minimal run-time cost, give the Form that holds the CheckBox: 1. a private bool holding the state of the checkbox; 2. a CheckBox_CheckedChanged event handler, updating said variable; 3. a public property to get the value of said variable. this set-up avoids the need for Control.Invoke PS: don't forget to copy the initial CheckBox.Checked value in your form's constructor. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


        1 Reply Last reply
        0
        • Z Zeokat

          Well i know that i can´t play directly with values of winforms from different thread of the UI thread. But i have a thread and i need to obtain the value of a checkbox from that thread... how can i obtain the value?? Thanks in advance. :)

          H Offline
          H Offline
          hwswin
          wrote on last edited by
          #4

          I have an other Thread t,and I can get the checkbox value from UI thread in Thread t. see follow source code.

              private void Form2\_Load(object sender, EventArgs e)
              {
                  Thread t = new Thread(new ThreadStart(deal));
                  t.Name = "deal";
                  t.IsBackground = true;
                  t.Start();
              }
          
              void deal()
              {
                  while (true)
                  {
                      System.Diagnostics.Debug.WriteLine(checkBox1.Checked);
                      Thread.Sleep(1000);
                  }
              }
          
          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