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?
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?
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.
Re: Vb.net Discover selected focus item keydown
Quote:
Originally Posted by
jmcilhinney
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
Quote:
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
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:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each tb In Controls.OfType(Of TextBox)()
AddHandler tb.TextChanged, AddressOf TextBoxes_TextChanged
Next
End Sub
Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs)
Dim tb = DirectCast(sender, TextBox)
Console.WriteLine($"The Text changed in the TextBox named {tb.Name}")
End Sub
Re: Vb.net Discover selected focus item keydown
Quote:
Originally Posted by
jmcilhinney
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:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each tb In Controls.OfType(Of TextBox)()
AddHandler tb.TextChanged, AddressOf TextBoxes_TextChanged
Next
End Sub
Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs)
Dim tb = DirectCast(sender, TextBox)
Console.WriteLine($"The Text changed in the TextBox named {tb.Name}")
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.
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.