Results 1 to 7 of 7

Thread: Accessing a Control from Me.Controls by name?

  1. #1

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    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:
    Don't Rate my posts.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Controls.Item(Controls.IndexOf(Button1)).Text = "tagged"
    3.     End Sub

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  6. #6

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    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.
    Don't Rate my posts.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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
  •  



Click Here to Expand Forum to Full Width