VB 2010: Shortcut in desktop - visual-studio-2010

How can I do a shortcut for a buttons so I can press it if I am in my desktop? I've already tried:
If e.Keycode = Keys.F2 Then
Stopclick.PerformClick()
End if
End Sub

I'm guessing that you mean you want to capture a key press when you minimize to your desktop or use a different program. The term that may help you out in your search is called Global Hotkeys. Basically what you need to do is register a hotkey globally within Windows so that when your application is minimized or no longer has focus, it is still able to intercept the messages.
You can search Google for '.Net Global Hotkeys'. You can also check out this link: http://www.vbforums.com/showthread.php?672702-Register-Global-HotKeys
If you are simply trying to catch the F2 event on your form, then try the following code:
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData as Windows.Forms.Keys) As Boolean
If keyData = Keys.F5 then
'Take action here
End If
Return MyBase.ProcessCMDKey(msg, keyData)
End Function

Related

Is it possible to prohibit pasting the clipboard's textual content into a TextBox control?

If the user tries to paste copied text via Ctrl-V, I can invoke KeyPress:
Private Sub cTextBox_KeyPress(KeyAscii As Integer)
If KeyAscii = 22 Then '22 is Ctrl-V.
Beep
KeyAscii = 0
End If
End Sub
But what do I do when she uses the (admittedly less common) Shift-Insert combination? KeyPress won't receive this key. KeyDown and KeyUp don't let us cancel the input.
Is sub-classing the only possibility?
And what about the option 'Paste' in the popup menu? Can I get rid of it by setting a windows style in SetWindowLong? Which?
Perhaps you could set a check in the textbox's Change event to compare to clipboard contents. If they match, disallow the change (i.e., set to prior value, default, blank, etc.)
The advantage of this approach is that it is independent of the numerous methods of pasting clipboard content.

How do I prevent a double-click on an Open File dialog from registering a click on the form under it?

I have a custom control that's essentially a drawing canvas, and a program that uses it for editing files. When a new file is opened, though, something very strange can happen.
If the user double-clicks on the file in the Open File dialog (standard TOpenDialog control) instead of selecting an item and hitting ENTER, the canvas underneath registers a click event and ends up executing a draw action at the position of the cursor immediately after loading is complete.
Obviously this is not the intended behavior for this. I've noticed before that when you double-click the mouse, the double-click message arrives before the second click message. I think the dialog box might be closing from the double-click, and then the second click message arrives and gets sent to whatever's at the appropriate coordinates now that it's gone.
Is there any way I can make this stop happening? I can't tell my code "after loading, just eat the next click," because it could have been opened with the 'ENTER' key instead, and then it would miss the first legitimate click. Can anyone think of a better way to handle this? (Using Windows 7, in case it makes a difference.)
If there's a "second click message," there's something wrong. (For one thing, Windows doesn't have "click" messages, just mouse-up and mouse-down messages.) A double click goes like this: mouse down, mouse up, double click, mouse up. The dialog disappears between the double-click message and the second mouse-up message. If your control receives the mouse-up message and treats it as a full click, then that explains the issue and you need to stop; a click is always a pair of mouse-down and mouse-up messages. If you haven't gotten both, then it's not a click.
In fact, it is The 2nd mouse-up event got fired on the picture box, which leads to the event handler invocation. This seems an OpenFileDialog bug. Need to add check for the IsMouseCaptured for the mouse up event, one click is mouse down and mouse up, instead of only a mouse up.
I solved it that way (it is of course an work-around):
CFileDialog my_file_dialog(...);
if ( my_file_dialog.DoModal()!=IDOK )
return;
CString fileName= my_file_dialog.GetPathName();
//...
CSelectItemsDlg dlg;
// Avoid that the double-click on the CFileDialog sends the WM_LBUTTONUP message to the next window causing the Unselect of an item that is under the mouse cursor.
// http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_10287063.html#a2476475
MSG msg;
while(PeekMessage(&msg,0,WM_LBUTTONUP,WM_LBUTTONUP,PM_REMOVE));
int DoModalRes = dlg.DoModal();
And you can believe that I have put a big smile on the face of my boss :)
Private IsMouseDown As Boolean
Private Sub picNenIn_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles picNenIn.MouseDown
IsMouseDown = True
'Code
End Sub
Private Sub picNenIn_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles picNenIn.MouseMove
If IsMouseDown Then
'Code
End If
End Sub
Private Sub picNenIn_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles picNenIn.MouseUp
IsMouseDown = False
'Code
End Sub

