Problem When Press Enter

Private Sub Command1_Click()
reply = MsgBox("Press Enter", vbOKCancel, "Warning")
Combo1.SetFocus
End Sub

Private Sub Combo1_KeyUp(keycode As Integer, shift As Integer)
If keycode = 13 Then
    MsgBox ("Keypress at combo1")
End If
End Sub

at The top a sample code  to explain about my problem.
When I click Command1 Button MSGBOX will appear and when i press ENTER ,this action make Keycode=13 and will Appear A MsgBOx at combo1. How to fix this problem ?
Because I press ENTER AT MSGBOX then why this action make combo1_keyUp running ?
ekohartonoAsked:
Who is Participating?
 
trkcorpConnect With a Mentor Commented:
I would alter the code I provided after a little thought... Do it like this because if you DON'T press Enter but click or Alt+key then bMsgResponse gets hung as ON and that too is a hole.  This should stop that.

Option Explicit
Private bMsgResponse As Boolean

Private Sub Combo1_Change()
   bMsgResponse = False
End Sub

Private Sub Combo1_Click()
   bMsgResponse = False
End Sub

Private Sub Combo1_LostFocus()
   bMsgResponse = False
End Sub

Private Sub Command1_Click()
Dim reply As Integer
reply = MsgBox("Press Enter", vbOKCancel, "Warning")
' before setfocus set a flag for combo1_keyup
bMsgResponse = True
Combo1.SetFocus
End Sub

Private Sub Combo1_KeyUp(keycode As Integer, shift As Integer)
' if here because of code above, do nothing
If bMsgResponse Then
  bMsgResponse = False
  Exit Sub
End If
If keycode = 13 Then
  MsgBox ("Keypress at combo1")
End If
End Sub
0
 
cnealyCommented:
Try commenting out the Combo1.SetFocus and then test the code.
0
 
ekohartonoAuthor Commented:
Combo1.SetFocus Is Needed so it must be at the command1 button because the command1 button use to make stfocus at the combo1.Like I say this Just sample code my original code more complicated and i need combo1.Setfocus
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
inethogCommented:
I had a quick look at your code and here's a solution for you:

Hope this helps,

D.

---------------

Option Explicit

Private Sub Command1_Click()

    Dim reply
   
    reply = MsgBox("Press Enter", vbOKCancel, "Warning")
    Combo1.SetFocus

End Sub

Private Sub Combo1_KeyUp(keycode As Integer, shift As Integer)
   
    If keycode = 13 Then
       MsgBox ("Keypress at combo1")
       Command1.SetFocus
    End If
   
End Sub
0
 
ekohartonoAuthor Commented:
To: inethog

Your Code still have a problem ,don't answer my problem.

Thank's any way
0
 
inethogCommented:
Works fine on my machine... *shrugs*
0
 
ekohartonoAuthor Commented:
inethog


You Allready press enter when MSGBOX "Press Enter" is appear ? then when you press enter what happend ? is the Msg (Key Pressed at Combo1) Appear ?
0
 
trkcorpCommented:
Well this may seem a bit hokey but it will work...
At the module level add this bool:

Option Explicit
Private bMsgResponse As Boolean

Private Sub Command1_Click()
Dim reply As Integer
reply = MsgBox("Press Enter", vbOKCancel, "Warning")
' before setfocus set a flag for combo1_keyup
bMsgResponse = True
Combo1.SetFocus
End Sub

Private Sub Combo1_KeyUp(keycode As Integer, shift As Integer)
' if here because of code above, do nothing
If bMsgResponse Then
   bMsgResponse = False
   Exit Sub
End If
If keycode = 13 Then
   MsgBox ("Keypress at combo1")
End If
End Sub

0
 
amebaCommented:
Use flag, or change "Private Sub Combo1_KeyUp(...)" to "Private Sub Combo1_KeyDown(...)"
0
 
KarcOriginCommented:
Dear  ekohartono,

I think you want to know why ENTER AT MSGBOX action make combo1_keyUp running.
Actually ENTER event is combination of KEY_DOWN + KEY_UP. Now what happen when you press ENTER the KEY_DOWN event executes first and your message box is unloaded, now after that you are setting focus to your Combobox and since you have pressed the ENTER key at last KEY_UP event fires. Because of this your ComboBox receive this event and your ("Keypress at combo1") pops up. To aviod this it is better to either write your code at KEY_DOWN events or use a flag as trkcorp has said.

I hope it helps you.

Bye.
0
 
MadmarlinCommented:

The Combo1_KeyUp event will be triggered when the control has been set to focus.
What i would suggest is setting a flag.

EG

Dim bol_Flag as boolean

Private Sub Command1_Click()
reply = MsgBox("Press Enter", vbOKCancel, "Warning")
bol_Flag = False
Combo1.SetFocus
bol_Flag = True
End Sub

Private Sub Combo1_KeyUp(keycode As Integer, shift As Integer)
if Bol_Flag = False then Exit Sub
If keycode = 13 Then
   MsgBox ("Keypress at combo1")
End If
End Sub


Here because your setting bol_Flag to false just before you setfoces the KeyUp event will just drop out.
But you set it back to true after you set focus as to not drop out if KeyUp is triggered another way..

Good Luck
Madmarlin
0
 
TigerZhaoCommented:
it's a bad habit to respond some "system key" (Like Enter, Alt+Tab, Ctrl+Alt+Delete), the best import is to check your requirement and design
0
 
trkcorpCommented:
Although madmartin's code is almost exactly like mine, he makes one bad assumption, that you will always execute the command1_click event to set the flag to true to allow Combo1_KeyUp to capture key strokes...  I doubt seriously that your program does not do more than the snippet we are working on.  If you do it like madmartin suggests your Combo1_KeyUp event will never ever see any keys unless you have clicked command1 and set the flag on... This is inverted logic with a big hole in it.  My way, the ONLY time you will exit the Combo1_KeyUp event is after your message box and setting the flag to false in the Combo1_KeyUp event frees the event to capture key strokes.  The boolean flag is, of course, False by default on load and will be False unless set true...
0
 
ekohartonoAuthor Commented:
In visual Basic don't have any kind of instruction like Pascal Have such as Clear Key ? Any way I think trkcorp answer is good
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.