VB6 Wrong Events fires on F5 - events

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.

Related

Proper Casing in VB6

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

Disabling Mouse Right Click On Image Control VB6

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.

How to make a message box appear after pressing a key

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.

Closing Form In VB6 With Keyword 'W' Or 'Ctrl+W' or 'Alt+w'

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?

textbox validation in vb6 (disable paste option)

validation on textbox in vb 6.0
i tried
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (KeyAscii = vbKeyBack Or KeyAscii = vbKeyDelete Or KeyAscii = vbKeySpace Or (KeyAscii >= Asc("1") And KeyAscii <= Asc("9"))) Then
KeyAscii = 0
End If
but now i want that paste option should disable when i right click on textbox
If you want to prevent right-click menu items from being used on the textbox you can create your own context menu.
Then, using API calls you need to unhook the default menu from the textbox and hook up the custom menu.
(I'm not aware of other APIs which only let you disable/hide items in the existing context menu)
The downside off course is that any menu item you want to keep such as copy or delete you need to write the code for yourself.
You can find a very good explenation on how to do this here Disable right click menu inside a textbox and here Weird reaction on a popup menu
What next,.. what is if the user uses CTRL+V to paste? Or what if the user has paste mapped to different key combinations, other than CTRL+V?
Validate the data instead?
You can end up writing a lot of code trying to prevent data entry. Why not save that work and instead let the user enter what they like and use the validate event to validate the data?
I wrote a sample on another site on using the validate event of a textbox here: Validate Value Is Numeric. That link also has a vb6 demo project I put together attached in the post.
The type of validation is irrelevant I suppose, it simply demonstrates using the validate event allows you to focus on validating the data, rather than trying to code every single possible way you can think of trying to prevent data to be entered in the first place.
The Validate event is triggered before lostfocus and before the next control's getfocus.
Only if the validate event is not told to cancel the action, will the lostfocus event and any subsequent event be executed.
The Validate event is intended to be used to ensure the control's value is validated before executing any other event.
Private Sub Text1_Change()
If Text1.Tag <> Text1.Text Then
Text1.Text = Text1.Tag
Text1.SelStart = Len(Text1.Text)
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Text1.Tag = Text1.Text & Chr(KeyAscii)
End Sub
Old problem but still relevant with legacy projects.
The solution proposed by Sam Sylvester is interesting but doesn't allow you to delete characters (use backspace) and appends the 22 ASCII character (Ctrl + V) in the textbox (which is bad).
This solution I found worked for me (prevents paste using Ctrl + V and also using the context menu):
Dim preventPasteFlag As Boolean
Private Sub Text1_Change()
If preventPasteFlag Then
Text1.Text = Text1.Tag
preventPasteFlag = False
Else
Text1.Tag = Text1.Text
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 22 Then
preventPasteFlag = True
End If
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
preventPasteFlag = True
End If
End Sub
this is quite similar:
Private Sub Text1_Change()
Dim i As Long, sTemp As String
For i = 1 To Len(Text1)
If InStr(1, "0123456789", Mid$(Text1, i, 1)) > 0 Then sTemp = sTemp & Mid$(Text1, i, 1)
Next
Text1 = sTemp
End Sub

Resources