I'm currently playing around in Visual Basic 6.0 and wondered how can you cause a message box or some other code to appear/activate when the user presses a specific key on the keyboard like F1 or F2.
Use the Form_KeyDown(Keycode, Shift) method in your form:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF1 Then
//do something here if F1 key is pressed
//or call a method
End If
End Sub
Also, you might also want to set the KeyPreview property to true for your form.
Related
I am doing a quiz game in VB6. I need the textbox to automatically capialize the first letter but this code
Private Sub Anstxt_Change()
Anstxt.Text = StrConv(Anstxt.Text, vbProperCase)
End Sub
causes the word to invert. So instead of "Trees" it turns into "Seert"
How do I change this?
Pay attention to where the cursor is positioned in the textbox when the event Change occurs: its at the start of the textbox. Add a Debug.Print statement to see what's going on while you type:
Private Sub Anstxt_Change()
Debug.Print StrConv(Anstxt.Text, vbProperCase)
Anstxt.Text = StrConv(Anstxt.Text, vbProperCase)
End Sub
The output looks like
T
T
Rt
Rt
Ert
Ert
Eert
Eert
Seert
Seert
Two things to notice here: the Change event is triggered twice: once from typing and once from changing the value of the textbox within the Change event. That gives you an idea that manipulating the text of a textbox in its Change event isn't a good idea. I suggest to put this code in the LostFocus event instead.
The second thing to notice is that as the cursor is always at the start of the textbox, the letters you type are inserted there in front of the existing letters. So after you change the .Text property of the textbox, you should position the cursor at the end of the textbox with the .SelStart method:
Anstxt.SelStart = Len(Anstxt.Text)
e.g.
Private Sub Anstxt_Change()
Anstxt.Text = StrConv(Anstxt.Text, vbProperCase)
Anstxt.SelStart = Len(Anstxt.Text)
End Sub
Ok, after searching for an hour, I still haven't found the right answer for this. All I want is to disable the right click on my button because it's creating a bug, according to our tester. I don't know if VB6 can't do this, but if VB6 can't do this, is there any possible way? Ok, just to be more specific, here is my example...
'I found this somewhere...
Private Sub cmdExportCSV_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
'Do Nothing
End If
End Sub
'Then I have here
Private Sub cmdExportCSV_Click()
'Some logic here
End Sub
But when I click the right button on my mouse, the cmdExportCSV_Click() still executing the code inside.
How about something like this then (based upon your code):
Private m_mouseButton As Integer
Private Sub cmdExportCSV_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
m_mouseButton = Button
End Sub
Private Sub cmdExportCSV_Click()
If m_mouseButton = vbRightButton Then
'Do Nothing
Exit Sub
End If
'Some logic here
End Sub
I didn't run this or anything, but it should work.
I have resolved my own problem.
From this..
Private Sub cmdExportCSV_Click()
If m_mouseButton = vbRightButton Then
'Do Nothing
Exit Sub
End If
'Some logic here
End Sub
To this..
Private Sub cmdExportCSV_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button And vbRightButton Then
Exit Sub
End If
'Some logic here
End Sub
That's it! VB6 won't directly allow you to disable the mouse right and mouse left button. My solution is just to exit event whenever the user click the mouse right button.
Anyway, I'm sorry as I found out that the system is using asImageCommand not CommandButton, so when you declare an event like this "Private Sub cmdExportCSV_Click()", the asImageCommand control will trigger when you click the mouse-right and mouse-left button. But if you are using CommandButton, it will only trigger when you click the left-button, and won't allow you to use the right-button of the mouse.
I think I have to update the title of my question.
According to Microsoft:
"In Visual Basic 6.0, the Click event is raised when the CheckBox state is changed programmatically. "
and this is exactly what i do not want.
I want click event only raise when i click on the checkbox and not when the state is changed.
Any idea how to do it ?
Thank you
you can set a flag when you are populating the form from code to ignore changes. This can get messy if the code is not organized well.
Form Level:
Public IgnoreChange As Boolean
Form Load:
IgnoreChange = False
Event:
If IgnoreChange Then Exit Sub
Your code:
frmReference.IgnoreChange = True
frmReference.Checkbox1.Checked = True
frmReference.IgnoreChange = False
Code should only respond to user actions
Not sure if it's the best way, but one way would be to have a variable called something like IgnoreEvents and set that to true right before changed the state programmatically.
Then in the event handler if that variable is true, you just exit the event handler without doing anything.
Put your Click event code in the MouseDown event instead. You'll have to manually set the checkstate though:
Private Sub Check1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Check1.Value = IIf(Check1.Value = vbChecked, vbUnchecked, vbChecked)
' run other necessary code here
End Sub
I Am New To VB6 , And Doing With College Project Can Anyone Tell How Can I Close My Form Without Using Any Command Buttons OR Control Tool.
Whenever Application Is Active OR Form Is Active , And User Press The 'W' Key Than Form Should Be 'END' / 'UNLOAD"
How Can I Do That ?
I TRIED THESE CODE :
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then
Unload Me
End If
End Sub
BUT IT DIDn't WORK.
You need to make sure that the Form's KeyPreview Property is set to True, otherwise your Form will not process the KeyStrokes. I would also test for both upper and lower case.
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 87 Or KeyAscii = 119 Then '87 is upper case 119 is lower case
Unload Me
End If
End Sub
and if you want to check for Modifier Keys such as Control and Alt I would use the Form's KeyDown EventHandler instead.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If (Shift And 1) Then ' Test for Shift Key
If (KeyCode = 87 Or KeyCode = 119) Then
Unload Me
End If
End If
If (Shift And 2) Then 'Test for Control Key
If (KeyCode = 87 Or KeyCode = 119) Then
Unload Me
End If
End If
If (Shift And 4) Then 'Test for Alt Key
If (KeyCode = 87 Or KeyCode = 119) Then
Unload Me
End If
End If
End Sub
Alt-F4 is a built-in hot key for Form Close in VB6 as in most other programs conforming to Windows Application Guidelines.
People also commonly have a menu option "Exit" and set its accelerator key to "x" so you might have a File menu with "F" and an option Exit with "x" and the user can type Alt-F, x to exit. See Notepad or hundreds of other programs as examples of this.
Yes, you can use hackish approaches, but why?
I have a grid on a user control, which is on a user document vbd page, which is in ActiveX exe Application
This is the event I get when pressing F5 on the grid
Private Sub mnuRightClickRefresh_Click()
'Call ...
End Sub
When I press F3 on the grid
I get the right event
Private Sub grdObjects_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 116 Then
'...
End If
End Sub
Any ideas?
It seems that you are trying to handle the _KeyUp function in two different functions.
When you Press F5, make sure that no control has any focus (thus making the form it self as the focused control) then you may handle the event in the following way:
Private Sub grdObjects_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 116
'Do Whatever F3 would want to do
Case 118
'Do F5 Stuff
Case 119
'Any other stuff for F6
End Select
End Sub
Let me know if this is what you wanted.