Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Prevent Focus Change on Tab Stroke

Posted on 1999-07-14
17
Medium Priority
?
375 Views
Last Modified: 2012-08-14
Hi guys,

I wnat to prevent the Focus Change (to get the KeyDown Event) by stroking the Tab Key. When My UserControl is laying alone on a Form I`m able to catch the
UserControl_KeyDown Event even with the Tab Key, but
if there an other Control on the Form which the property
"TabStop" is set on true, the KeyDown Evnent isn`t Raised.
I also tried to Get the WM_KEYDOWN Window Message (0x100)
with using SetWindowLong on my own WindpwProc, but i cant get the Message in that cases i wrote above.
0
Comment
Question by:mkodragon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 2
  • +2
17 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524660
No solution, but a workaround perhaps. Stick a few controls that can get focus before and after your control. Give them a left property negative enough so they can't be seen.

Example, let's say this is the tab order:

C1
C2
YourControl
C3
C4

Set the left property of C1-4 to a big negative value. But don't make them invisible. They should still be active controls and technically "visible" with a tab stop.

If C1 gets focus, the user tabbed into it from the previous control in the tab order. Set focus to your control. To the user it will seem that they just tabbed straight into your control.
If C2 gets focus, the user tried to tab out of your control by hitting Shift+Tab. Set focus back to your control
If C3 gets focus, the user tried to tab out of your control, set focus back to your control
If C4 gets focus the user hit Shift + tab on the next control, set focus to your control so it will look to the user as if they simply tabbed into your control.

I know it is a horrible workaround but it works.
0
 
LVL 1

Expert Comment

by:prozak
ID: 1524661
I believe that in the KeyDown event, if you set the KeyAscii code to 0 that it will essentially ignore the key (like they never even hit tab).

Private Sub Control_KeyDown(KeyAscii As Integer)
  If KeyAscii = vbKeyTab Then
    KeyAscii = 0
  End If
End Sub

I'm not positive but I seem to remember doing this at one time.
 
0
 
LVL 1

Expert Comment

by:prozak
ID: 1524662
Wait a minute I just re-read your question and realized that the problem is that KeyDown is not firing. Never mind.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:Vbmaster
ID: 1524663
When the Usercontrol looses Focus isn't the LostFocus event fired?
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524664
Probably, yes, but the problem is that if the user control loses focus, some other control gets the focus. And it may have all sorts of code in there that you do NOT want to execute.
By the time you've get the focus back on to the user control, the damage may be done.
0
 

Author Comment

by:mkodragon
ID: 1524665
It also doesn`t work whith the workaroud with the internal
structure of my UserControl.

It´s also funny that i doesn`t get the LostFocus and
GetFocus of my UserControl (Maybe wrong settings?!?!)

I wrote two function in combination with an Private Collection.
One switches the TabStop of all Controls on the
UserControl`s Parent Form to false, storing the
Controlname and old TabStop setting into the
Collection.
The otherone is doing the otherwise round.
But without the Got/LostFocus events .....

Info: There are  a FlexGrid, 3 Textboxes (Indexed) and
      3 Images in my UserControl
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1524666
Set the causesvalidation property to true.

Now use this code

Private Sub Text1_Validate(Cancel As Boolean)
    If (CaseInWhichIDontWantToLooseFocus) Then
        Cancel = True
    End If
End Sub

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524667
:-) And what if mkodragon hasn't got VB6?
0
 

Author Comment

by:mkodragon
ID: 1524668
Hi,

caraf_g is right.

I don`t use VB6, I still using VB5.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524669
You'll have to reject mirkwood's answer then. Was my suggestion any use to you?
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1524670
How can you smell he has not VB6?
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524671
I'm psychic. No... It's just my "standard" reaction when someone suggests a VB6-specific solution: most people still work with VB5, not to speak of the poor suckers who are stuck in VB4 or even worse...

Which of course highlights the fact that it is always a good point to state the version of VB you're working with when you're asking a question <scrambles off to update the text of his latest question now>
0
 

Author Comment

by:mkodragon
ID: 1524672
Hi caraf_g

I`ve tried it, but it didn`t work proper.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1524673
0
 

Author Comment

by:mkodragon
ID: 1524674
hi caraf_G

Just give a new answer, so that i can grade your answer

I can use that!

cu mkodragon
0
 
LVL 10

Accepted Solution

by:
caraf_g earned 800 total points
ID: 1524675
Thanks
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2096563
anyone looking at this question please be advised that my homepage is has moved from domain homepage.tinet.ie to homepage.eircom.net
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question