Solved

INSPECT PRESS ENTER AT BUTTON

Posted on 2013-06-21
27
355 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
Comment Utility
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
Comment Utility
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
Comment Utility
why don't you just use the button_click event?
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
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
Comment Utility
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
Comment Utility
Did you try the changed code?
0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
- 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Change

Button_Click(Sender, e)

to

Button_Click(Sender, new eventargs())
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Cool
0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
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
Comment Utility
> 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
Comment Utility
-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
Comment Utility
I don't know how you assigned F5 to it.
0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
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
Comment Utility
Sorry mate I don't understand how your code works so can't really help.
0
 
LVL 1

Author Comment

by:emi_sastra
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi CodeCruiser,

Thank you very much for your help.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

14 Experts available now in Live!

Get 1:1 Help Now