Combobox from Visual Studio Macros

When you need to debug a Website that hosts on IIS Express, you usually don't restart it all over again, every time when you need to rebuilt your code. You just attach VS to the process. And the macros script helps a lot:
Public Module AttachToProcess
Public Sub AttachToWebServer()
Dim attached As Boolean = False
Dim proc As EnvDTE.Process
For Each proc In DTE.Debugger.LocalProcesses
If (Right(proc.Name, 14) = "iisexpress.exe") Then
proc.Attach()
attached = True
Exit For
End If
Next
If attached = False Then
MsgBox("iisexpress.exe is not running")
End If
End Sub
End Module
You can assign a keystroke and voila. The only problem is if your solution contains more than one webapp, there would be more than one iisexpress.exe processes with different PIDs, and VS would sometimes choose the wrong one.
The question: Is it possible to popup a dialog if there is more than one iisexpress.exe running to choose the right one?
Of course you can always use default 'Attach To Proccess' dialog, but it's not gonna be as quick as using that script and keyboard shortcut.
You can open a dialog, but it's not the most straightforward thing. You will need to put all your UI code into the macro, EG layout, size of controls, etc...
It's about 200ish lines of code, and rather than put it all here I will defer you to my blog at http://www.brianschmitt.com/2010/09/save-and-change-tool-layout-in-visual.html
You should be able to reuse the View Switcher dialog box and list out all instances of IISExpress. It shouldn't take much to do what you need it to.

VB6 KeyDown Fails to Cancel the Keypress

I'm trying to detect when the user presses certain keys in a text box and then I want to increase or decrease the value in the text according to their keypress and cancel out the key they pressed. The cancel part isn't working. I'm accustomed to just putting in KeyCode = 0 if I want to cancel their keypress, like I do in MS Access. However, this isn't working for me in VB6. The plus sign gets into the textbox.
Any suggestions?
I'd say ignore the KeyDown event and just use the KeyPress event:
Private Sub RTB_KeyPress(KeyAscii As Integer)
' "Cancel" the keystroke
KeyAscii = 0
End Sub
The best way to do what you're doing, though, would be to set your form's KeyPreview property to True, and then add a handler for the form's KeyPress event - this means the form gets a chance to handle any keystrokes first. By handling it here and cancelling as above, you can change the text in the textbox however you like.
I used Visual Basic for the first 10 years of my career, but I had to google for the above code. I recommend at least moving on to VB.NET - sometimes you can even import VB6 projects into .NET (sometimes).
Private Sub Text1_KeyUp(KeyCode As MSForms.ReturnInteger, Shift As Integer)
If KeyCode = 27 Then
'CONDITION
End If
End Sub

Disable HotKeys When Typing - VB6

In my VB6 program, I have tons of hotkeys such as X, A, D... ETC . I also have a chat system in it, where everytime I use the characters X or A it will do the actions of those hotkeys. For example, if X was to close the application (not that it really does), when I am typing "fiXing" into my chat textbox, it will close the application. Can anyone tell me how to disable the hotkeys when typing EXCEPT the Enter Key?
thanks,
Kevin
In the chat TextBox's GotFocus event set a flag to disable your hotkeys. Then re-enable them in the TextBox's LostFocus event.
I don't know how you trap your hotkeys, but the code to set the flag is pretty simple:
Private suppressHotkeys As Boolean
Private Sub txtChat_GotFocus()
suppressHotkeys = True
End Sub
Private Sub txtChat_LostFocus()
suppressHotkeys = False
End Sub
Then in the code that traps the hotkeys, just check the flag:
If (Not suppressHotkeys) Then
//process hotkey
End If
It would probably be better to use a key combination for your hot keys. It is more common to press say Ctrl+X or Alt+X. You would test for them in either the KeyDown or KeyUp events.
If KeyCode = vbKeyX And (Shift And vbCtrlMask = vbCtrlMask) Then
' Do something
End If

Resources