Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form does not have focus 1

Status
Not open for further replies.

SkennyR

Programmer
Mar 7, 2004
157
US
I have a program that does some behind the scenes things, then displays a form.
I have form's key preview set to true, maximised, control box false. The form should and does fill up the whole screen. I have windows taskbar set to autohide, so all I see on the screen is my form.
The form expects user to press ENTER key to do its thing.
Problem is that form displays how I want it to, but if there is another program in the taskbar, if I press ENTER, that other program sees the keypress. I have to mouse click on the form and then it will see the keypress.
I have searched for a way to give the form focus, but I cant find it. Me.SetFocus gives me an error.
Appreciate any help..
TIA.
 
Have you tried setting the focus in the From.Paint event?

Private Sub Form_Paint()
Me.SetFocus
End Sub
 
That doesnt work either. On this form, I have a label with a number counting down 1 every second. User must hit enter before the number reaches zero.
I added your code with a message box, just to be sure that your code was getting hit:

Private Sub Form_Paint()
Me.SetFocus
msgbox "ok"
End Sub

But the messagebox kept popping up every second.
So i took above code back out.
It wont let me setfocus to the label or the form.
It gives me an invalid procedure error.
I am coding this correctly arent i?
Me.SetFocus
I have also tried
Me.show
Me.SetFocus
It works as long as no other program is in the taskbar. (without the Me.SetFocus command).
I am compiling to an exe file before I run it.
It seems to do right when running from VB6 program.
Thanks for your help...
 
You need to put Me.SetFocus in the Form_Activate event, not the Form_Load

[gray]Experience is something you don't get until just after you need it.[/gray]
 
No, didnt work. I changed Form_Load to Form_Activate.
I tried the Me.SetFocus at the beginning of the sub and at the end of it, same result.
I dont get the error now, but I still have to click on the displayed form before it sees my key press.
 
You use SetFocus to change the focus from one form in your app to another, not to grab focus from another application and put it on the VB app. So it doesn't surprise me that it doesn't do anything, since SkennyR only has one form.

Now, I'm not able to create the error consistently. I put up a form, add a command button, set its "default" property to true (which will make the button push when you hit the enter key) and add a message box to the command button's click event handler. I also set the WindowState property to vbMaximized. I compiled this, and ran it, and it worked. I then set the autohide property on the taskbar to true, and the form had lost focus, which is understandable since I clicked on the taskbar to set its property. I then closed the program and reran it, and it worked fine.

Can you repeat the error with a new project that has no other functionality? If not, you have learned that the error is in some dark corner of your code that has escaped your notice.

HTH

Bob
 
Thanks Bob.
Im pretty sure I have everything set as you do above, execept for the command button.
Im using Form_Keypress to get the keypress, because the user can pick which key to press in a setup portion of this program.
So I would say you are most likely correct about the dark corner of my code. That would not surprise me a bit.
I will look into the rest of the code to see if I can find anything, it's just hard to find something when you dont know what youre looking for...
 
Here's all the code I'm using now:
Code:
Option Explicit

Private Sub Command1_Click()
MsgBox "successful"
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
MsgBox KeyAscii
End Sub
with keypreview and windowstate set on the form. It works. What other code do you have?

Bob
 
Oh, and by the way, just out of curiosity, which key are you setting up as the key to press in your tests?
 
Thanks,
I am not at my VB6 computer right now, but maybe tonight I can post some of my code.
I have tried using enter and spacebar, both act the same..
 
I'm no expert, but if I am understanding the issue correctly, couldn't you just go the command button route rather than keypress, set visible = false and then setfocus to the command button?
 
That would limit the cancel key press to enter only, is that correct?
I want the user to be able to decide which key (s)he presses to cancel the countdown.
Thanks..
 
Enter or Space. Once a button has Focus either of those keys will trigger it.
 
Ok, I have found the problem, and Bob, once again you are correct. It was in my programming.
Here's the problem:
I want to hide the mouse cursor when my "countdown" form is displayed. Here is the code I used for that:

Private Declare Function ShowCursor Lib "user32" _
(ByVal bShow As Long) As Long
Dim blnShow As Boolean

Then to turn off the mouse cursor:

ShowCursor False

If I take out the ShowCursor False command, the form does what I want it to.

But now, how do I get rid of the mouse cursor AND keep focus on my form??
Thanks for everyone's help thus far.
 
Perhaps you can just set the position of the cursor to one slightly off the screen ? so it is active, but not visible ?
 
Um, I'd recommend that you revisit this idea entirely. You might want to take a bit of a look at this link: (the page only has a link to Amazon to buy the book, but you can actually browse the book there by using the tree in the left hand column). Your solution isn't consistent with Microsoft UI guidelines. For example, someone with bad eyes may be in the habit of using the mouse all the time, and may find it an imposition to not have it.

I would suggest that the functionality that you have with pressing a key ought to be selectable with a mouse click as well. I wouldn't ever present (well, at least, I never have presented) the user with a form that doesn't have mouse interaction capability in it.

Now, that said, there may be a good reason for it. Why are you trying not to show the mouse?

Bob
 
Thanks to both you guys.

jzr: that sounds like a good option, hide the mouse cursor off screen, if someone wants to use the mouse they can just move the cursor back on screen. Now how do I do that?
:)

Bob: What i am doing is building a program that when the machine is turned on, loads a "front end" menu for an arcade machine, then when the user exits the front end, the program picks back up and counts down from a user selected time. If the user presses a user-assigned cancel button within that time, the program ends and the user is back in windows.
If the countdown reaches zero, then the machine shuts down.
I do like the idea of also being able to have the mouse-click cancel it too.
This is for my home arcade machine, which doesnt normally have the mouse or keyboard connected.
This will allow me to connect the mouse and keyboard, exit the frontend, then cancel the shutdown to do maintenace on windows or PC.
I am finished with it (except maybe adding the mouse click), but the part about the form losing focus has really been a pain.
Thanks again for everyone's help and paitence.
 
Well ... you can easily set a form to appear off the screen, so I though it would be easy to set the cursor position there.
There is an API call of SetCursorPos, which allows you to set the cursor position wherever you want, although I see it says it won't go outside the screen - so - no help here !
 
Thanks jzr,
I will search this forum for that api.
Just moving the mouse cursor to one corner of the screen would be better than it's current default, right in the center.
 
API Explanation:
SetCursorPos sets the position of the mouse cursor. If you try to set the coordinates outside of the range of the display (for example, to (700,40) on a 640x480 display) or outside the confining rectangle (set by ClipCursor), the cursor will just go to the edge of the screen or the rectangle. The function returns 0 if an error occured, or 1 if successful.
Parameter Information:
Declare Function SetCursorPos Lib "user32.dll" _
(ByVal x As Long, ByVal y As Long) As Long

x
The x coordinate to move the cursor to.
y
The y coordinate to move the cursor to.

Code:

' Move the mouse cursor to the point (100,200) on the screen
Dim retval As Long ' return value

retval = SetCursorPos(100, 200) ' move the cursor to (100,200)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top