-
March 8th, 2015, 10:34 PM
#1
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";
}
-
March 9th, 2015, 03:57 PM
#2
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 (i = 0; i < 20; i++) { 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 sender, EventArgs e) { int i = 0; for (i=0; i<20; i++) { //Change the name of the labels. mylabel[i].Text = "Hello world"; } } } }
-
March 11th, 2015, 03:56 PM
#3
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
-
March 20th, 2015, 04:00 AM
#4
Re: Using Object Name in Code
Originally Posted by TheCPUWizard
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
-
March 20th, 2015, 04:02 AM
#5
Re: Using Object Name in Code
Originally Posted by Matt Hauser
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 (i = 0; i < 20; i++) { 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 sender, EventArgs e) { int i = 0; for (i=0; i<20; i++) { //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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|