Results 1 to 7 of 7

Thread: Vb.net Discover selected focus item keydown

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Vb.net Discover selected focus item keydown

    I want to get the focus of the objects inside a form and know if they are of type text or if they are of type text, like richtextbox,textbox,wordwrap I can control an event in them created by code, because I will do 5 per code to control but I intend to do it but then I made the following code:

    Code:
    ' captures all form events
    For Each formcontrols As Control In Me.Controls
      If formcontrols.HasChildren then
      ''''MsgBox(formcontrols.Controls.Count, MsgboxStyle.OkOnly,"Count Element intro")
    
    'captures whatever textbox there are and finds which one has the focus
    For Each textboxbycode As TextBox In Me.Controls
        If textboxbycode.focus then
          ' here the idea is to capture the keydown event
         End if
    End If
    Next
    The code above counts the elements that are inside a form to be unlimited the items now I need to capture the keydown event of these elements without being created is it possible or create a keydown event for it?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Vb.net Discover selected focus item keydown

    Your post makes little sense as it is. Are you actually saying that you want to determine which controls can receive focus and attach a handler to their KeyDown event?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Vb.net Discover selected focus item keydown

    If you're saying that you just want to find out what control currently has focus then that's exactly what the form's ActiveControl property is for. I'm not sure what use that would be though, if your aim is to handle events. In that case you should just handle the event for every control that could be the active control.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: Vb.net Discover selected focus item keydown

    Quote Originally Posted by jmcilhinney View Post
    If you're saying that you just want to find out what control currently has focus then that's exactly what the form's ActiveControl property is for. I'm not sure what use that would be though, if your aim is to handle events. In that case you should just handle the event for every control that could be the active control.
    Yes in case finding focus is not the problem the focus is just to know who is selected is clicked, but in case I want to control the keydown event of an object with focus . When i click on a textbox and type something i create a key event then it show me an action.

    that's what I'm having trouble getting from the control with the focus on the keydown event.

    see a quickly created demo logic
    for each obj as control in me.controls ' obj is a variable that says a control in each control of the form
    obj_Keydown(sender,e) ' the idea here is to get the obj keydown but this code only works with existing keydowns I've used before
    next ' reads the next thing.

    if me.ActiveControls = textbox then
    msgbox (from my function)
    end if

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Vb.net Discover selected focus item keydown

    You just handle the appropriate event of every control. In the event handler, the sender parameter is a reference to the object that raised the event, so that's how you know what control it was. E.g.
    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.     For Each tb In Controls.OfType(Of TextBox)()
    3.         AddHandler tb.TextChanged, AddressOf TextBoxes_TextChanged
    4.     Next
    5. End Sub
    6.  
    7. Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs)
    8.     Dim tb = DirectCast(sender, TextBox)
    9.  
    10.     Console.WriteLine($"The Text changed in the TextBox named {tb.Name}")
    11. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: Vb.net Discover selected focus item keydown

    Quote Originally Posted by jmcilhinney View Post
    You just handle the appropriate event of every control. In the event handler, the sender parameter is a reference to the object that raised the event, so that's how you know what control it was. E.g.
    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.     For Each tb In Controls.OfType(Of TextBox)()
    3.         AddHandler tb.TextChanged, AddressOf TextBoxes_TextChanged
    4.     Next
    5. End Sub
    6.  
    7. Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs)
    8.     Dim tb = DirectCast(sender, TextBox)
    9.  
    10.     Console.WriteLine($"The Text changed in the TextBox named {tb.Name}")
    11. End Sub
    I would never find this, really very good knowledge only for those who have been working for years thank you very much, the visual basic guides did not teach how to work that way, only apply the calculation and guidance functions but there is no linking these options in the code. is need explore it.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Vb.net Discover selected focus item keydown

    This is why you don't just try to solve the specific issue in front of you. If you learn about events and event handlers in general then you learn this information, which you can then apply to any number of specific situations. This is why it is important to learn the general principles of programming and the specific language that you're using and not just jump in the deep end and try to put out fires as they inevitably arise. You're always going to encounter stuff you don't know and can't work out but the more general knowledge you can pick up first, the better. Programming is all about understand the general principles and then applying them in various combinations to specific situations.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width