Results 1 to 5 of 5

Thread: [RESOLVED] how to make a picturebox click fire a keypress event

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2013
    Posts
    196

    Resolved [RESOLVED] how to make a picturebox click fire a keypress event

    I am trying to make a PictureBox.Click event emulate or trigger a Textbox1.KeyPress event. Since the PictureBoxSearch.Click event gets eventargs and not keypresseventargs, the code below gets an error at TextBox1_KeyPress(sender, e). The error is
    System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.KeyPressEventArgs'.'
    Can a KeyPressEventArgs variable be created somehow? If so, I could populate it with the Chr(13) character that's required. If not, is there another way to get the PictureBoxSearch.Click event trigger the Textbox1.KeyPress?


    Code:
       'run this if the enter key is pressed in Textbox1
        Private Async Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            If e.KeyChar = Chr(13) Then
                If CheckBox1.Checked Or CheckBox2.Checked Then
                    ListBox1.Items.Clear()
                    stopFlag = False
                    ProgressBar1.Style = ProgressBarStyle.Marquee
                    ProgressBar1.Visible = True
                    e.Handled = True
                    TextBox1.Enabled = False
                    CheckBox1.Enabled = False
                    CheckBox2.Enabled = False
                    ButtonStop.Enabled = True
                    Await Task.Run(Sub() StartProcessing())
                End If
            End If
        End Sub
    
        Private Sub PictureBoxSearch_Click(sender As Object, e As EventArgs) Handles PictureBoxSearch.Click
          TextBox1_KeyPress(sender, e)
        End Sub

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: how to make a picturebox click fire a keypress event

    You're making it too hard on yourself. Whatever you have in the keypress handler can be shifted into a method that can then be called from the keypress handler (while passing in the parts of the e argument that you use in the method) and called from the Click event handler (passing in whatever you want). That way, the parts of the e argument that you want will be available as arguments, and when it isn't a keypress event, you can pass in whatever you want.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2013
    Posts
    196

    Re: how to make a picturebox click fire a keypress event

    You are correct shaggy. Thanks

    Here it is
    Code:
      Private Sub PictureBoxSearch_Click(sender As Object, e As EventArgs) Handles PictureBoxSearch.Click
            StartHandler(Chr(13))
        End Sub
      
      Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            e.Handled = True
            StartHandler(e.KeyChar)
        End Sub
    
        Private Async Sub StartHandler(key As Char)
            If key = Chr(13) Then
                If CheckBox1.Checked Or CheckBox2.Checked Then
                    ListBox1.Items.Clear()
                    stopFlag = False
                    ProgressBar1.Style = ProgressBarStyle.Marquee
                    ProgressBar1.Visible = True
                    TextBox1.Enabled = False
                    CheckBox1.Enabled = False
                    CheckBox2.Enabled = False
                    ButtonStop.Enabled = True
                    Await Task.Run(Sub() StartProcessing())
                End If
            End If
        End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: how to make a picturebox click fire a keypress event

    Quote Originally Posted by PickyBiker View Post
    You are correct shaggy. Thanks

    Here it is
    Code:
      Private Sub PictureBoxSearch_Click(sender As Object, e As EventArgs) Handles PictureBoxSearch.Click
            StartHandler(Chr(13))
        End Sub
      
      Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            e.Handled = True
            StartHandler(e.KeyChar)
        End Sub
    
        Private Async Sub StartHandler(key As Char)
            If key = Chr(13) Then
                If CheckBox1.Checked Or CheckBox2.Checked Then
                    ListBox1.Items.Clear()
                    stopFlag = False
                    ProgressBar1.Style = ProgressBarStyle.Marquee
                    ProgressBar1.Visible = True
                    TextBox1.Enabled = False
                    CheckBox1.Enabled = False
                    CheckBox2.Enabled = False
                    ButtonStop.Enabled = True
                    Await Task.Run(Sub() StartProcessing())
                End If
            End If
        End Sub
    Nope. That's bad. What's the point of passing a hard-coded value into a method that then checks that value? Get rid of the parameter and the If statement. Put the If statement in the KeyPress event handler and then only call the method if the condition is True. I'd also suggest that using KeyPress and Char(13) is bad here. If you were going to use a Char, use ControlChars.Lf. Better yet, don't use a Char. Handle KeyUp or KeyDown and use Keys.Enter instead.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2013
    Posts
    196

    Re: [RESOLVED] how to make a picturebox click fire a keypress event

    I made the changes suggested by jmcilhinney. Moving the if statement to the keypress handler make sense and does away with the hard coded chr(13).

    At first I though the code below worked, but the if fell through for any keypress. I tried a couple combinations of
    If e.KeyChar = key.enter Then
    if e.keyChar is key.enter
    But neither of them would compile.

    I changed "If Keys.Enter Then" to If "e.KeyChar = ControlChars.Cr Then" and that works as expected.

    I like it.

    Code:
     
        Private Sub PictureBoxSearch_Click(sender As Object, e As EventArgs) Handles PictureBoxSearch.Click
            StartHandler()
        End Sub
        
        Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            If Keys.Enter Then
                e.Handled = True
                StartHandler()
            End If
        End Sub
    
        Private Async Sub StartHandler()
            If CheckBox1.Checked Or CheckBox2.Checked Then
                ListBox1.Items.Clear()
                stopFlag = False
                ProgressBar1.Style = ProgressBarStyle.Marquee
                ProgressBar1.Visible = True
                TextBox1.Enabled = False
                CheckBox1.Enabled = False
                CheckBox2.Enabled = False
                ButtonStop.Enabled = True
                Await Task.Run(Sub() StartProcessing())
            End If
        End Sub
    Last edited by PickyBiker; Sep 23rd, 2022 at 01:43 PM.

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