CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2006
    Posts
    21

    Using Object Name in Code

    Hi all
    I was wondering how you would go about using an object name in the code.
    Example i have a number of labels naming lbl001, lbl002 etc.
    In my code, say if i want to change all the label text, how would i change them easily?
    ie
    for (int 1=0;i<20;i++)
    {
    lbl + i.tostring().padleft(0,3) .text - "Hello world";
    }

  2. #2
    Join Date
    Apr 1999
    Location
    Davenport, Iowa, USA
    Posts
    158

    Re: Using Object Name in Code

    ishtarsg,

    I don't know if this is what you're looking for, but here is an example of creating 20 labels dynamically, then when you hit a button, it renames all of them to "Hello world".

    Hope this helps!
    Matt
    PHP Code:
    namespace UserCtrl
    {
        public 
    partial class Form1 Form
        
    {
            
    Label[] mylabel = new Label[20];

            public 
    Form1()

            {
                
    InitializeComponent();

                
    int i 0;
                
    int topx 10;
                
    int leftx 10;

                
    //create the labels...
                
    for (020i++)
                {
                    
    mylabel[i] = new Label();
                    
    mylabel[i].Top topx;
                    
    mylabel[i].Left leftx;
                    
    mylabel[i].Text "Label " i;
                    
    this.Controls.Add(mylabel[i]);
                    
    topx += 25;
                }
            }

            private 
    void button1_Click(object senderEventArgs e)
            {
                
    int i 0;
                for (
    i=0i<20i++)
                {
                    
    //Change the name of the labels.
                    
    mylabel[i].Text "Hello world";
                } 
            }
        }


  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Using Object Name in Code

    If the controls (not just labels) are static, then one can iterate through the Controls collection of the parent, and once a reference to the control has been found access the appropriate properties.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Apr 2006
    Posts
    21

    Re: Using Object Name in Code

    Quote Originally Posted by TheCPUWizard View Post
    If the controls (not just labels) are static, then one can iterate through the Controls collection of the parent, and once a reference to the control has been found access the appropriate properties.
    Hi
    Thanks for the reply, the labels are static and your methods works.

    Thanks

  5. #5
    Join Date
    Apr 2006
    Posts
    21

    Re: Using Object Name in Code

    Quote Originally Posted by Matt Hauser View Post
    ishtarsg,

    I don't know if this is what you're looking for, but here is an example of creating 20 labels dynamically, then when you hit a button, it renames all of them to "Hello world".

    Hope this helps!
    Matt
    PHP Code:
    namespace UserCtrl
    {
        public 
    partial class Form1 Form
        
    {
            
    Label[] mylabel = new Label[20];

            public 
    Form1()

            {
                
    InitializeComponent();

                
    int i 0;
                
    int topx 10;
                
    int leftx 10;

                
    //create the labels...
                
    for (020i++)
                {
                    
    mylabel[i] = new Label();
                    
    mylabel[i].Top topx;
                    
    mylabel[i].Left leftx;
                    
    mylabel[i].Text "Label " i;
                    
    this.Controls.Add(mylabel[i]);
                    
    topx += 25;
                }
            }

            private 
    void button1_Click(object senderEventArgs e)
            {
                
    int i 0;
                for (
    i=0i<20i++)
                {
                    
    //Change the name of the labels.
                    
    mylabel[i].Text "Hello world";
                } 
            }
        }

    Hi because the GUI was design in the IDE hence repositioning objects in run time is pretty tedious especially in the calculation since all the object are randomly placed.

    Still thanks for the help

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

Featured