Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. KeyDown Event and Focus Command not working

KeyDown Event and Focus Command not working

Scheduled Pinned Locked Moved Visual Basic
csharphelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Midnight Ahri
    wrote on last edited by
    #1

    i found some messenger code, modify and use it, my problem is, txtMessage keydown event that will automatically click button send is not working, and again everytime my form loads, focus is on txtDisplay, not on txtMessage. i copied some of the code below. is this caused by add handler and thread?

    Option Strict On
    Imports System.Windows.Forms
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Text
    Imports DevExpress.XtraEditors

    Namespace Forms
    Public Class Messenger

        Delegate Sub AddMessage(ByRef message As String)
    
        Private Const port As Integer = 33333
        Private Const broadcastAddress As String = "255.255.255.255"
    
        Private receivingClient As UdpClient
        Private sendingClient As UdpClient
        Private receivingThread As Thread
    
        Public Sub New()
            ' This call is required by the designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
            AddHandler btnSend.Click, AddressOf btnSend\_Click
        End Sub
    
        Private Sub txtMessage\_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
            Select Case e.KeyCode
                Case Keys.Enter
                    btnSend.PerformClick()
            End Select
        End Sub
    
        Private Sub Messenger\_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Objects.Classes.Universal.Variables.formMessenger = True
    
            InitializeSender()
            InitializeReceiver()
    
            Me.txtMessage.Text = ""
            Me.txtMessage.Focus()
        End Sub
    
        Private Sub btnSend\_Click(sender As System.Object, e As System.EventArgs)
            txtMessage.Text = txtMessage.Text.Trim()
    
            Dim toSend As String =
                "(" +
                DateTime.Now.ToString("HH:mm") +
                ")" +
                Objects.Classes.Universal.Variables.Username + " > " + txtMessage.Text
            Dim data() As Byte = Encoding.ASCII.GetBytes(toSend)
            sendingClient.Send(data, data.Length)
            Me.txtMessage.Text = ""
            Me.txtMessage.Focus()
        End Sub
    
        Private Sub MessageReceived(ByRef message As String)
            txtDisplay.Text += message + vbNewLine
        End Sub
    
        Private Sub InitializeSender()
            sendingClient = New UdpClient(broadcastAddress, port)
            sendingClient.EnableBroadc
    
    S N 2 Replies Last reply
    0
    • M Midnight Ahri

      i found some messenger code, modify and use it, my problem is, txtMessage keydown event that will automatically click button send is not working, and again everytime my form loads, focus is on txtDisplay, not on txtMessage. i copied some of the code below. is this caused by add handler and thread?

      Option Strict On
      Imports System.Windows.Forms
      Imports System.Net
      Imports System.Net.Sockets
      Imports System.Threading
      Imports System.Text
      Imports DevExpress.XtraEditors

      Namespace Forms
      Public Class Messenger

          Delegate Sub AddMessage(ByRef message As String)
      
          Private Const port As Integer = 33333
          Private Const broadcastAddress As String = "255.255.255.255"
      
          Private receivingClient As UdpClient
          Private sendingClient As UdpClient
          Private receivingThread As Thread
      
          Public Sub New()
              ' This call is required by the designer.
              InitializeComponent()
              ' Add any initialization after the InitializeComponent() call.
              AddHandler btnSend.Click, AddressOf btnSend\_Click
          End Sub
      
          Private Sub txtMessage\_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
              Select Case e.KeyCode
                  Case Keys.Enter
                      btnSend.PerformClick()
              End Select
          End Sub
      
          Private Sub Messenger\_Load(sender As Object, e As System.EventArgs) Handles Me.Load
              Objects.Classes.Universal.Variables.formMessenger = True
      
              InitializeSender()
              InitializeReceiver()
      
              Me.txtMessage.Text = ""
              Me.txtMessage.Focus()
          End Sub
      
          Private Sub btnSend\_Click(sender As System.Object, e As System.EventArgs)
              txtMessage.Text = txtMessage.Text.Trim()
      
              Dim toSend As String =
                  "(" +
                  DateTime.Now.ToString("HH:mm") +
                  ")" +
                  Objects.Classes.Universal.Variables.Username + " > " + txtMessage.Text
              Dim data() As Byte = Encoding.ASCII.GetBytes(toSend)
              sendingClient.Send(data, data.Length)
              Me.txtMessage.Text = ""
              Me.txtMessage.Focus()
          End Sub
      
          Private Sub MessageReceived(ByRef message As String)
              txtDisplay.Text += message + vbNewLine
          End Sub
      
          Private Sub InitializeSender()
              sendingClient = New UdpClient(broadcastAddress, port)
              sendingClient.EnableBroadc
      
      S Offline
      S Offline
      Simon_Whale
      wrote on last edited by
      #2

      have you looked at the tab order of the two textboxes?

      Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

      M 1 Reply Last reply
      0
      • S Simon_Whale

        have you looked at the tab order of the two textboxes?

        Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

        M Offline
        M Offline
        Midnight Ahri
        wrote on last edited by
        #3

        yes, i put tabindex for txtMessage = 0 and 99 for txtDisplay. but still the txtDisplay was focused when i open the form. X|

        1 Reply Last reply
        0
        • M Midnight Ahri

          i found some messenger code, modify and use it, my problem is, txtMessage keydown event that will automatically click button send is not working, and again everytime my form loads, focus is on txtDisplay, not on txtMessage. i copied some of the code below. is this caused by add handler and thread?

          Option Strict On
          Imports System.Windows.Forms
          Imports System.Net
          Imports System.Net.Sockets
          Imports System.Threading
          Imports System.Text
          Imports DevExpress.XtraEditors

          Namespace Forms
          Public Class Messenger

              Delegate Sub AddMessage(ByRef message As String)
          
              Private Const port As Integer = 33333
              Private Const broadcastAddress As String = "255.255.255.255"
          
              Private receivingClient As UdpClient
              Private sendingClient As UdpClient
              Private receivingThread As Thread
          
              Public Sub New()
                  ' This call is required by the designer.
                  InitializeComponent()
                  ' Add any initialization after the InitializeComponent() call.
                  AddHandler btnSend.Click, AddressOf btnSend\_Click
              End Sub
          
              Private Sub txtMessage\_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
                  Select Case e.KeyCode
                      Case Keys.Enter
                          btnSend.PerformClick()
                  End Select
              End Sub
          
              Private Sub Messenger\_Load(sender As Object, e As System.EventArgs) Handles Me.Load
                  Objects.Classes.Universal.Variables.formMessenger = True
          
                  InitializeSender()
                  InitializeReceiver()
          
                  Me.txtMessage.Text = ""
                  Me.txtMessage.Focus()
              End Sub
          
              Private Sub btnSend\_Click(sender As System.Object, e As System.EventArgs)
                  txtMessage.Text = txtMessage.Text.Trim()
          
                  Dim toSend As String =
                      "(" +
                      DateTime.Now.ToString("HH:mm") +
                      ")" +
                      Objects.Classes.Universal.Variables.Username + " > " + txtMessage.Text
                  Dim data() As Byte = Encoding.ASCII.GetBytes(toSend)
                  sendingClient.Send(data, data.Length)
                  Me.txtMessage.Text = ""
                  Me.txtMessage.Focus()
              End Sub
          
              Private Sub MessageReceived(ByRef message As String)
                  txtDisplay.Text += message + vbNewLine
              End Sub
          
              Private Sub InitializeSender()
                  sendingClient = New UdpClient(broadcastAddress, port)
                  sendingClient.EnableBroadc
          
          N Offline
          N Offline
          Newbie51
          wrote on last edited by
          #4

          I think that is because on some of the subs, you tell it what to handle.

          txtMessage_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)

          should be

          txtMessage_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) handles txtmessage_keydown

          M 1 Reply Last reply
          0
          • N Newbie51

            I think that is because on some of the subs, you tell it what to handle.

            txtMessage_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)

            should be

            txtMessage_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) handles txtmessage_keydown

            M Offline
            M Offline
            Midnight Ahri
            wrote on last edited by
            #5

            thank you very much, yes i forgot to put that handle. afterall, it's because i use LayoutControl from another component, that control defaultly focuses any textbox at the top, i've turn it off. X|

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups