0
votes

Here's a little background on my app:

  1. I have a main form that contains a vertical SplitContainer.
  2. Inside the SplitContainer's left panel, I have a TreeView control.
  3. 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.
  4. 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
1
Could you show some of the code you are using to set focus on the Datagrid - msarchet
Does the treeview actually have the focus or the item is just highlighed? the treeview has a property to not show as highlighted when it loses focus. See where the actual focus is. Also at what point do you set the Focus to the datagridview? - Pinakin Shah
Pinakin, the treeview has the focus. If I use the up/down arrow keys, the treeview selection changes. - Roger

1 Answers

0
votes

If you are trying to set focus to a a DataGridView make sure that you are specifically setting focus to the View and not the entire DataGrid itself. As long as the controls are visible there shouldn't be a problem in setting focus to them.