Windows Develop Bookmark and Share   
 index > Windows Forms General > UserControl focus question
 

UserControl focus question

Hi,

I am a little bit confused about focus handling in a UserControl.

I have a UserControl with TabStop set to true and a TextBox in a form. Whenever I press the tab-key the focus changes as it should.
But as soon as I add a TextBox inside the UserControl, the UserControl does not get the focus anymore.

What I want:
Focus on single TextBox. Press Tab to set focus to UserControl itself. Press Tab to move to the TextBox which is a child of the UserControl. Press Tab to move to external TextBox.

What I get: Focus in son single TextBox. Pressing Tab moves the focus to the UserControl's child TextBox. Pressing Tab moves the focus to the external/single TextBox again.

Why isn't it possible to set the focus to the UserControl itself, when it has child elements that can receive the focus?


I want to program a UserControl with two elements. One area (I used a Panel as base control for this) where I want to build up the complete desired functionality on my own and one area for standard text input (=standart TextBox). Now I want that the focussteps through every element of my UserControl when the user hits the tab key. I already set the TabStop property of the UserControl and Panel to true. But as mentioned above, as soon as I add the TextBox, the parent UserControl does not receive the focus anymore.

I guess if I would choose TextBox as baseclass instead of Panel everything might work. But I want a baseclass with an absolute minimum of functionality.

Br,
Wannabe

wannabe_2  Monday, September 21, 2009 7:05 PM
I have found a solution:
Just set

SetStyle(ControlStyles::Selectable, true);



for the Panel control to true as well as TabStop = true. Then the Panel can get the focus.

  • Marked As Answer bywannabe_2 Tuesday, September 22, 2009 5:30 PM
  •  
wannabe_2  Tuesday, September 22, 2009 5:25 PM
UserControl contains code that ensures it cannot get the focus back when it loses it, it get immediately passed on to one of its child controls. That's by design, it was made to act as a container. Just like a Form. One thing it cannot do either is to indicate that it has the focus. Explain why you need it to get the focus to get ahead.

Hans Passant.
nobugz  Tuesday, September 22, 2009 2:17 AM
The reason why I want to set the focus to the UserControl itself is, because I found no way to set the focus to a Panel control that is a child of it.
I choose the Panel control because of it's minimal functionality. It shell become a very personal text editor and it has nothing (at least not much) in common with a TextBox.
However I want include TextBoxes and other standard controls in my UserControl, too.

In case that Panel is just a container, too, what control shall I takt as baseclass for a child inside my UserControl. It must be able to get the focus the same way as a TextBox or a CheckBox. The rest of the functionality (keyboard-, mousehandling, painting, memory organization...)I want to do by myself.

Br,
Wannabe
wannabe_2  Tuesday, September 22, 2009 5:32 AM
Wow, that's courageous. TextBox provides a *lot* of functionality. Turning a panel into a focusable control takes surgery. At least this:

using System;
using System.Windows.Forms;

class MyEditor : Panel {
public MyEditor() {
this.SetStyle(ControlStyles.ContainerControl, false);
this.TabStop = true;
}
protected override void OnMouseDown(MouseEventArgs e) {
this.Focus();
base.OnMouseDown(e);
}
}

I don't know what it takes to make it compete in the tab order when you use the keyboard.

Hans Passant.
nobugz  Tuesday, September 22, 2009 12:12 PM
I have found a solution:
Just set

SetStyle(ControlStyles::Selectable, true);



for the Panel control to true as well as TabStop = true. Then the Panel can get the focus.

  • Marked As Answer bywannabe_2 Tuesday, September 22, 2009 5:30 PM
  •  
wannabe_2  Tuesday, September 22, 2009 5:25 PM

You can use google to search for other answers

Custom Search

More Threads

• using system icons
• Treeview AfterLabelEdit Problem
• Problem with selecting items in a ListBox
• using the tag of a control array to index new forms
• How can I customise the default OpenFileDialog appearance.
• Datagridview combobox in VS2005
• no-touch deployment!
• TreeView Checkbox at child only and style
• Copy and Paste or drag and drop Feature????
• C# 2008 Express - Label auto size functionality