Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 844
  • Last Modified:

How to make Form Keydown event ignore if in Textbox

Hi,

I have got a project where I need to detect a key press at any time that the form is open and send out different serial data dependent on the keycode detected.  The form however has text boxes, which I need to be able to type into and not flag up as a form keydown event.

I know in VB5 you could use the following...

If TypeOf Screen.ActiveControl Is TextBox Then
    Exit sub

is there a VB.NET equivalent?

Thanks in advance
0
fvcctv
Asked:
fvcctv
  • 2
1 Solution
 
planoczCommented:
Use something like this. just change thee code to match what you need.

 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        HandleTextBoxes(Me)
    End Sub
   Private Sub HandleTextBoxes(ByVal container As Control)
        Dim Ctl As Control
        For Each Ctl In container.Controls
            If TypeOf Ctl Is TextBox Then
                AddHandler CType(Ctl, TextBox).TextChanged, AddressOf Me.OnTextboxChange
            ElseIf Ctl.Controls.Count > 0 Then
                HandleTextBoxes(Ctl) ' recurse into another container
            End If
        Next
    End Sub
 
  Public Sub OnTextboxChange(ByVal sender As Object, ByVal e As EventArgs)
        Debug.WriteLine("Text changed in " & sender.name)
    End Sub

Open in new window

0
 
fvcctvAuthor Commented:
If TypeOf Me.ActiveControl Is TextBox Then
0
 
fvcctvAuthor Commented:
Found solution myself
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now