?
Solved

INSPECT PRESS ENTER AT BUTTON

Posted on 2013-06-21
27
Medium Priority
?
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 82

Expert Comment

by:David Johnson, CD, MVP
ID: 39268569
why don't you just use the button_click event?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

801 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