Solved

Focus problem

Posted on 2000-03-01
14
179 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2016 VB code 9 117
Specify ip address in my connection string for sas 2 60
VBA open file from excel cell 4 42
Put text in a picture ASP.NET C# 2 58
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

825 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