Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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)
        {
            try
            {
                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);
                }
                else
                {
                    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
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
            }
}
 

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);
 
and
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 22:15pm
Comments
hemantrautela at 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
good
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.
 
—SA
  Permalink  
Comments
Abdul Quader Mamun at 23-Jan-13 3:35am
   
It is not possible to define a number with textbox name.
Sergey Alexandrovich Kryukov at 23-Jan-13 3:47am
   
Why? And why are you even mention it? It all depends on what do you mean by "define".
—SA
Abdul Quader Mamun at 23-Jan-13 4:18am
   
It is not possible to define dynamic variable name or textbox name.
Sergey Alexandrovich Kryukov at 23-Jan-13 4:28am
   
OK, if you don't want to answer my question, don't...
—SA
Rate this: bad
good
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
)
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.find(v=vs.100).aspx[^]
 
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;
  Permalink  
v2
Comments
Elham.Deljooei at 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 at 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 at 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 at 23-Jan-13 5:03am
   
I have added the explanation by improving the solution. pls check
Elham.Deljooei at 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 at 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 at 23-Jan-13 5:23am
   
I'm really sorry. I didn't see your message properly:(
Elham.Deljooei at 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 at 23-Jan-13 5:37am
   
here are some useful links
http://www.csharp-station.com/tutorial.aspx
http://www.c-sharpcorner.com/UploadFile/anandnarayanswamy/CSTutorial1AN11282005015140AM/CSTutorial1AN.aspx
 
and please confirmed your question is resolve by pressing "mark as answer".
Elham.Deljooei at 23-Jan-13 6:09am
   
confirmed your question is resolve by mark as answer. how to do this?
Elham.Deljooei at 23-Jan-13 6:41am
   
Thx for your help:)
Tharaka MTR at 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 at 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 at 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 at 23-Jan-13 8:48am
   
ok. that is the best one I know. some one will reply to another solution.
Elham.Deljooei at 25-Jan-13 1:39am
   
Hi
That code has this eeror "Sequence contains no elements"
Tharaka MTR at 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 at 30-Jan-13 5:10am
   
Hi
I saw your message 5 min ago.
Thx for your help.
Tharaka MTR at 30-Jan-13 5:33am
   
was your problem fixed?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can do like the bellow.
 
List<TextBox> textBoxList = new List<TextBox>();
textBoxList.Add(txtName);
textBoxList.Add(txtPhone);
 
TextBox txtName=textBoxList[1];
 
  Permalink  
Comments
Elham.Deljooei at 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
0 DamithSL 320
1 OriginalGriff 195
2 Afzaal Ahmad Zeeshan 154
3 BillWoodruff 135
4 Peter Leow 115
0 OriginalGriff 7,510
1 DamithSL 5,519
2 Sergey Alexandrovich Kryukov 4,994
3 Maciej Los 4,936
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2014
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