Consulting

Results 1 to 12 of 12

Thread: Userform control's handle

  1. #1
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location

    Userform control's handle

    Hey guys,

    I've looked up and down, for 2 days, on how to obtain a control's handle on a userform in VBA...and have yet to find an answer.

    I've looked at FindWindow and FindWindowEx APIs as well as EnumChildWindows (with GetWindowText and GetWindowTextLength) and I'm just either not understanding what's out there on the net or I'm not going the right path.

    If I have a Userform with a TextBox, ListBox, and 2 CommandButtons, for example, does anybody know how to get each control's Handle?

    I guess nobody does this in VBA...In VB, each control has a hWnd property. So does that mean that controls in VBA are Windowless?




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Joseph,

    Not all form controls have handles, most MSForms are windowless contrrols. Only the Listbox and Frame controls have distinct handles, all others share the same hWnd.

    I have some code that can get the handle, but unless it is a listbox or a frame it is not of much use.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Thanks for the info, Bob. It's the most useful piece of information I've found yet.

    The thing is, I'm trying to help someone on another board and, for some reason, they want to be able to programmatically "push" a commandbutton on a userform in VBA. I know I could do it if I can use SendMessage and set BM_SETSTATE to true for it to be "highlighted" (pushed in), but the problem with SendMessage is I need a handle:
    http://msdn.microsoft.com/en-us/library/bb761823(VS.85).aspx

    I like the question because it's an interesting challenge...but I'm running out of choices now.

    Are some controls in VB6/VB.NET also Windowless like VBA's controls?




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by malik641
    Thanks for the info, Bob. It's the most useful piece of information I've found yet.

    The thing is, I'm trying to help someone on another board and, for some reason, they want to be able to programmatically "push" a commandbutton on a userform in VBA. I know I could do it if I can use SendMessage and set BM_SETSTATE to true for it to be "highlighted" (pushed in), but the problem with SendMessage is I need a handle:
    http://msdn.microsoft.com/en-us/library/bb761823(VS.85).aspx
    Couldn't they just call the button's click event, same net effect?

    Quote Originally Posted by malik641
    I like the question because it's an interesting challenge...but I'm running out of choices now.

    Are some controls in VB6/VB.NET also Windowless like VBA's controls?
    I can't say for sure about VB.Net, but I think all VB6 controls are not windowless (windowed?), so I would guess the same is true in .Net.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    I suggested just that, but the problem with calling the _Click() event is they said that the button doesn't "visually" push in and come back out and they want it to....weird on WHY they need that, but again it's a cool challenge for me


    And just looked at the intellisense in VB.NET for some controls, each has its own handle property:
    [VBA]Me.cmbTable.Handle
    Me.TextBox1.Handle
    Me.Label1.Handle
    Me.lstAvailableColumns.Handle
    Me.tabMain.Handle[/VBA]
    I'll verify later if they do return distinct handle numbers.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why not just use a toggle button?

  7. #7
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    I don't know. It's not my project. The user specifically requested to "push" a command button using code only (as if it was left-clicked by the mouse and they held down the button).




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  8. #8
    VBAX Regular
    Joined
    Jul 2008
    Location
    Cincinnati, OH
    Posts
    86
    Location
    Hi Josheph.

    My "off the top of my head" approach would be to use the SendInput API function. You would need to determine the screen coords of the button on the userform. Some functions/properties you may need.

    GetWindowPlacement API to determine the position of the userform in screen coords.

    UserForm.InsideHeight and/or UserForm.InsideWidth. You can determine the width of the userform border using these in conjuction with the height and width properties.

    GetClientRect API function.
    ClientToScreen API function.

    Conversion of points to pixels using a custom function.

    Ect...

  9. #9
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Hey Tom,

    Thanks for the suggestion. I will look into that. It just seems like so much work for something that is not so difficult in VB 6.0+.

    I'm going to try to verify that I cannot get the handles of VBA userform controls. If there is no way to get them, I'll probably start using your alternative.

    It's just so sad that I only need the handle and then I could use the SendMessage API. But I can't get this handle!




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You won't get handles of userform controls, as I said, the majority share the same handle.

    Here's a thought, create a VB6 form and call that from VBA.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Hey Bob,

    Sorry about that. I was mistaken when I re-read post #4 and confused it with post #2.

    And that's definitely a good idea. I'll look into that as well. I'll post my findings.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It is easy, I do it when I want a menu userform as that is easy in VB, difficult in VBA>
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •