-
Sep 22nd, 2022, 06:20 PM
#1
Thread Starter
Addicted Member
[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
-
Sep 22nd, 2022, 06:44 PM
#2
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
-
Sep 22nd, 2022, 06:58 PM
#3
Thread Starter
Addicted Member
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
-
Sep 22nd, 2022, 08:20 PM
#4
Re: how to make a picturebox click fire a keypress event
Originally Posted by PickyBiker
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.
-
Sep 23rd, 2022, 12:01 PM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|