How to reference a control which was added programmingly

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • william

    #1

    How to reference a control which was added programmingly

    Hi, everyone

    After adding a control into a container like form using form.controls.a dd(myControl), how could I reference it
    For example
    dim aControl as new butto
    me.controls.add (aControl

    then I want set backcolor of this button, how

    Thanks

    william
  • Gary Milton

    #2
    Re: How to reference a control which was added programmingly

    \\\
    Dim aControl As New Button

    Me.Controls.Add (aControl)

    aControl.BackCo lor = Color.Blue
    ///

    HTH,
    Gary

    "william" <anonymous@disc ussions.microso ft.com> wrote in message
    news:B4D2E6BE-6602-45F5-890D-1C07C890351D@mi crosoft.com...[color=blue]
    > Hi, everyone,
    >
    > After adding a control into a container like form using[/color]
    form.controls.a dd(myControl), how could I reference it?[color=blue]
    > For example,
    > dim aControl as new button
    > me.controls.add (aControl)
    >
    > then I want set backcolor of this button, how?
    >
    > Thanks.
    >
    > william[/color]


    Comment

    • APG

      #3
      Re: How to reference a control which was added programmingly

      Hi William,

      You can use the same button object till it is within scope. For example,

      Dim aControl As New Button
      Me.Controls.Add (aControl)

      aControl.BackCo lor = Color.AliceBlue

      HTH.

      william wrote:
      [color=blue]
      > Hi, everyone,
      >
      > After adding a control into a container like form using form.controls.a dd(myControl), how could I reference it?
      > For example,
      > dim aControl as new button
      > me.controls.add (aControl)
      >
      > then I want set backcolor of this button, how?
      >
      > Thanks.
      >
      > william[/color]

      Comment

      • Jeremy Williams

        #4
        Re: How to reference a control which was added programmingly


        "william" <anonymous@disc ussions.microso ft.com> wrote in message
        news:353AA573-C974-44DD-81B9-2EEFEFABD1E6@mi crosoft.com...[color=blue]
        > Hi,
        > The problem is I did it in a function, so the object(control) is out of[/color]
        the scope.[color=blue]
        > Here is the case:
        > In my form load event, I call a function to load all controls[/color]
        programmingly, then in other function, I want to use these controls.[color=blue]
        > For example,
        > private sub Form_Load(...)
        > ...
        > LoadControl
        > end sub
        > private sub LoadControl
        > dim aButton as new button
        > aButton.name="M yButton"
        > aButton.text="O K"
        > 'add other properties here
        > me.Controls.Add (aButton)
        > ...
        > end sub
        > private sub MyFunction
        > ...
        > 'todo: need set Backcolor=blue to the button with name of "MyButton"
        >
        > end sub
        >
        > How can I get the proper button handler?
        >
        > thanks
        >
        > william
        >[/color]

        The simplest approach is to declare your control variables at the form
        scope, not the LoadControl scope. You can then access the variables from any
        method in the form class.

        Your only other approach is to loop through the controls in the form's
        controls collection, looking for the control that matches the criteria you
        want to match, then setting the BackColor of that control. This is a very
        bad option, though, since you would be looping through every time you wanted
        to change a property of a control. Better to just declare the variables at
        the form's scope.


        Comment

        Working...