Solved

Problem When Press Enter

Posted on 2002-03-27
14
182 Views
Last Modified: 2010-05-18
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 ?
0
Comment
Question by:ekohartono
  • 4
  • 3
  • 2
  • +5
14 Comments
 
LVL 1

Expert Comment

by:cnealy
Comment Utility
Try commenting out the Combo1.SetFocus and then test the code.
0
 

Author Comment

by:ekohartono
Comment Utility
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
 
LVL 1

Expert Comment

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

Author Comment

by:ekohartono
Comment Utility
To: inethog

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

Thank's any way
0
 
LVL 1

Expert Comment

by:inethog
Comment Utility
Works fine on my machine... *shrugs*
0
 

Author Comment

by:ekohartono
Comment Utility
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
 
LVL 4

Expert Comment

by:trkcorp
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 15

Expert Comment

by:ameba
Comment Utility
Use flag, or change "Private Sub Combo1_KeyUp(...)" to "Private Sub Combo1_KeyDown(...)"
0
 
LVL 5

Expert Comment

by:KarcOrigin
Comment Utility
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
 
LVL 1

Expert Comment

by:Madmarlin
Comment Utility

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

Expert Comment

by:TigerZhao
Comment Utility
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
 
LVL 4

Expert Comment

by:trkcorp
Comment Utility
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
 
LVL 4

Accepted Solution

by:
trkcorp earned 50 total points
Comment Utility
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
 

Author Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB6 convert MSHFlexgrid1 cells no2 2 49
VBA color chart bars 12 64
vb6 connector to mongodb 2 23
How to Add / Edit Windows Menu 4 23
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

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

10 Experts available now in Live!

Get 1:1 Help Now