|
-
Sep 27th, 2012, 04:02 AM
#1
Thread Starter
Hyperactive Member
Make label ID dynamic
Hello,
Code:
int i;
i = 5;
lblName + i .text = "Test";
That's what i need in brevity.
Plz help me to solve this issue.
Thanks in advance.
-
Sep 27th, 2012, 04:17 AM
#2
Re: Make label ID dynamic
You can get a control by name from the Controls collection of its parent, e.g.
vb.net Code:
For i = 1 To 5
Controls("Label" & i).Text = "Label" & i
Next
The item is returned as a Control reference, so if you're just using members of the Control class, e.g. Text, then that's fine. If you want to access a member of that specific type of control then you should cast as that type. Indeed, if you have Option Strict On, as you should, then you will be required to cast.
Last edited by jmcilhinney; Sep 27th, 2012 at 04:24 AM.
-
Sep 27th, 2012, 04:35 AM
#3
Thread Starter
Hyperactive Member
Re: Make label ID dynamic
Thank you, I tried make code:
[Code]
int i = 1;
Controls["lblFP" + i].Text = "Test";
[Code/]
Give me error "Object reference not set to an instance of an object." i try adjust code to run but need help.
-
Sep 27th, 2012, 04:56 AM
#4
Re: Make label ID dynamic
Ah whoops, I forgot I was C# land and posted VB code. It seems that you successfully translated it though. As for the error message, that means that there is no control on your form with that Name.
-
Sep 27th, 2012, 07:00 AM
#5
Thread Starter
Hyperactive Member
Re: Make label ID dynamic
Hi,
I catch where is the problem, i am using groupbox this code can't find any object on groupbox must put the label on form direct and use it.
Do there any idea to surmount this problem?
-
Sep 27th, 2012, 09:00 AM
#6
Re: Make label ID dynamic
Please read what I posted.
You can get a control by name from the Controls collection of its parent
If the GroupBox is the parent of the Label then you have to use the Controls collection of the GroupBox, not the form. This is exactly why I prefer not to post code: people just copy the code and don't actually read.
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
|