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. This should be easy

This should be easy

Scheduled Pinned Locked Moved C#
csharpquestion
5 Posts 3 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.
  • N Offline
    N Offline
    Neil Van Eps
    wrote on last edited by
    #1

    Unfortunately it has proven otherwise. I have a simple C# form with a label control(prompt), an OK button, and a Cancel button. When the form loads, the OK button is disabled and the form spawns a worker thread that does some work (runs a pin-pad in this case) and calls a callback function when it is done. In the callback function I re-enable the OK button, and I want to set the focus to the OK button but I can't seem to do it. I used the Focus() method and it fails even though the CanFocus property is true. I also set the ActiveControl property of the form but this failed also. Does anyone have any sage advice for me? Neil Van Eps "Sweet liquor eases the pain" - Lionel Hutz from the Simpsons

    L H 2 Replies Last reply
    0
    • N Neil Van Eps

      Unfortunately it has proven otherwise. I have a simple C# form with a label control(prompt), an OK button, and a Cancel button. When the form loads, the OK button is disabled and the form spawns a worker thread that does some work (runs a pin-pad in this case) and calls a callback function when it is done. In the callback function I re-enable the OK button, and I want to set the focus to the OK button but I can't seem to do it. I used the Focus() method and it fails even though the CanFocus property is true. I also set the ActiveControl property of the form but this failed also. Does anyone have any sage advice for me? Neil Van Eps "Sweet liquor eases the pain" - Lionel Hutz from the Simpsons

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      You will need to Invoke everything coming from the other thread.

      top secret AdvancedTextBox

      1 Reply Last reply
      0
      • N Neil Van Eps

        Unfortunately it has proven otherwise. I have a simple C# form with a label control(prompt), an OK button, and a Cancel button. When the form loads, the OK button is disabled and the form spawns a worker thread that does some work (runs a pin-pad in this case) and calls a callback function when it is done. In the callback function I re-enable the OK button, and I want to set the focus to the OK button but I can't seem to do it. I used the Focus() method and it fails even though the CanFocus property is true. I also set the ActiveControl property of the form but this failed also. Does anyone have any sage advice for me? Neil Van Eps "Sweet liquor eases the pain" - Lionel Hutz from the Simpsons

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        When getting/setting properties or calling methods on a control from another thread, you need to use Control.Invoke. If you're not sure if you need to invoke the method (properties are just getter and setter methods), you can use Control.InvokeRequired. More information and examples can be found in the .NET Framework SDK regarding these members.

        Microsoft MVP, Visual C# My Articles

        N 1 Reply Last reply
        0
        • H Heath Stewart

          When getting/setting properties or calling methods on a control from another thread, you need to use Control.Invoke. If you're not sure if you need to invoke the method (properties are just getter and setter methods), you can use Control.InvokeRequired. More information and examples can be found in the .NET Framework SDK regarding these members.

          Microsoft MVP, Visual C# My Articles

          N Offline
          N Offline
          Neil Van Eps
          wrote on last edited by
          #4

          So just because you declare a callback function doesn't mean it runs in the parent thread. Got it - thanks. Neil Van Eps "Sweet liquor eases the pain" - Lionel Hutz from the Simpsons

          H 1 Reply Last reply
          0
          • N Neil Van Eps

            So just because you declare a callback function doesn't mean it runs in the parent thread. Got it - thanks. Neil Van Eps "Sweet liquor eases the pain" - Lionel Hutz from the Simpsons

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            It runs in the thread context in which it was invoked.

            Microsoft MVP, Visual C# My Articles

            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