Click here to Skip to main content
12,945,697 members (54,035 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi everyone
I have some TextBox and Lable. I want to put integer variable in part of TextBox's name.
for example:

private void btnSabtClerkEvaluation_Click(object sender, EventArgs e)
                if (lbl1MadrakeTahsili1.Text.Trim() == string.Empty || lbl2Takhasos2.Text.Trim() == string.Empty || lbl3Khalaghiat3.Text.Trim() == string.Empty || lbl4Nazm4.Text.Trim() == string.Empty || lbl5Raftar5.Text.Trim() == string.Empty ||
                   lbl6Taamol6.Text.Trim() == string.Empty || lbl7Masouliatpaziri7.Text.Trim() == string.Empty || lbl8Arastegi8.Text.Trim() == string.Empty || lbl9MizaneKhata9.Text.Trim() == string.Empty || lbl10SakhtiKar10.Text.Trim() == string.Empty)
                    MessageBox.Show("Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    SqlCommand cmd = new SqlCommand("ClerkEvaluation_Insert", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    for (int i = 0; i <= 10; i++)
                        cmd.Parameters.AddWithValue("@clrk_Name", txtNameClerkEvaluation.Text);
                        cmd.Parameters.AddWithValue("@clrk_Family", txtFamilyClerkEvaluation.Text);
                        cmd.Parameters.AddWithValue("@clrk_MelliCode", txtMelliCodeClerkEvaluation.Text);
                        cmd.Parameters.AddWithValue("@clrke_Percent", lbl[i].Text);// this name is lbl1. How to give number?
                        cmd.Parameters.AddWithValue("@clrke_Tozihat", txt[i].Text);// This name is lbl2

I want to send a number of Contorols on the form to sql and sql to do own order base on this number. I exactly want to determine when which one of lables are sending now. I should define lbl[i], because with 'i' i can send all Lable.Text or TextBox.Text order. I want to say lbl[i] (=lbl1) .Text to send now.but i have tried

cmd.Parameters.AddWithValue("@clrke_Percent", lbl[i].Name.Substring(3) + " " + lbl[i].Text);
cmd.Parameters.AddWithValue("@clrke_Tozihat", txt[i].Name.Substring(3) + " " + txt[i].Text);

When i write this line, I get this error
"Error2The name 'lbl' does not exist in the current contextD:\Project\evaluation\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs976119WindowsFormsApplication1"
Thx for your help.
Posted 22-Jan-13 21:15pm
hemantrautela 23-Jan-13 3:27am
where the declaration & declination of lbl[] and txt[] array..??

"lbl[i] (=lbl1)" for achieving this you have first initialize lbl[] array(Label Type) as
lbl[0] = lbl0; lbl[1] = lbl1; and so on....
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

As simple search shows, lbl is really never defined. So, do you think that it will be defined by a miracle? Make sure you define it.

Abdul Quader Mamun 23-Jan-13 3:35am
It is not possible to define a number with textbox name.
Why? And why are you even mention it? It all depends on what do you mean by "define".
Abdul Quader Mamun 23-Jan-13 4:18am
It is not possible to define dynamic variable name or textbox name.
OK, if you don't want to answer my question, don't...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

try like follows

cmd.Parameters.AddWithValue("@clrke_Percent", this.Controls.Find(string.Format("lbl{0}", i), false).First().Text);
         cmd.Parameters.AddWithValue("@clrke_Percent", this.Controls.Find(string.Format("txt{0}", i), false).First().Text);

first see the definition for Controls.Find
public Control[] Find(
	string key,
	bool searchAllChildren

suppose i=2
string.Format("lbl{0}", i) give the value =lbl2
this.Controls.Find search the controls by given key.

for ex:
after compiling the
this.Controls.Find(string.Format("lbl{0}", i), false).First().Text
it just like
this.Controls.Find("lbl2", false).First().Text
.First() is a LINQ to select the first element.

forget to mentioned you may have to include the namespace
using System.Linq;
Elham.Deljooei 23-Jan-13 4:15am
Excuse me. can i say me "cmd.Parameters.AddWithValue("@clrke_Percent", this.Controls.Find(string.Format("lbl{0}", i), false).First().Text);"
what does this do exactly? Because i Want variable value be part of textbox's name. for example i define lbl1 and in the code i want say lbl[i].Text. based on 'i' the code choose own lbl and send its text.
I have tried this but it isn't correct.
i'm sorry again. Thx for your help.
Tharaka MTR 23-Jan-13 4:33am
What I understood is you have set of label and text boxes
for ex: lbl1, lbl2 lbl3,lbl4 etc ..
txt1,txt2,txt3 etc..
and you want to get the value of those controls dynamically. please correct me if I'm wrong.

If I am right, you can use this.Controls.Find() method for this.
Elham.Deljooei 23-Jan-13 4:44am
Of course, you're right. but i don't know how to use this.
Exactly i want to say when variable 'i=1' then choose lbl1 and 'i=2' then choose lbl2(in a For).
Tharaka MTR 23-Jan-13 5:03am
I have added the explanation by improving the solution. pls check
Elham.Deljooei 23-Jan-13 5:14am
First i want to know Why 'false'?
i added Using System.Linq; but those lines don't run.When the bugger arrived this line it jump to Catch part.
Thn for your help.
Tharaka MTR 23-Jan-13 5:18am
oh.. false is for not to search all the children.
That's why I asked to check the definition of the Find() method first

public Control[] Find(
string key,
bool searchAllChildren

It asked two parameter, "key" and Boolean parameter for searchAllchildren
Elham.Deljooei 23-Jan-13 5:23am
I'm really sorry. I didn't see your message properly:(
Elham.Deljooei 23-Jan-13 5:29am
I'm a new programer. I like Programing very much but i don't know how to start to learn c#.
can you instruct me?
Tharaka MTR 23-Jan-13 5:37am
here are some useful links

and please confirmed your question is resolve by pressing "mark as answer".
Elham.Deljooei 23-Jan-13 6:09am
confirmed your question is resolve by mark as answer. how to do this?
Elham.Deljooei 23-Jan-13 6:41am
Thx for your help:)
Tharaka MTR 23-Jan-13 6:56am
:) .. in each solution there is a button called "Mark As Answer". Please select the correct solution and click that button. then it will marked as answer.
Elham.Deljooei 23-Jan-13 8:13am
oh yeah. but i haven't gotton my ansewr.
i think it's correct but don't run.
Elham.Deljooei 23-Jan-13 8:23am
In Try when debugger arrive this line. it jump to Catch part and don't show any error message.
Tharaka MTR 23-Jan-13 8:48am
ok. that is the best one I know. some one will reply to another solution.
Elham.Deljooei 25-Jan-13 1:39am
That code has this eeror "Sequence contains no elements"
Tharaka MTR 26-Jan-13 8:33am
first validate the control count.
if (this.Controls.Find(string.Format("lbl{0}", i), false).Count()>0){
// then your code here
Elham.Deljooei 30-Jan-13 5:10am
I saw your message 5 min ago.
Thx for your help.
Tharaka MTR 30-Jan-13 5:33am
was your problem fixed?
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You can do like the bellow.

List<TextBox> textBoxList = new List<TextBox>();
TextBox txtName=textBoxList[1];
Elham.Deljooei 23-Jan-13 5:01am
I don't know What you mean exactly?
I'm a new programer. I like Programing very much but i don't know how to start to learn c#.
can you instruct me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,899
CHill60 3,170
Maciej Los 2,443
Jochen Arndt 1,910
ppolymorphe 1,775

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100