access form control propertys via control name as string

Discussion in 'Microsoft C# .NET' started by Peted, May 22, 2007.

  1. Peted

    Peted Guest

    if i have the name of a control in a string is there anyway to access
    its properties ?

    Eg

    Button btnClickme1;

    i want to do this

    ("btnClickme" + "1").text = "newtext";

    is this possible

    i am trying to do this becasue i have 8 buttons on the form, they are
    only differentiated in the name by a number

    Eg

    btnClickme1, btnClickme2 etc etc

    and i dont know any way of selectively accessing the properties within
    a for loop

    any advice appreciated


    thanks

    Peted
     
    Peted, May 22, 2007
    #1
    1. Advertisements

  2. Peted

    Guest Guest

    for (int i = 1; i < 9;i++)
    this.Controls["btnClickme" + i.ToString()].Text = "newtext";

    will change the text on all the buttons.

    "Peted" wrote:

    > if i have the name of a control in a string is there anyway to access
    > its properties ?
    >
    > Eg
    >
    > Button btnClickme1;
    >
    > i want to do this
    >
    > ("btnClickme" + "1").text = "newtext";
    >
    > is this possible
    >
    > i am trying to do this becasue i have 8 buttons on the form, they are
    > only differentiated in the name by a number
    >
    > Eg
    >
    > btnClickme1, btnClickme2 etc etc
    >
    > and i dont know any way of selectively accessing the properties within
    > a for loop
    >
    > any advice appreciated
    >
    >
    > thanks
    >
    > Peted
    >
     
    Guest, May 22, 2007
    #2
    1. Advertisements

  3. Peted

    Peter Duniho Guest

    On Tue, 22 May 2007 01:20:23 -0700, <Peted> wrote:

    > i want to do this
    >
    > ("btnClickme" + "1").text = "newtext";
    >
    > is this possible


    In the form containing the controls:

    for (int i = 0; i < cbuttonMax; i++)
    {
    Controls.Find("btnClickme" + i.ToString()).Text = "newtext";
    }

    Generally speaking, if you know the type of control you're dealing with,
    you can always cast it to the appropriate type and access the property
    directly. In this case you don't even need to cast, since the Text
    property is in the base Control class returned by the Find() method.

    Pete
     
    Peter Duniho, May 22, 2007
    #3
  4. Peted

    Peter Duniho Guest

    On Tue, 22 May 2007 01:59:46 -0700, Peter Duniho
    <> wrote:

    > In the form containing the controls:
    >
    > for (int i = 0; i < cbuttonMax; i++)
    > {
    > Controls.Find("btnClickme" + i.ToString()).Text = "newtext";
    > }


    Sorry...shouldn't post off the top of my head so late. :)

    Of course, the reply from tony is correct. You can fix mine by indexing
    the array returned by the Find() method (eg "Controls.Find("btnClickme"
    + i.ToString())[0].Text = "newtext";"), but that's overkill.

    Note that whether you use Find() or the [] operator tony suggests, it is
    better to check the returned value (non-empty array using Find(), non-null
    result using []) before attempting to access the Text property. This will
    prevent an exception in case there's some bug somewhere that causes your
    index to wind up out of range of which controls are actually present.

    Pete
     
    Peter Duniho, May 22, 2007
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SurfCoder

    Changing Connectionstring propertys RT?????

    SurfCoder, Jul 31, 2003, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    241
    Nicholas Paldino [.NET/C# MVP]
    Jul 31, 2003
  2. Hessam
    Replies:
    0
    Views:
    961
    Hessam
    Aug 8, 2003
  3. Jacek Jurkowski

    Propertys and PropertyGridControl ...

    Jacek Jurkowski, Mar 13, 2005, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    219
    Jacek Jurkowski
    Mar 13, 2005
  4. Merk
    Replies:
    2
    Views:
    258
  5. greatbarrier86

    String.Contains then a String.Replace or just String.Replace

    greatbarrier86, Mar 13, 2009, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    611
    Tom Overton
    Mar 13, 2009
Loading...

Share This Page