Hi,

I have one form with multiple controls (e.g. Button,Textbox, RadioButton, ComboBox, etc.). I want to set the focus dynamically on a control using key F4 to highlight that control.
Scenario:
1.Suppose the user clicks on any control
2.Press Key F4 to highlight that control so that the user will know which control was clicked before.

Appreciate your suggestions.
Thank you.

Clicking on a control already sets focus to that control. If you want to do it dynamically you can do something like

TextBox1.Focus()

If you want to attach it to a hotkey then you can set the forms KeyPreview property to True and add

Private Sub Form1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    If e.KeyCode = Keys.F4 Then
        TextBox1.Focus()
    End If
End Sub

If you want to highlight the focused control somehow then you'll have to specify how you want it highlighted. If you want to change the background colour of a textbox (for example) then you could add

Private Sub TextBox1_Enter(sender As System.Object, e As System.EventArgs) Handles TextBox1.Enter
    TextBox1.Tag = TextBox1.BackColor
    TextBox1.BackColor = Color.LightBlue
End Sub

Private Sub TextBox1_Leave(sender As System.Object, e As System.EventArgs) Handles TextBox1.Leave
    TextBox1.BackColor = TextBox1.Tag
End Sub

Edited 3 Years Ago by Reverend Jim

@Reverend Jim,
Your answer is nice...
But, I have a doubt...

Press Key F4 to highlight that control so that the user will know which control was clicked before.

nikki05 asked about how to highlight the previously focused field.
But as per your code,

Private Sub TextBox1_Enter(sender As System.Object, e As System.EventArgs) Handles TextBox1.Enter

You are highlighting the current control.

Am I right in my question?

Please correct me if i am wrong?

I thought the question was phrased awkwardly and I interpreted it in a different way than you did. But if the OP can clarify what types of controls are involved and how the highlighting is to be indicated then I'd be happy to offer a suggestion. For example, if it's only textbox controls then all that has to be done is changing the BG colour. Other controls might require other methods. Better information gets a better answer.

Hi,
Thank for reply.
I want to make my system to be dynamic so that it works well with all the controls. Now I am getting problem to change the color of specific control, e.g. when I click on textbox1 and then press F4, the color of textbox1 changes to LightCyan (Highlight), which is good. Now, when I click on textbox2 and then press F4, the color of textbox2 changes to LightCyan, which is good too. However, I want the the color of textbox1 should not remain LightCyan, it should clear the highlight of textbox1. I would like to change the colour of recently clicked textbox or control only. That is, only one control should be highlighted at one time.
Thank you.

Public Class Form1

Private Property selectedControl As New Control

Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

    If e.KeyCode = Keys.F4 Then
        'Button1.Focus

        If (selectedControl.Focus) Then

            Me.selectedControl.BackColor = Color.LightCyan

        Else
            'Nothing

        End If

    End If
End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    addTextBoxClickHandlers()
End Sub
Private Sub addTextBoxClickHandlers()
    Dim ctl As Control

    For i As Integer = 1 To Controls.Count

        ctl = DirectCast(Me.Controls.Item(Me.Controls.IndexOfKey("TextBox" & i.ToString)), TextBox)
        AddHandler ctl.Click, AddressOf ctl_Click
        AddHandler ctl.GotFocus, AddressOf ctl_GotFocus

        ctl = DirectCast(Me.Controls.Item(Me.Controls.IndexOfKey("Button" & i.ToString)), Button)
        AddHandler ctl.Click, AddressOf ctl_Click
        AddHandler ctl.GotFocus, AddressOf ctl_GotFocus

    Next

End Sub

Private Sub ctl_Click(ByVal sender As Object, ByVal e As System.EventArgs) 

    MsgBox(CType(sender, Control).Text)

End Sub

Private Sub ctl_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) 

    Me.selectedControl = CType(sender, Control)

End Sub

End Class

That's what I did in my example above. When the control gets focus (Enter), the current BG colour is saved in the Tag property and the BG colour is set to a highlight colour. When the control loses focus (Leave) the original BG colour is restored.

