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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get focus on forms

Status
Not open for further replies.

harjitgill

IS-IT--Management
Jul 11, 2003
22
0
0
GB

Hello All,

I am a newbie to VB and what i am trying to do would be simple for you but hard for me. what i am trying to do is create a form that looks like a message box with a OK button on it. I have created the form and button but all i want is to have the keyboard disabled so the user has to click the button. I have had a good look in the properties of the form and found nothing that resembles or looks like anything to do with the disabling the KB. Thanks for your help in advance
 
It sounds like you want your form to be Modal over the form that loads it, and perhaps over the entire application.
 
Hello Sheco,

I don't know what you mean by modal? I only want to know how to stop people from pressing the return, spacebar and Esc keys on the keyboard and they have to click the OK button
 
Sorry, i misunderstood.

You want to make it so that your program locks up on computers without a mouse or other pointing device?
 
Hello Sheco,

Well i want the user to just press the OK button on the screen and stop them from using the keyboard. What i mean is that stop them from pressing the spacebar, return keys to terminate the program, so they are forced to click the OK button
 
What if the OK button has focus... must they actually click it with a pointer or would it be ok to click the Enter key on the keyboard?
 
Create a subroutine for the Form's KeyPress Event and kill all keyboard inputs. Like this

Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then ' Enter Key
        KeyAscii = 0
    End If
End Sub

This should disable all keyboard activity on the form.

HTH



AMACycle

American Motorcyclist Association
 
I have tried your code and it does not work for me. You are right that all i want is to disable the keyboard and force the user to click the button
 
Well the code does not work for me. All i want is the user to not use the keyboard and just click the button to terminate the application. So to stop them from using the spacebar and the return keys to terminate the application.
 
And what happens if the user has no pointing device? or the one that they do have is broken?

Almost every program can be used with keyboard only, why not allow the user to click tab until the OK button is selected (or have it selected by default) and then hit the Enter key?
 
Try this ... change the Keypress routine to the BUTTON'S Keypress routine:

Code:
Private Sub cmdButtonName_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then ' Enter Key
        KeyAscii = 0
    End If
End Sub

HTH



AMACycle

American Motorcyclist Association
 
I wouldn't like any program that REQUIRED me to use the mouse.

-David
2006 Microsoft Valueable Professional (MVP)
 
<This should disable all keyboard activity on the form.
Not quite...only keys that have an ascii value associated with them. To do that, use the KeyDown event.

Also, if KeyPreview is False, its default value, this will have no effect, which is probably why the OP can't get it to work.

All that said, disabling all keyboard input is contrary to established software architecture practices. I hope you don't intend to distribute your application to any blind people, for example.

Bob
 
<This should disable all keyboard activity on the form.
Not quite...only keys that have an ascii value associated with them. To do that, use the KeyDown event.

Also, if KeyPreview is False, its default value, this will have no effect, which is probably why the OP can't get it to work.

All that said, disabling all keyboard input is contrary to established software architecture practices. I hope you don't intend to distribute your application to any blind people, for example.

Now, I suspect that the reason that you're doing this is because you're not getting your Ok button code to run when the user leaves the form. The way that you fix that is to put the code in the unload event (or queryunload event if you want to control the means by which the user unloads), and then just say unload me in your ok button.

HTH

Bob
 
Many thanks for all your help and i have to admit defeat on this project. My boss wanted a popup box which says (Please secure your working area before you cease work)and a OK button to click on to, to acknolage that you have accepted the message. The messagebox will be displayed when the user shuts down the PC. Many thanks again
 
Doesn't sound too difficult. Why are you giving up on it?
 
Just lock the workstation:

Code:
Option Explicit

Private Declare Function LockWorkStation Lib "user32.dll" () As Long

Private Sub Command1_Click()
    LockWorkStation
End Sub

-David
2006 Microsoft Valueable Professional (MVP)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top