我想通过循环得到winfrom程序该页面的所有控件的Text文本值,将获取到的Text文本值存在字典中,再通过查询字典中的键值对,找到对应的中文文本,再通过SQL语句查询到英文文本,把中文文本替换掉。 在实现该功能时,一直存在着字典中查询不到键值对的情况。但是在ListBox显示的时候又是存在该Text文本的。 有什么好的解决办法。
Dictionary<string, Control> dic = new Dictionary<string, Control>();
private void Test2_Load(object sender, EventArgs e)
{
listBox1.Items.Clear();
GetControl(this.Controls);
}
//循环遍历出所有的控件ID
private void GetControl(Control.ControlCollection ctc)
{
foreach (Control ct in ctc)
{
AddControlInofToListBox(ct);
if (ct.HasChildren)
{
dic.Add(ct.Text, ct);
GetControl(ct.Controls);
}
else
{
dic.Add(ct.Text, ct);
GetControl(ct.Controls);
}
}
}
//220722 Arthur 获取控件属性
private void AddControlInofToListBox(Control ct)
{
switch (ct.GetType().Name)
{
//如果是ListBox、CheckBox、Button
case "ListBox":
case "GroupBox":
case "Button":
case "Label":
listBox1.Items.Add(ct.Text);
break;
//如果是CheckBox
case "CheckBox":
if (((CheckBox)ct).Checked)
{
listBox1.Items.Add(ct.Text);
//((CheckBox)ct).Checked = false;
}
else
{
listBox1.Items.Add(ct.Text);
//((CheckBox)ct).Checked = true;
}
break;
//如果是RadioButton
case "RadioButton":
RadioButton rdb = (RadioButton)ct;
if (rdb.Checked)
{
listBox1.Items.Add(ct.Text);
//rdb.Checked = false;
}
else
{
listBox1.Items.Add(ct.Text);
//rdb.Checked = true;
}
break;
//其它值
default:
listBox1.Items.Add(ct.Text);
break;
}
}
private void button3_Click(object sender, EventArgs e)
{
string sqlstr = "";
string sqlstr1 = "";
string cnname = "";
string enname = "";
string page = "";
string controlname = "";
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
sqlstr = "select * from dbo.ROC_translate where page='Wireless Test1.0'and type='控件文本'";
dt = Utils.executeQueryT(sqlstr, connStr);
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
//所有的中文文本
cnname = dt.Rows[i][2].ToString();
//获取的窗体名
page = dt.Rows[i][0].ToString();
//所有的控件id
controlname = dt.Rows[i][4].ToString();
sqlstr1 = "select ENtext,CNtext from dbo.ROC_translate where CNtext='" + cnname + "'"
+ "and page='" + page + "'";
dt1 = Utils.executeQueryT(sqlstr1, connStr);
if (dt1 != null && dt1.Rows.Count > 0)
{
//循环获取的英文文本
enname = dt1.Rows[0][0].ToString();
//获取中文文本
cnname = dt1.Rows[0][1].ToString();
if (cnname!="")
{
if (dic.ContainsKey(cnname))
{
var ctr1 = dic[cnname];
ctr1.Text = enname;
}
}
}
}
dt.Clear();
}
this.button3.Visible = false;
this.button4.Visible = true;
}
例如我要把退出按钮的Text文本获取到,然后通过Text文本获取数据库中对应的英文Text文本,然后将其替换掉