Here's a little background on my app:
- I have a main form that contains a vertical SplitContainer.
- Inside the SplitContainer's left panel, I have a TreeView control.
- Inside the SplitContainer's right panel, I have another SplitContainer (this one horizontal). This container is fixed, with headings above the split and a Panel control below it.
- I dynamically load my own user controls into the Panel control whenever the tree view changes.
So far so good. Here's my problem, though.
Most of the user controls that are dynamically loaded have a DataGridView that should receive focus when the control is initialized. Sometimes this grid is on the UserControl surface directly; sometimes it's in a GroupBox; etc.
What is the best way to programmatically set focus on the DataGridView control, given that it may placed within a variable number of control containers? I've tried the obvious - .Select, .Focus, .ActiveControl, etc. None of these seem to work - the TreeView item that I selected is still highlighted, and even though a record is selected in the DataGridView, it doesn't have focus.
Any ideas would be appreciated.
EDIT: Code below
Here's the code where I add the user control within the main form:
Private Sub LoadRightPanel(ByVal section As String, ByVal moduleName As String)
Heading.Text = String.Empty
SuspendLayout()
RightPanel.Controls.Clear()
Select Case section.Trim.ToLower
Case "effective date types"
Heading.Text = "Effective Date Types"
RightPanel.Controls.Add(New EffectiveDateTypeAddEditControl(moduleName))
.
.
.
End Select
With RightPanel.Controls(0)
.Dock = DockStyle.Fill
End With
ResumeLayout()
End Sub
And this is where I attempt to set focus within the user control constructor:
Public Sub New(ByVal programModule As String)
InitializeComponent()
currentModule = programModule
UpdateGrid()
UpdateButtonSettings()
ValueGrid.Select()
End Sub