|
-
May 7th, 2004, 08:10 PM
#1
Thread Starter
PowerPoster
Accessing a Control from Me.Controls by name?
Basically I'm making a form that handles the mouseovers for multiple controls, and each form should interact with a slightly different control.
So I could do,
If control = "this one" then
'do stuff with this one
Else
etc.
Or, I could make things difficult for myself (this option is always the best).
I remember in VB6 you used to be able to access a control just by knowing its name (passing a method a string value) and I can't remember what it is anymore.
So basically the controls are like this (the mouseover stuff),
room1
room2
room3
And the the particular controls that they have to interact with will be,
room1Zone1
room2Zone1
So I can get the room part (which is all I need) from the Sender.Name property, I just can't figure out how I'm supposed to use it to access the rooms properties and stuff.
It was something like this,
Me.Controls("control name").text or whatever here
but Controls() expects the index of the control in question, so I dunno how I can find that. :doh:
-
May 7th, 2004, 09:06 PM
#2
I wonder how many charact
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Controls.Item(Controls.IndexOf(Button1)).Text = "tagged"
End Sub
-
May 7th, 2004, 09:16 PM
#3
Why does Sender have the Name of the control?
You can loop through and check each control in the control collection for the right name. It'd be easier to store a reference to the control instead of its name. You may be making this harder than it needs to be. For instance this:
Controls.Item(Controls.IndexOf(Button1)).Text = "tagged"
And this are the same thing:
Button1.Text = "tagged"
Since the first code implies that you already have a reference to the control in question.
-
May 7th, 2004, 09:24 PM
#4
I wonder how many charact
Yea Ednessis you're right... lol
but Controls() expects the index of the control in question, so I dunno how I can find that.
I was bewildered by the question until I hit this part, so I gave him that much.
-
May 7th, 2004, 09:43 PM
#5
Yeah a lot of times people are more used to referring to things by some index or key and forget that you can just keep a reference to the object itself (speaking of the question not your reply). Most of the time it is much easier to store a reference then to translate back and forth between some key. Or if sender is the textbox in question you can just cast it to textbox type.
-
May 8th, 2004, 01:12 AM
#6
Thread Starter
PowerPoster
Sorry, I didn't explain very well.
Basically, the mouseover event will have the sender object in its parameters (like they all do.)
This can be either,
room1
room2
room3
room4
Each room has a 3 zones attached to it (not literally , but on the form they belong with each other),
room1Zone1
room1Zone2
room1Zone3
room2Zone1
etc.
Because I'm trying to make a routine that handles all of the rooms mouseovers, I don't want to hard code which rooms it should deal with. So instead I was hoping to do something like,
Me.Controls(sender.name & "Zone1")
which would mean that I wouldn't have to have,
If sender.name = room1 then
'deal with the room1Zone#'s
Else sender.name = room2 then
'deal with the room2Zone#'s
etc
That make anymore sense?
The code nemaroller posted won't work since it assumes that I know which controls I want to work with before hand, which I won't.
I guess I can always loop, but it means alot of code.
-
May 8th, 2004, 01:48 AM
#7
Looping is the only way to find it by name. You can setup a hashtable with the room textboxes as keys and link them to the other items for better access.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|