Share via

How to Release focus on specific objects

Mansour_Dalir 2,036 Reputation points
2024-01-27T18:18:02.18+00:00

error2

hi

error2

How to get the focus of the list box when (Cancel Validating TextBox) is equal to (True)

The moment of clicking on the list box, the focus should be specially captured and the text box will remain invalidated . or I want the datagridview cell to still be in EditText mode when I was selecting an item on the ListBox. The ListBox is a child of the panel and the panel is a child of the current form. The ListBox does not belong to another form!

Developer technologies | VB

Answer accepted by question author

Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
2024-01-30T14:55:50.1933333+00:00

Hi @Mansour_Dalir ,

I tested with the code below and when selecting an item from the listbox, the cells of the Datagridview exit edit mode, but do not lose focus.

    Dim dt As New DataTable
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dt.Columns.Add("Column1")
        dt.Columns.Add("Column2")
        dt.Columns.Add("Column3")
        dt.Rows.Add("1", "2", "3")
        dt.Rows.Add("4", "5", "6")
        dt.Rows.Add("7", "8", "9")
        DataGridView1.DataSource = dt

        editingListBox.Visible = False
        Me.Controls.Add(editingListBox)
    End Sub

    Private Sub DataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
        ShowEditingListBox(e.RowIndex, e.ColumnIndex)
    End Sub

    Private Sub ShowEditingListBox(rowIndex As Integer, columnIndex As Integer)
        Dim cellRect As Rectangle = DataGridView1.GetCellDisplayRectangle(columnIndex, rowIndex, False)
        editingListBox.Location = New Point(cellRect.Left, cellRect.Bottom)
        editingListBox.Width = cellRect.Width
        editingListBox.Items.Clear()
        editingListBox.Items.AddRange({"Item 1", "Item 2", "Item 3"})
        editingListBox.Visible = True
        editingListBox.BringToFront()
    End Sub


    Private Sub editingListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles editingListBox.SelectedIndexChanged
        If editingListBox.SelectedItem IsNot Nothing Then
            Console.WriteLine(editingListBox.SelectedItem.ToString())
            Dim editingCell As DataGridViewTextBoxCell = DirectCast(DataGridView1.CurrentCell, DataGridViewTextBoxCell)
            editingCell.Value = editingListBox.SelectedItem.ToString()
            editingListBox.Visible = False
        End If
    End Sub

Best Regards.

Jiachen Li


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.