INSPECT PRESS ENTER AT BUTTON

Hi All,

I want to do something when  user press Enter at Button Control.

How could I do it ?

Thank you.
LVL 1
emi_sastraAsked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
As shown in example at

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.click.aspx

button click event handler has following signature

Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click


So my question again, what control is this that you are using?
0
 
emi_sastraAuthor Commented:
More info. There are some buttons at a form.

I need to know which button is press Enter by user.

Thank you.
0
 
emi_sastraAuthor Commented:
I try below, I have problem to pass MouseEventArgs from sub Button_Enter.

 Private Sub Button_Click(Sender As Object, e As MouseEventArgs) Handles _
                btnUangAwal.Click, _
                btnUangKeluar.Click, _
                btnBukaLaci.Click, _
                btnLaporanAkhirShift.Click, _
                btnKunciLayar.Click, _
                btnClose.Click

        Dim btn As CButtonLib.CButton = DirectCast(Sender, CButtonLib.CButton)

        Check_Button(btn)
 
    End Sub

    Private Sub Button_Enter(Sender As Object, e As KeyEventArgs) Handles _
                  btnUangAwal.KeyDown, _
                  btnUangKeluar.KeyDown, _
                  btnBukaLaci.KeyDown, _
                  btnLaporanAkhirShift.KeyDown, _
                  btnKunciLayar.KeyDown, _
                  btnClose.KeyDown

        If Not e.KeyCode = Keys.LWin Then Exit Sub

        Dim btn As CButtonLib.CButton = DirectCast(Sender, CButtonLib.CButton)

        Button_Click(Sender, e)  -->problem here

    End Sub

How could I do it ?

Thank you.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
David Johnson, CD, MVPOwnerCommented:
why don't you just use the button_click event?
0
 
CodeCruiserCommented:
If you want a specific button to be clicked when Enter is pressed then set the AcceptButton property of the form.

You can change your code to below

 Private Sub Button_Click(Sender As Object, e As EventArgs) Handles _
                btnUangAwal.Click, _
                btnUangKeluar.Click, _
                btnBukaLaci.Click, _
                btnLaporanAkhirShift.Click, _
                btnKunciLayar.Click, _
                btnClose.Click

        Dim btn As CButtonLib.CButton = DirectCast(Sender, CButtonLib.CButton)

        Check_Button(btn)
 
    End Sub
0
 
emi_sastraAuthor Commented:
Hi CodeCruiser,

-If you want a specific button to be clicked when Enter is pressed then set the AcceptButton property of the form.
The form AcceptButton property only accept none or one button only.

Thank you.
0
 
CodeCruiserCommented:
Did you try the changed code?
0
 
emi_sastraAuthor Commented:
- Did you try the changed code?
Yes, the result :

Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.KeyEventArgs'.

Thank you.
0
 
CodeCruiserCommented:
Button click accepts EventArgs and I do not see why it is looking for mouseeventargs.

CButton is part of infragistics?
0
 
emi_sastraAuthor Commented:
Private Sub Button_Click(Sender As Object, e As MouseEventArgs)  Handles _
                btnUangAwal.Click
You want me change to :
Private Sub Button_Click(Sender As Object, e As EventArgs)  Handles _
                btnUangAwal.Click

Which is not compatible.

Thank you.
0
 
emi_sastraAuthor Commented:
Private Sub Button_Click(Sender As Object, e As MouseEventArgs)  Handles _
                btnUangAwal.Click
You want me change to :
Private Sub Button_Click(Sender As Object, e As EventArgs)  Handles _
                btnUangAwal.Click

The first one is mouse event, the second one is keyboard event.

Which is not compatible.

Thank you.
0
 
emi_sastraAuthor Commented:
Hi CodeCruiser,

Private Sub Button_Click(Sender As Object, e As EventArgs)  Handles _
                btnUangAwal.Click

Change to :
Private Sub Button_Click(sender As Object, e As System.EventArgs) Handles Button.Click

It works.

But a problem raised is :

 Private Sub Button_Enter(Sender As Object, e As KeyEventArgs) Handles _
                  btnUangAwal.KeyDown, _
                  btnUangKeluar.KeyDown, _
                  btnBukaLaci.KeyDown, _
                  btnLaporanAkhirShift.KeyDown, _
                  btnKunciLayar.KeyDown, _
                  btnClose.KeyDown

        If Not e.KeyCode = Keys.LWin Then Exit Sub
       
        Button_Click(Sender, e)

    End Sub

Any keyboard control I press, it goes to button keydown. While I want to accept Enter key only.

How could I overcome this problem ?

Thank you.
0
 
CodeCruiserCommented:
Change

Button_Click(Sender, e)

to

Button_Click(Sender, new eventargs())
0
 
emi_sastraAuthor Commented:
If Not e.KeyCode = Keys.LWin Then Exit Sub

The problem is at  ===>     If Not e.KeyCode

 I can not capture Enter keycode.

Thank you.
0
 
emi_sastraAuthor Commented:
If Not e.KeyCode = Keys.LWin Then Exit Sub

