Solved

INSPECT PRESS ENTER AT BUTTON

Posted on 2013-06-21
27
361 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 80

Expert Comment

by:David Johnson, CD, MVP
ID: 39268569
why don't you just use the button_click event?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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