Solved

INSPECT PRESS ENTER AT BUTTON

Posted on 2013-06-21
27
356 Views
Last Modified: 2013-07-04
Hi All,

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

How could I do it ?

Thank you.
0
Comment
Question by:emi_sastra
  • 16
  • 10
27 Comments
 
LVL 1

Author Comment

by:emi_sastra
ID: 39265358
More info. There are some buttons at a form.

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

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39267652
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
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 39268569
why don't you just use the button_click event?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39268686
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 39268709
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39268738
Did you try the changed code?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39268789
- 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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39269778
Button click accepts EventArgs and I do not see why it is looking for mouseeventargs.

CButton is part of infragistics?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39270111
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 39270117
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39270832
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 39270928
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39271450
Change

Button_Click(Sender, e)

to

Button_Click(Sender, new eventargs())
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:emi_sastra
ID: 39271563
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 39271580
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 39271939
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39272208
Cool
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39273266
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39274557
> 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
 
LVL 1

Author Comment

by:emi_sastra
ID: 39276729
-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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39278206
I don't know how you assigned F5 to it.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39278487
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39279498
Sorry mate I don't understand how your code works so can't really help.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39295382
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39297344
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
 
LVL 1

Author Comment

by:emi_sastra
ID: 39301058
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
 
LVL 1

Author Closing Comment

by:emi_sastra
ID: 39301061
Hi CodeCruiser,

Thank you very much for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now