when I click on textbox1 and then press F4

I don't understand the purpose of pressing F4. If you click on the textbox it gets focus. Why do you need to press F4? Your original post was confusing. What do you want to happen when F4 is pressed?

By pressing F4, I wanted to highlight that control for further manupulation for my own purpose. I am up to there. Now I just want to remove the highlight of the previously clicked control. I want to use LostFocus event handeler for multiple controls dynamically. Appreciate if someone can help me for this.

You can use AddHandler for that. For an example please see Create controls at runtime with events. Even though you aren't creating the controls at runtime it still shows how to use addhandler. And if you want to add all of the textboxes you can do

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        For Each tbx As TextBox In Me.Controls.OfType(Of TextBox)()
            AddHandler tbx.GotFocus, AddressOf TextBox_GotFocus
            AddHandler tbx.LostFocus, AddressOf TextBox_LostFocus
        Next

    End Sub

    Private Sub TextBox_GotFocus(sender As Object, e As System.EventArgs)

        Dim tbx As TextBox = sender
        tbx.Tag = tbx.BackColor
        tbx.BackColor = Color.LightBlue

    End Sub

    Private Sub TextBox_LostFocus(sender As Object, e As System.EventArgs)

        Dim tbx As TextBox = sender
        tbx.BackColor = tbx.Tag

    End Sub

End Class

And I'm still missing something. If the control already has focus then it is already highlighted in which case you don't need to press F4. You did originally say

when I click on textbox1 and then press F4

Hi

Thank you very much for resolving my problem. Now I am very close to my aim. However, I still have one problem.My actual aim is store the texts of controls (e.g. Label, Button, TextBox, ComboBox, etc.) into a dictionary for further use. Now I am able to store the texts, highlight with F4.

I am able to search the texts of controls using selectedControl_Click event handlers. However, I am getting problem with selectedControl_TextChanged handler.Combobox and ListBox are not working properly with selectedControl_Click event handler so I thought to useselectedControl_TextChanged handler.

Public Class Form1

Private _dic As New SortedDictionary(Of String, String)
Private Property selectedControl As New Control


Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown


   If e.KeyCode = Keys.F4 Then
            selectedControl.Tag = selectedControl.BackColor
            selectedControl.BackColor = Color.LightBlue
        End If

End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Try

        Dim myLabel As Label
        Dim myButton As Button
        Dim mytext As TextBox
        Dim myRadio As RadioButton
        Dim myList As ListBox
        Dim myCombo As ComboBox

        _dic.Clear()
        For Each ctl As Control In Me.Controls

            If TypeOf ctl Is Label Then
                myLabel = CType(ctl, Label)
                _dic.Add(ctl.Name, myLabel.Text)


            ElseIf TypeOf ctl Is Button Then
                myButton = CType(ctl, Button)
                _dic.Add(ctl.Name, myButton.Text)

            ElseIf TypeOf ctl Is TextBox Then
                mytext = CType(ctl, TextBox)
                _dic.Add(ctl.Name, mytext.Text)

            ElseIf TypeOf ctl Is RadioButton Then
                myRadio = CType(ctl, RadioButton)
                _dic.Add(ctl.Name, myRadio.Text)

            ElseIf TypeOf ctl Is ListBox Then
                myList = CType(ctl, ListBox)


                For i As Integer = 0 To myList.Items.Count - 1
                    _dic.Add(ctl.Name & i.ToString, myList.Items(i).ToString)
                Next

            ElseIf TypeOf ctl Is ComboBox Then
                myCombo = CType(ctl, ComboBox)

                For j As Integer = 0 To myCombo.Items.Count - 1
                    _dic.Add(ctl.Name & j.ToString, myCombo.Items(j).ToString)
                Next

            End If
        Next


        For Each Item As KeyValuePair(Of String, String) In _dic
            Dim key As String = Item.Key
            Dim value As String = Item.Value
            Debug.Print(key & " " & value)
        Next


    Catch ex As Exception
        Debug.Print(ex.ToString)
    End Try

    addSelectedClickHandlers()

