Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unique IDs for VB controls 2

Status
Not open for further replies.

petherin

Programmer
Dec 3, 2002
13
0
0
GB
Within a VB project, does each control (i.e. every control on every form in a project) have a unique ID, other than its full name (e.g. frmMain.cboAddressLine1 or whatever)? Something like a number that can be read so you can tell which control was clicked on from a single procedure? I don't wish to do the check on the actual Click event of the control, but in a procedure elsewhere.
 
You can do something like this:

frmForm.Controls(lngLocalI).Name

lngLocalI would just be a number allowing you to access a particular control in the controls collection much in the same way as you'd access an element in an array.
 
A simple answer to yoru question on the Unique ID would be hWnd.

Craig

"I feel sorry for people who don't drink. When they wake up in the morning, that's as good as they're going to feel all day."
~Frank Sinatra
 
not all controls have a hWnd property (labels for example)

also

From MSDN (hWnd Property)

Note Because the value of this property can change while a program is running, never store the hWnd value in a variable.

good luck!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
Petherin,

We could help More if you told us exactly what you wanted to do.



Craig

"I feel sorry for people who don't drink. When they wake up in the morning, that's as good as they're going to feel all day."
~Frank Sinatra
 
Ehhm the most unique quality of an object is the object itself. You can use the is operator to compare any object to the object you want. For example (in a form with a button called cmdExit):

Code:
private sub showExitButton(byref cmd as CommandButton)
 if cmd is Me.cmdExit then msgbox "This is the one"
end sub

An alternative, though not a recommendable one, is using the ObjPtr function.

Best regards
 
The problem I think he is having is sending which button it is.

For Instance I think, maybe I'm wrong, that he wants to put the same code in each text box, to do different things. Like.

private sub Text1_Click()
DoStuffWithClick Text1
end sub

private sub DoStuffWithClick(byref ctl as Control)
Select Case ctl.Name
.....
end sub

I think what he is asking is how does he get rid of TEXT1 above and put in generic text. Something like Me.Control....


Craig

"I feel sorry for people who don't drink. When they wake up in the morning, that's as good as they're going to feel all day."
~Frank Sinatra
 
Yes, what I was after is something like this:

The user clicks a control (any control, on any form in the project). Attached to the the code for each control would be something like:

Private Sub Command1_Click()
isClickCorrect (UniqueIDForControl)
End Sub

Then isClickCorrect would be along the lines of:

Public Function isClickCorrect(ControlID as Integer) as Boolean
select case ControlID
case 213,42,56:
isClickCorrect = True
case else:
isClickCorrect = False
end select
end Function

What I want is a way of generating or getting a unique ID for each and every control. The way I did it before was to just assign an abitrary number to each control that is passed to the isClickCorrect function, but with something like 1,000 controls in the project this is very labour intensive.
 
You could.... put a value in the Tag field for the control.

Or ... use

me.ActiveControl

Craig

"I feel sorry for people who don't drink. When they wake up in the morning, that's as good as they're going to feel all day."
~Frank Sinatra
 
Where can I find info on the ObjPtr thing mentioned, I couldn't find it in MSDN?
 
ObjPtr function exists but officially not documented due to the reason that Microsoft don't want programmers to play with pointers in VB.

It is also hidden in VB's Object Browser. To see its definition, select Show Hidden Members from context menu, and search for "objptr".
This is all about ObjPtr that I found in Microsoft KnowledgeBase article #Q199824.
___

ObjPtr
------

ObjPtr takes an object variable name as a parameter and obtains the address of the interface referenced by this object variable.

One scenario of using this function is when you need to do a collection of objects. By indexing the object using its address as the key, you can get faster access to the object than walking the collection and using the Is operator. In
many cases, the address of an object is the only reliable thing to use as a key.

Example:

objCollection.Add MyObj1, CStr(ObjPtr(MyObj1))
...
objCollection.Remove CStr(ObjPtr(MyObj1))

___
One thing to note that ObjPtr is the not only way to obtain the object pointer. In fact
[tt]
Dim lngVar As Long
lngVar = ObjPtr(anObject)
[/tt]
is equivalent to
[tt]
Dim lngVar As Long
CopyMemory lngVar, anObject, 4
[/tt]
 
petherin,

Rather than messing with the Pointers. Maybe it's hidden for a reason LOL

Why not do this.

Private Sub Command1_Click()
isClickCorrect (me.ActiveControl)
End Sub

Then isClickCorrect would be along the lines of:

Public Function isClickCorrect(i_Object as Object) as Boolean
select case i_Object.Name
case "cmd1", "cmd2", "cmd3":
isClickCorrect = True
case else:
isClickCorrect = False
end select
end Function


Craig

"I feel sorry for people who don't drink. When they wake up in the morning, that's as good as they're going to feel all day."
~Frank Sinatra
 
Craig

Yes, I finally opted for the simpler method of just using the control itself rather than a pointer to it. Should've thought of that earlier really. Dah well.
 
Since the control name is not unique within the app - but is within the form, add the reference to "me", as in:

[tab]Me.Name& "." & (Me.ActiveControl)

Of course you then need to reference both the form name and control name in the called procedure.

Other possabilities do exist, such as createing your own reference value (in a table) which is refreshed during startup of the app, and using the formname.controlname, retrieve the reference value from the table for use in your routine.

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
I though I made a post here yesterday, but I guess it didn't take when I thought it did.

Here's a copy:

ObjPtr may not be consistant enough, and therefore useless for your purpose.

Try passing the form to the proceedure:


Public Function isClickCorrect(frm As VB.Form, ctl As Control) As Boolean
Dim sCtlName As string
sCtlName =UCase(frm.Name)

Select Case sCtlName
Case "FORM1"
Select Case UCase(ctl.Name)
Case "CMDCANCEL"
'It further identification is needed:
Select Case UCase(ctl.Tag)
Case "SOME_ACTION"
Call frm.SomePublicMethod


Or, the other way around, where the first Select case is the control's name, and then depending on which form was passed, take action.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top