Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact
A1VBCode Forums
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      

Home » Visual Basic (VB 4/5/6) » General Visual Basic » Remove Focus From Only CommandButton On Form?...


Remove Focus From Only CommandButton On Form?...Expand / Collapse
Author
Message
Posted 5/15/2008 1:36:42 PM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: Today @ 9:47:00 AM
Posts: 278, Visits: 1,426
I think we all know that KeyPreview will do nothing for you if the only control on your Form is a CommandButton. At least as far as KeyCode's for Arrow keys & the Enter Key are concerned. That's why I prefer to put animation code in PictureBox events. My question is, is there an undocumented trick to get the focus off of the CommandButton? Just in case what I'm asking is not clear, refer to this simple code that has the me.KeyPreview=True but has only a CommandButton on the Form. This code will never fire. Btw, I read the KeyPreview property info in the library and the note at the bottom regarding this issue and it does not mention a work around.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyLeft Then
      Shape1.Left = Shape1.Left - 100
   End If
End Sub

Private Sub Command1_Click()
   ' Do nothing. I'm here just to make a point.
End Sub

Please don't respond with the obvious, like "Set the Button's Visible or Enabled property to False".

 

"So much to learn. So little time to do it. Wise men know it's later than one thinks"!

***Vote here to make Keith a Moderator***

http://www.a1vbcode.com/vbforums/FindPost23630.aspx

Post #24928
Posted 5/15/2008 2:43:35 PM
Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 6:57:34 PM
Posts: 872, Visits: 4,801
This is a real hack and it probably falls into the don't respond with the obvious category but here you go.
Option Explicit

Private Sub Command1_Click()
    MsgBox "I work!" & vbCrLf & "Now press the left arrow key."
    Picture1.SetFocus
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyLeft Then
      Shape1.Left = Shape1.Left - 100
   End If
End Sub


Private Sub Form_Load()
    Picture1.Move -100, 0, 1, 1
    Me.Show
End Sub
Post #24930
Posted 5/15/2008 5:39:53 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 8:00:49 AM
Posts: 1,376, Visits: 3,098
Apart from the Shape shown how many other controls are there on the Form?

You could always set the Command button TabStop to False. That way you would have to physically press the button to make it work.

I've just put a Shape and Command button (with the TabStop to False) on a Form with Me.KeyPreview = True and the Shape wouldn't move. I put a PictureBox on the Form then it would move. If I made the PictureBox not visible it wouldn't move again, strange. Now a Shape doesn't have a TabStop so the tab key wouldn't work.

Ok a search on VBforums.

Focus Rect

How to prevent Command btn from taking focus?

Focus Of Command Buttons

Keith

http://www.martin2k.co.uk/forums/

I've been programming with VB for 12 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
Post #24932
Posted 5/15/2008 10:36:10 PM


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: Today @ 9:47:00 AM
Posts: 278, Visits: 1,426
Keithuk (5/15/2008)
Apart from the Shape shown how many other controls are there on the Form?

Keith, like I said, only the CommandButton. That's why Mark's code doesn't quite answer the question. This code isn't part of a project. It's simply an issue that I was aware of but often wondered if there was a quick work around that I was missing. This issue won't manifest itself if there's another control (not a CommandButton or ListBox) on the Form because they won't overide the KeyPreview property of the Form. I was not surprised that the links you posted point to API,s as solution though.

I will continue to put my graphics in a PicBox instead of the Form because it eliminates this whole issue entirely as I illuded to in this post.

http://www.a1vbcode.com/vbforums/FindPost24875.aspx

It was this thread that got me thinking about this issue in the first place and why I told Jenna not to use the Form.

Thanks for the effort guys. 

 

"So much to learn. So little time to do it. Wise men know it's later than one thinks"!

***Vote here to make Keith a Moderator***

http://www.a1vbcode.com/vbforums/FindPost23630.aspx

Post #24933
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 2 (2 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Brian, Peter

PermissionsExpand / Collapse

All times are GMT -5:00, Time now is 7:32pm