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. In a forms app, how to get a reference to object that has focus?

In a forms app, how to get a reference to object that has focus?

Scheduled Pinned Locked Moved C#
helptutorialquestion
3 Posts 2 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.
  • S Offline
    S Offline
    sherifffruitfly
    wrote on last edited by
    #1

    I have no idea. Basically, I want to do the following: 1) get reference to whatever object has focus. 2) give another control focus. 3) do some stuff with control from 2) 4) give focus back to the initial object. I don't know how to do 1) and 4), but I'm sure I could figure out 4), given 1) (but not vice-versa). So - can anyone help me with 1)? Thanks! cdj

    D 1 Reply Last reply
    0
    • S sherifffruitfly

      I have no idea. Basically, I want to do the following: 1) get reference to whatever object has focus. 2) give another control focus. 3) do some stuff with control from 2) 4) give focus back to the initial object. I don't know how to do 1) and 4), but I'm sure I could figure out 4), given 1) (but not vice-versa). So - can anyone help me with 1)? Thanks! cdj

      D Offline
      D Offline
      dsl fahk
      wrote on last edited by
      #2

      okay. The first one is probably going to be the hardest one to answer. I don't think the form has any methods or properties that tell you which has focus, but you could do a foreach statement which would look something like public Control FocusedControl() { foreach (Control Ctrl in this.Controls) { if (Ctrl.Focused == true) return Ctrl; else return FocusedControl; } } } I am not sure if it will let you return the focused control, in which case you could use a different iteration public Control FocusedControl() { Control FocusedCtrl = null; Int FocusIndex = 0; while (FocusedCtrl == null && FocusIndex < this.Controls.Count) { if (this.Controls[FocusIndex].Focused == true) { FocusedCtrl = this.Controls[FocusIndex]; } } return FocusedCtrl; I think either of those will work. Then to give focus to an object all you need to know is which object you want to give focus to. MyControl.Focus(); for number 3 I think you mean without knowing the name of it, which you can store in a variable of type Control. To access properties that most controls don't have, such as the url of a webBrowser, you can use ((WebBrowser)this.Controls[FocusIndex]).url = "http://www.codeproject.com"; and finally for number 4 you know how to "store" a Control in the Control variable, and all you have to do is go back to what you named the variable, and use the Focus() method. Hope this helps. :)

      S 1 Reply Last reply
      0
      • D dsl fahk

        okay. The first one is probably going to be the hardest one to answer. I don't think the form has any methods or properties that tell you which has focus, but you could do a foreach statement which would look something like public Control FocusedControl() { foreach (Control Ctrl in this.Controls) { if (Ctrl.Focused == true) return Ctrl; else return FocusedControl; } } } I am not sure if it will let you return the focused control, in which case you could use a different iteration public Control FocusedControl() { Control FocusedCtrl = null; Int FocusIndex = 0; while (FocusedCtrl == null && FocusIndex < this.Controls.Count) { if (this.Controls[FocusIndex].Focused == true) { FocusedCtrl = this.Controls[FocusIndex]; } } return FocusedCtrl; I think either of those will work. Then to give focus to an object all you need to know is which object you want to give focus to. MyControl.Focus(); for number 3 I think you mean without knowing the name of it, which you can store in a variable of type Control. To access properties that most controls don't have, such as the url of a webBrowser, you can use ((WebBrowser)this.Controls[FocusIndex]).url = "http://www.codeproject.com"; and finally for number 4 you know how to "store" a Control in the Control variable, and all you have to do is go back to what you named the variable, and use the Focus() method. Hope this helps. :)

        S Offline
        S Offline
        sherifffruitfly
        wrote on last edited by
        #3

        For 3), I do know the name of it - there's no problem there. I included the whole sequence just to give the reader the problem-context. I'd thought of polling every object with a focus property - if I can't come up with anything else, I'll do it. Obviously it's hugely inefficient. Thanks for your thoughts! cdj

        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