如何将整数变量放在文本框名称的一部分? [英] How to put integer variable in part of textbox's name?
问题描述
大家好
我有一些TextBox和Lable。我想把整数变量放在TextBox名称的一部分。
例如:
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();
}
}
}
}
我想在表单上发送一些Contorols到sql和sql,根据这个号码做自己的订单。我确实想确定哪一个标签现在发送。我应该定义lbl [i],因为使用''i''我可以发送所有Lable.Text或TextBox.Text顺序。我想说lbl [i](= lbl1).Text现在发送。但我已经尝试了
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.
推荐答案
简单搜索显示,lbl
实际上从未定义过。所以,你认为它会被一个奇迹定义吗?确保你定义它。
-SA
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
尝试如下
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);
首先看到的定义Controls.Find
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 [ ^ ]
假设i = 2
string.Format(lbl {0},i)
给出值= lbl2
this.Controls.Find
按给定的键搜索控件。
例如:
编译完成后
this.Controls.Find(string.Format(lbl {0},i),false).First()。Text
就像
this.Controls.Find(lbl2,false).First()。Text
.First()
是一个选择第一个元素的LINQ。
忘了提到你可能必须包含命名空间
使用System.Linq
;
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
;
你可以这样做波纹管。
You can do like the bellow.
List<TextBox> textBoxList = new List<TextBox>();
textBoxList.Add(txtName);
textBoxList.Add(txtPhone);
TextBox txtName=textBoxList[1];
这篇关于如何将整数变量放在文本框名称的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!