Actually I want to code :
If Not e.KeyCode = Keys.Enter Then Exit Sub, but I never get Enter Key, always LWIN.

Thank you.
0
 
emi_sastraAuthor Commented:
Hi CodeCruiser,

I use PreviewKeyDown and it works.

 Private Sub Button_Enter(Sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles _
                  btnUangAwal.PreviewKeyDown, _
                  btnUangKeluar.PreviewKeyDown, _
                  btnBukaLaci.PreviewKeyDown, _
                  btnLaporanAkhirShift.PreviewKeyDown, _
                  btnKunciLayar.PreviewKeyDown, _
                  btnClose.PreviewKeyDown

        If Not e.KeyCode = Keys.Enter Then Exit Sub

        Button_Click(Sender, New EventArgs())

    End Sub

Thank you.
0
 
CodeCruiserCommented:
Cool
0
 
emi_sastraAuthor Commented:
Still one problem.

Private Sub Button_Click(sender As Object, e As System.EventArgs) Handles _
                btnTunda.Click

        Dim dictionary As New Dictionary(Of CButtonLib.CButton, Integer)
        dictionary.Add(btnTunda, Keys.F5)
     
          Dim btn As CButtonLib.CButton = DirectCast(sender, CButtonLib.CButton)

        Dim keyEventArgs = New KeyEventArgs(dictionary(btn))

        Check_Keys(btn, keyEventArgs)

    End Sub

 Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

        'Kalau bukan F, Ctrl dan Alt maka keluar

        If e.KeyCode = Keys.Escape Then
            Dim intAnswer As Integer = MsgBox("Yakin Mau Keluar Dari Menu ...!", MsgBoxStyle.YesNo, Me.Text)
            If intAnswer = vbYes Then
                Me.Close()
                Exit Sub
            End If
        Else
            If Not (e.Shift Or _
             e.KeyCode.ToString.Contains("F") Or _
             e.Alt Or _
             e.Control) Then Exit Sub
        End If

        Me.Check_Keys(sender, e)

    End Sub

After a button click, I showdialog a form. After the form is close.
The event goes to frmMain_KeyDown and the just closed form is showed again at Check_Keys sub.

What should I do overcome this ?

Thank you.
0
 
CodeCruiserCommented:
> After the form is close. The event goes to frmMain_KeyDown and the just closed form is showed again at Check_Keys sub.

How do you close the form? With mouse click or with escape? Maybe something wrong in your check_keys method.
0
 
emi_sastraAuthor Commented:
-How do you close the form? With mouse click or with escape? Maybe something wrong in your check_keys method.
I close the child form with Esc key, but this is happened at the child form.

I think the child form opened by using button click, I assign F5 to it.
Then the child form is opened, after the child form is closed, the form keydown event is trigger since we have assign F5 to it.

Could it be ?

Thank you.
0
 
CodeCruiserCommented:
I don't know how you assigned F5 to it.
0
 
emi_sastraAuthor Commented:
Please see below code :
   
 Dim dictionary As New Dictionary(Of CButtonLib.CButton, Integer)
        dictionary.Add(btnTunda, Keys.F5)
   
        Dim btn As CButtonLib.CButton = DirectCast(sender, CButtonLib.CButton)

        Dim keyEventArgs = New KeyEventArgs(dictionary(btn))

        Check_Keys(btn, keyEventArgs)

Thank you.
0
 
CodeCruiserCommented:
Sorry mate I don't understand how your code works so can't really help.
0
 
emi_sastraAuthor Commented:
I am sorry, just back.

Please see below code.

public class Form1

    private blnEventFromButtonClick as boolean = false

Private Sub Button_Click(sender As Object, e As System.EventArgs) Handles _
                btnTunda.Click

        Dim dictionary As New Dictionary(Of button, Integer)
        dictionary.Add(btnTunda, Keys.F5)
     
          Dim btn As button = DirectCast(sender, button)

        Dim keyEventArgs = New KeyEventArgs(dictionary(btn))

         blnEventFromButtonClick = true

    End Sub

 Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

       if blnEventFromButtonClick  then
            msgbox "Event from Button Click"
      else
          msgbox "Event Not from Button Click"
      endif
       
         blnEventFromButtonClick = false

      End Sub

End Class

After I click btnTunda (click event), then I assign Keys.F5 to it.
Does it trigger form KeyDown event also after the click event sub is done ?
If it is, how to clear the Keys.F5 from memory to avoid form keydown event triggered ?

Thank you.
0
 
CodeCruiserCommented:
Why not move the

  blnEventFromButtonClick = true

to top of that sub and then in form keydown event, ignore the key if   blnEventFromButtonClick is true?
0
 
emi_sastraAuthor Commented:
Hi  CodeCruiser,

I've got the problem, it is not because of the F keys assigned.

But because I have assign 2 handles :

 Private Sub Button_Click(sender As Object, e As System.EventArgs) Handles _
                btnTunda.Click, _
                btnTunda.Click

End Sub

Because it is call twice, then it is called twice.

Why it is happened, because I have more than 10 button, and I missed it.

Thank you.
0
 
emi_sastraAuthor Commented:
Hi CodeCruiser,

Thank you very much for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.