Disabling Mouse Right Click On Image Control VB6 - 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.

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

VB6 Listbox Right click

So i'm working on a game. Actually an mmorpg. And right now, I have a friends list, in the form of a listbox. Now what i would like to do is set it up so that if i right click a line in the listbox, it will ask me yes or no, and if yes it will warp me to that player. If the line is blank it wont have the popup asking yes or no.
And to help, the warp command is WarpMeTo Trim$()
For an example, if i had it warp me to something that was in a textbox it would be this WarpMeTo Trim$(textbox.text)
and yes, i do use the above for warping administrators to other players, where you input the name.
Actually, If anyone could just tell me how to "get" the name that is on that line in the listbox, that would be awesome, because I'm thinking of making a totally separate menu pop up when you click a name.
This can be split into 2 parts
Make the listbox select an item on a right click
Show the item text only when it was right clicked
When you right click on an item in a listbox, the item isn't selected
To select the item you will have to simulate the left click via the mouse_event() API
After that you can use the _Click() event to show the item, but you will have to make sure the item was right clicked and not left clicked
'1 form with:
' 1 listbox: name=List1
Option Explicit
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Private mblnRightClick As Boolean
Private Sub Form_Load()
Dim intIndex As Integer
'initialize no right click
mblnRightClick = False
'fill the listbox with some data
List1.Clear
For intIndex = 1 To 10
List1.AddItem CStr(intIndex)
Next intIndex
End Sub
Private Sub Form_Resize()
List1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub
Private Sub List1_Click()
If mblnRightClick Then
'process simulated left click
MsgBox List1.Text
'release right click simulation
mblnRightClick = False
End If
End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
'simulate left click
mouse_event MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, X, Y, 0, 0
mblnRightClick = True
End If
End Sub
If you select an item at once, you can put the following code on Menu Popup event:
listBox1.SelectedItem.ToString()
If you want to check all the selected items on listbox (multi select mode), you can use the following code:
Dim k As Long
Dim s As String
For k = 0 To listBox1.ListCount - 1
If listBox1.Selected(k) Then
s = listBox1.List(k)
' ... do something
End If
Next
As the other answers show, the problem is that the listbox doesn't really do what you need. It only changes the selected item on a left click. So you could have the user left clicks on their friend, then right clicks anywhere on the control to warp to that player. BUt that's going to lead to a lot of unexpected arrivals.
You could use coordinates to work out which name had been right clicked, without worrying about it being selected, something like this
Private Sub List_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim IndexRightClicked As Long
If Button = vbRightButton Then
IndexRightClicked = X + Y
WarpMeTo Trim$(List.ItemData(IndexRightClicked))
End If
End Sub
Where X = Y is replaced by maths, knowing the size of items in your list (and correcting for scrolling).
The best option though, I think, is for you to create a user control for the friends list. This will be more work, but means you can create something with the functionality that a good friends list is going to need. Then you could fairly easily use control arrays of CommandButtons or Labels (both can have appearance customized a lot) which are scrolled etc as appropriate.

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

How to navigate using WebBrowser Control

How to navigate using WebBrowser Control? I want to open a C:\file1.html... an example code would be appreciated.
The VB code you have is correct, that is an error with the javascript on the page you are loading, so you can use:
Private Sub Command1_Click()
WebBrowser1.Navigate2 "C:\file1.html"
End Sub
But in Form_Load set silent mode, eg:
Private Sub Form_Load()
WebBrowser1.silent = true
End Sub
i first put a webcomponent as mentioned in the post "How do i open a webpage" in vb6 by adatapost
Private Sub Command1_Click()
WebBrowser1.Navigate2 "C:\file1.html"
End Sub
this works but as u click on the button it pops a error..A error has occured on the scirpt of the page , line 6, Error: object expected, url: file:///C:file1/index.html, do u want to continue , yes or NO............................how to come around this error. ??

Excel VBA - Pause Events while Updating ComboBox

I have an application written in VBA for Excel that takes in a live data feed. Whenever there is a change in the data, various events are triggered within VBA.
I also have some UserForms with ComboBoxes on them. My problem is that when I click the down arrow on the ComboBox and try to make a selection, the moment I get an update from the data feed, the ComboBox resets. What I would like to do is pause the events while I am making my selection in the ComboBox and then unpause it when I am done. How do I generate this functionality?
Try this to turn off:
application.enableevents = false
And this to turn back on:
application.enableevents = true
To pause and show a message and to keep working on something during the pause. finally press button
Public Ready As Boolean
Private Sub Command1_Click()
Ready = True
End Sub
Private Sub Form_Load()
Me.Show
Ready = False
Call Wait
Label1.Visible = True
End Sub
Public Function Wait()
Do While Ready = False
DoEvents
Loop
End Function
Maybe you can put a flag to bypass the update event on your combobox until a selection is made.
Private bLock as boolean ' declare at module level
' When a user clicks on the combobox
Private Sub DropDownArrow_Click() ' or cboComboBox_Click()
bLocked = True
End Sub
' This procedure is the one that does the updating from the data source.
' If the flag is set, do not touch the comboboxes.
Private Sub subUpdateComboBoxes()
If Not bLocked then
' Update the comboboxes
End If
End Sub
' When the selection is made, or the focus changes from the combobox.
' check if a selection is made and reset the flag.
Private Sub cboComboBox_AfterUpdate() ' Or LostFucus or something else
if Format(cboComboBox.Value) <> vbNullString Then
bLocked = False
End If
End Sub
Hope that helps.

Resources