Handle Number of a control

Haluk

Rules Violation
Joined
Oct 26, 2002
Messages
1,075
Hi;

I have a number of controls on a UserForm consisting of TextBoxes, ListBoxes and CommandButtons. I need to have the handle number of a ListBox say, ListBox1.

Can we get the handle number of the control Listbox1 via API ? I can get the handle number of the UserForm itself, but I can not get it for ListBox1. I tried to use GetFocus() API but I was not able to get it.

Thx...
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
From what I've heard before, a control only becomes a 'window' when it has the focus, otherwise it is just drawn on the screen. I may be wrong but I've never seen a way to get the handle of a VBA control, except with this method here. You must set the focus to the control you want and then use the GetFocus API and I think it's far from satisfactory.<hr>
CODE<pre>Private Declare Function GetFocus Lib "user32" () As Long

Sub GetHandle()
'Returns the handle of a Combobox on a userform
Me.ComboBox1.SetFocus
MsgBox GetFocus
End Sub</pre>

_________________
<font face="Arial Black" color="#0000FF">Hope this helps,
DK</font>

This message was edited by dk on 2003-02-03 03:18
 
Upvote 0
Thx Dan;

I've tried the similar code before, for the ListBox and got a handle number to use somewhere else in my codes. But, i got wrong results.

So this means that, i've got the correct handle number but something is going wrong in the rest of my codes. I'll check that part again.

Thank you very much...
 
Upvote 0
Hi again Dan;

I've a ListBox on my UserForm and a TextBox. What I'm trying to do is, while I'm typing a string in the TextBox, I want the closest ListBox item to that string to be selected. Something like auto-complete feature. For this purpose, I'm using the lines below to achieve my goal. But, for the time being..... I'm lost !

Thx for any suggestions.

Code:
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const LB_FINDSTRING = &H18F

Dim ListHwnd As Long

Private Sub TextBox1_Change()
ListBox1.ListIndex = SendMessage(ListHwnd, LB_FINDSTRING, -1, CStr(TextBox1.Text))
End Sub

Private Sub UserForm_Initialize()
ListBox1.SetFocus
ListHwnd = GetFocus()
End Sub

(Running XL9 under Win98)
 
Upvote 0
Hi everybody;

I just wanted to know, if someone has an idea for the problem I've stated in my previous message.

Regards,
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,718
Members
448,294
Latest member
jmjmjmjmjmjm

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top