End Sub


Private Sub addSelectedClickHandlers()

    Try
        For Each selectedControl As Control In Me.Controls

        If TypeOf selectedControl Is ComboBox Then
                AddHandler selectedControl.TextChanged, AddressOf selectedControl_TextChanged

                AddHandler selectedControl.GotFocus, AddressOf selectedControl_GotFocus
                AddHandler selectedControl.LostFocus, AddressOf selectedControl_LostFocus

            Else
                AddHandler selectedControl.Click, AddressOf selectedControl_Click
                AddHandler selectedControl.GotFocus, AddressOf selectedControl_GotFocus
                AddHandler selectedControl.LostFocus, AddressOf selectedControl_LostFocus
            End If

        Next

    Catch ex As Exception
        Debug.Print(ex.ToString)
    End Try


End Sub

Private Sub selectedControl_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    If TypeOf sender Is Control Then

        Me.selectedControl = DirectCast(sender, Control)

        If _dic.ContainsKey(selectedControl.Name) Then

            Dim text As String = _dic(selectedControl.Name)

            Debug.Print("I found the text """ & text & """")
        Else
            'some sort of error, key was not found

        End If
    Else
        'some sort of error, sender is not a control
    End If

End Sub

Private Sub selectedControl_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)


    If TypeOf sender Is Control Then

        Me.selectedControl = DirectCast(sender, Control)

        If _dic.ContainsKey(selectedControl.Name) Then

            Dim text As String = _dic(selectedControl.Name)

            Debug.Print("I found the text """ & text & """")
        Else
            'some sort of error, key was not found

        End If
    Else
        'some sort of error, sender is not a control
    End If

End Sub

Private Sub selectedControl_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
    Me.selectedControl = CType(sender, Control)
End Sub

Private Sub selectedControl_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
      Me.selectedControl = CType(sender, Control)
    selectedControl.BackColor = selectedControl.Tag
End Sub


End Class

Hi
I am storing texts of controls into a dictionary. I have to highlight and find the texts of controls pressing F4 for my further use. I am able to do with textBox, Buttons,RadioButtons. I getting problem with ComboBox and ListBox. Appreciate if you have an idea.

Thank you.

 Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Try
            If e.KeyCode = Keys.F4 Then


                If selectedControl Is Nothing Then

                    If _dic.ContainsKey(Me.Name) Then
                        Debug.Print("I found the text: """ & _dic(Me.Name) & """")
                    End If

                Else
                    If _dic.ContainsKey(selectedControl.Name) Then
                        selectedControl.Tag = selectedControl.BackColor
                        selectedControl.BackColor = Color.Red
                        Debug.Print("I found the text """ & _dic(selectedControl.Name) & """")

                    ElseIf _dic.ContainsKey(Me.Name) Then
                        Debug.Print("I found the text """ & _dic(Me.Name) & """")

                    End If

                End If

            End If


        Catch ex As Exception
            Debug.Print(ex.ToString)
        End Try
    End Sub

I have to highlight and find the texts of controls pressing F4 for my further use.

Apparently I have no idea what you are trying to do and you don't seem to want to answer my question. I suspect we are having a language problem.

Edited 3 Years Ago by Reverend Jim

You do not need to understand why I want to use F4 Key and I don't think it is neccessary to reveal the purpose of F4 here. What I want to do is already mentioned in my previous posts. If someone can help me to select the items of ComboBox that are stored into dictionary.
e.g. With the following code, I can find the texts of Label, Button, RadioButton, etc.

If _dic.ContainsKey(Me.Name) Then
Debug.Print("I found the text: """ & _dic(Me.Name) & """")
End If

I wasn't trying to be nosy. I was just trying to save you from doing something that wasn't necessary. After all, your thread title was Set Focus to Control Using Shortcut Key

This question has already been answered. Start a new discussion instead.