Solved

Focus problem

Posted on 2000-03-01
14
176 Views
Last Modified: 2010-05-18
I have an array of txtBoxes named(txtFact) and a HScrollBar and some others controls on my form.
I want to execute the HScrollBar.GotFocus when the user click on the HScrollBar, but it doesn't work.
I made a test, i put a watchPoint: Form1.ActiveControl.Name and the Result is TxtFact.
Then why when i click on the HScrollBar the sub GotFocus is not execute.

Guys, go to see the : Erick37's comment March 01 2000 - 02:59PM EST  it is very good.
0
Comment
Question by:dosyl
  • 7
  • 5
  • 2
14 Comments
 

Expert Comment

by:jhedlund
ID: 2573969
This works fine for me.  I put a text box and a HScrollBar on a form, and in their _GotFocus events I put the following code:

       Debug.Print "ActiveControl: " & Me.ActiveControl.Name

And it properly identified the text box when it had focus, and the HScrollBar when it had focus.

Can you give a little more information ?  What version VB?  Try what I did, and see what results you get...
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 100 total points
ID: 2573981
Is the ScrollBar's TabStop property set to True?
0
 
LVL 1

Author Comment

by:dosyl
ID: 2574002
Erick37, that's it, i don't remember why i put it to False; but it is possible to do the GotFocus sub with the TabStop=False? If Yes how?
0
 
LVL 1

Author Comment

by:dosyl
ID: 2574029
I remember why I put the TabStop's property; it is because i don't like to see the HScrollBar flashed.
0
 

Expert Comment

by:jhedlund
ID: 2574031
You can add the following line to HScrollBar_Change event:

        HScrollBar.SetFocus

0
 
LVL 32

Expert Comment

by:Erick37
ID: 2574040
It may be possible using subclassing to do this.  Let me see...
0
 
LVL 1

Author Comment

by:dosyl
ID: 2574041
I remember why I put the TabStop's property; it is because i don't like to see the HScrollBar flashed.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:Erick37
ID: 2574108
I can post the code for a scrollbar hook that intercepts the left mouse button click event.  This will give you a Click event for the scrollbar.  You may be able to replace the GotFocus with Click.

If you need to do this for many controls, the coding can get a bit complicated.

If you cannot live with the flashing, let me know.
0
 
LVL 1

Author Comment

by:dosyl
ID: 2574159
Erick37, Scuse but i don't understand there is no Click Event or MouseSomething Event in a HScrollBar.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2574225
No, but you can make one.  This creates a Click method for the HScroll1 scrollbar:

'~~~~FORM CODE~~~~
Option Explicit

Private Sub Form_Load()
'
' Hook into the message stream.
'
glPrevWndProc = fSubClass(HScroll1.hwnd)
End Sub

Private Sub Form_Unload(Cancel As Integer)
'
' Stop intercepting messages.
'
Call pUnSubClass(HScroll1.hwnd)
End Sub

Public Sub HScroll_Click(ByVal wParam As Long, ByVal lParam As Long)
    Debug.Print "HScroll Click " & Hex(lParam)
End Sub

'~~~~MODULE CODE~~~~
'MUST be in a module (bas file)
'Warning: since subclassing is used, DO NOT
'pause the code or add breakpoints while running
'this code in the VB IDE.

Option Explicit
Public glPrevWndProc As Long

Public Const GWL_WNDPROC = (-4)
Public Const WM_LBUTTONDOWN = &H201

'The SetWindowLong function changes an attribute of the specified window.
'Calling SetWindowLong with the GWL_WNDPROC index creates a subclass of
'the window class used to create the window. An application should not
'subclass a window created by another process. The SetWindowLong function
'creates the window subclass by changing the window procedure associated
'with a particular window, causing Windows to call the new window procedure
'instead of the previous one. An application must pass any messages not
'processed by the new window procedure to the previous window procedure by
'calling CallWindowProc. This allows the application to create a chain of
'window procedures.
'
'Parameters
'hwnd - Identifies the window to which the window belongs.
'
'nIndex - Set to GWL_WNDPROC to set a new address for the window procedure.
'
'dwNewLong - Address of the new window procedure.
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

'The CallWindowProc function passes message information to the specified window
'procedure. Use this function for window subclassing. Usually, all windows
'with the same class share one window procedure. A subclass is a window or
'set of windows with the same class whose messages are intercepted and processed
'by another window procedure before being passed to the window procedure of the class.
'
'Parameters:
'lpPrevWndFunc - Pointer to the previous window procedure.
'                If this value is obtained by calling the GetWindowLong function
'                with the nIndex parameter set to GWL_WNDPROC, it is the address
'                of a window or a handle representing that address.
'
'hwnd - Identifies the window procedure to receive the message.
'
'Msg - Specifies the message.
'
'wParam - Depends on the value of the Msg parameter.
'
'lParam - Depends on the value of the Msg parameter.
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
    (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Sub pUnSubClass(lhWnd As Long)
'
' Stops the form from intercepting Window's messages
' by resetting the default procedures associated
' with the form.
'
Call SetWindowLong(lhWnd, GWL_WNDPROC, glPrevWndProc)
End Sub


Public Function pMyWindowProc(ByVal hw As Long, ByVal uMsg As Long, _
    ByVal wParam As Long, ByVal lParam As Long) As Long
'
' This is the callback procedure used when a message is received
' by this form.  The desired message is processed and all others are
' passed back to the original procedure associated with the form.
'

If uMsg = WM_LBUTTONDOWN Then
    Call Form1.HScroll_Click(wParam, lParam)
End If

pMyWindowProc = CallWindowProc(glPrevWndProc, hw, uMsg, wParam, lParam)
End Function

Public Function fSubClass(lhWnd As Long) As Long
'
' Subclass the form by replacing the "normal" procedures associated
' with the form with procedure "pMyWindowProc".
'
' NOTE: Comment all calls to fSubClass while in IDE.
'
fSubClass = SetWindowLong(lhWnd, GWL_WNDPROC, AddressOf pMyWindowProc)
End Function

0
 
LVL 1

Author Comment

by:dosyl
ID: 2576651
Erick37
What is the reason we can't put breakpoint, i read your warning but i don't understand why Vb do this?
Your idea to make a Click Event is very good.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2577109
For some reason VB IDE will crash if you pause the code while it is running a subclassed window.

When debugging with breakpoints or stepping thru code, comment out the calls in the Load and Unload events and run without the subclassing.
0
 
LVL 1

Author Comment

by:dosyl
ID: 2577312
Edited text of question.
0
 
LVL 1

Author Comment

by:dosyl
ID: 2577321
I accept this answer because it was solution for my problem.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now