Reputation: 203
I have a form that has multiple UserControls
in it.
When a button inside a user control is pressed, it will open and add another user control contaning a text box and 2 buttons.
As soon as this user contorl is opened, the focus/cursor goes to the text box.
Pressing TAB button takes focus from one control to another, moving all through the form.
So basically I want to avoid this scenario.
i.e, there is a UserControl
object called XXX. Now when I click "Open Edit" button in it, it opens a new UserControl
object called YYY inside XXX.
UserControl
YYY has a text box and 2 (Save & Cancel) buttons.
I want to make sure that tabbing keeps the focus always inside YYY until YYY is closed by clicking the Cancel button.
I am not sure whether there's a form wide TabStop
property? Or should i have to loop through other controls
and set it's TabStop
to FALSE?
Considering that there're many other UserControls under different classes, it's kinda tedious.
Is there a easier way to achieve this?
Upvotes: 0
Views: 1567
Reputation: 125197
Actually your question is:
How to keep tab stop in a user control?
You can override ProcessTabKey
in your user control that you want to keep the tab stop.
protected override bool ProcessTabKey(bool forward)
{
return this.SelectNextControl(this.ActiveControl, true, true, true, true);
}
This way, the tab stop will remain in your user control.
Upvotes: 6
Reputation: 33738
You will need to set the TabStop
property of each control to False
.
You can do this either:
Form_Load
event handler) for specific controls Naïve recursive function:
I write in VB, so you'll need to translate
Public Sub LoopControls() ' Assumes this function lives within the scope of a Form
LoopControls(Me)
End Sub
Public Sub LoopControls(ByVal container As Control)
'
' TODO: Do something with the current control (container)
'
'
' Loop over the current control's inner controls
'
If Not Nothing Is container.Controls
For Each control As Control In container.Controls
LoopControls(control)
Next
End If
End Sub
Upvotes: 2