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