How can I keep multiple controls in focus?

I have a tree on the left side. Selecting node displays the relevant information in the form on the right.

Can I save the tree and any control (text box, combo box, check box) on the right in focus at the same time? This will allow the user to select a field, make changes, select another node, and without having to go back and select the same field again, just enter and change the value of the same field.

Thanx.

EDIT
I assume that you can perform this behavior manually:

private Control __cFocus; private void {anyControl}_Focus(object sender, EventArgs e) { __cFocus = (Control)sender; } private void treeView1_AfterSelect(object sender, EventArgs e) { __cFocus.Focus(); } 

I'm just wondering if there is an automatic / more elegant solution

EDIT 2
Good, so it seems to me that I will need to implement it manually. Then a manual implementation. However, another problem now arises; not sure if I should ask this as a separate question.

When you select node, the text field gets the focus as intended, but only when using the keyboard. This does not work when selecting a node with the mouse. At first I thought it might be a mouse event that is interfering, but step by step showed that the MouseUp event fired first, and then an AfterSelect event that sets focus, so I don't think it interferes. The textbox Enter event also fires, but for some reason it loses focus again for the tree.

Thanx

+4
source share
4 answers

no, you cannot keep two controls in focus at the same time. But what you can do is set focus to the target control in the AfterSelect event of the treeview

 private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { textBox1.Focus(); textBox1.SelectAll(); } 

then in your text box exit, save the changes, for example:

 private void textBox1_Leave(object sender, EventArgs e) { //save changes here } 

Thus, every time you select an item in the tree, check the text field for changes and save as necessary, you will reorient the text field for your next edit

+2
source

If you want to focus on it, you can use usercontrol. you can put your text block in usercontrol and set the focus of this text field to usercontrol using the specified properties when choosing a tree.

+1
source

There can only be one element inside!

But I have an idea for you that can solve your problem. Assuming you have a window with a TreeView and a TextBox. Set the HideSelection TreeView property to false and subscribe to the AfterSelect event (for example, Esperanson already answered) as follows:

 private void OnTreeViewAfterSelect(object sender, TreeViewEventArgs e) { textBox1.Text = e.Node.Text; textBox1.Focus(); } 

Then subscribe to the KeyDown event in the TextBox and follow these steps in the event method:

 private void OnTextBoxKeyDown(object sender, KeyEventArgs e) { if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.Down)) { treeView1.Focus(); SendKeys.Send(e.KeyCode == Keys.Up ? "{UP}" : "{DOWN}"); } } 

Finally, sign the Leave event from the TextBox and follow these steps in the event method:

 private void OnTextBoxLeave(object sender, EventArgs e) { if (treeView1.SelectedNode != null) { treeView1.SelectedNode.Text = textBox1.Text; } } 

And, voilรก, it should work as you expected it ...

+1
source

No, you cannot, only one control can be in focus at any given time.

See Moonlight's comment for one way to achieve your desired behavior.

0
source

Source: https://habr.com/ru/post/1382483/


All Articles