Win窗体控件的动态添加及其控制问题
2007-03-30 10:06
351 查看
Win窗体控件的动态添加及其控制问题
使用语言:C#
一、动态添加控件代码:
Hashtable mht = new Hashtable();// 为操作动态添加的控件,特添加此代码
int N=10;
GroupBox [] testGroupBox=new GroupBox
;
for(int i=0;i<N;i++)
{
#region 动态添加GroupBox控件
testGroupBox[i]=new GroupBox();
testGroupBox[i].Location=new Point(30,60+120*i);
testGroupBox[i].Size=new System.Drawing.Size(900,100);
testGroupBox[i].Text=(i+1).ToString();
this.Controls.Add(testGroupBox[i]);
#endregion
#region 动态添加Label控件
Label [] correctLabel=new Label
;
correctLabel[i]=new Label();
correctLabel[i].Location=new Point(10,10);
correctLabel[i].Size=new System.Drawing.Size(15,15);
correctLabel[i].Text=="singleRadioButton["+i.ToString()+"][1]";
correctLabel[i].ForeColor=Color.Red;
correctLabel[i].Visible=false;
correctLabel[i].Name="Label["+i.ToString()+"]";
mht[correctLabel[i].Name]=correctLabel[i];// ◆
testGroupBox[i].Controls.Add(correctLabel[i]);
#endregion
#region 动态添加RadioButton控件
RadioButton [] singleRadioButton=new RadioButton[4];
for(int j=0;j<4;j++)
{
singleRadioButton[j]=new RadioButton();
singleRadioButton[j].Name="singleRadioButton["+i.ToString()+"]["+j.ToString()+"]";
mht[singleRadioButton[j].Name]=singleRadioButton[j];// ◆
singleRadioButton[j].Location=new Point(10+210*j,10);
singleRadioButton[j].Size=new System.Drawing.Size(200,50);
switch (j)
{
case 0:
singleRadioButton[j].Text="A:";
break;
case 1:
singleRadioButton[j].Text="B:";
break;
case 2:
singleRadioButton[j].Text="C:";
break;
case 3:
singleRadioButton[j].Text="D:";
break;
}
testGroupBox[i].Controls.Add(singleRadioButton[j]);
#endregion
}
}
二、操作动态添加的控件。
为操作动态添加的控件,特声明一中第一行的代码和注释为“◆”的所在行的代码。
操作代码如下:
strRadioButtonName=((Label)mht["Label["+i.ToString()+"]"]).Text;
if(mht.Contains(strRadioButtonName))// 判断该控件是否存在
{
if(((RadioButton)mht[strRadioButtonName]).Checked==true)
{
((Label)mht["Label["+i.ToString()+"]"]).Text="●";
}
else
{
((Label)mht["Label["+i.ToString()+"]"]).Text="Х";
}
((RadioButton)mht[strRadioButtonName.Substring(0,strRadioButtonName.Length-3)+"[0]"]).Enabled=false;
((RadioButton)mht[strRadioButtonName.Substring(0,strRadioButtonName.Length-3)+"[1]"]).Enabled=false;
((RadioButton)mht[strRadioButtonName.Substring(0,strRadioButtonName.Length-3)+"[2]"]).Enabled=false;
((RadioButton)mht[strRadioButtonName.Substring(0,strRadioButtonName.Length-3)+"[3]"]).Enabled=false;
}
else
{
((Label)mht["Label["+i.ToString()+"]"]).Text="?";
}
((Label)mht["Label["+i.ToString()+"]"]).Visible=true;
使用语言:C#
一、动态添加控件代码:
Hashtable mht = new Hashtable();// 为操作动态添加的控件,特添加此代码
int N=10;
GroupBox [] testGroupBox=new GroupBox
;
for(int i=0;i<N;i++)
{
#region 动态添加GroupBox控件
testGroupBox[i]=new GroupBox();
testGroupBox[i].Location=new Point(30,60+120*i);
testGroupBox[i].Size=new System.Drawing.Size(900,100);
testGroupBox[i].Text=(i+1).ToString();
this.Controls.Add(testGroupBox[i]);
#endregion
#region 动态添加Label控件
Label [] correctLabel=new Label
;
correctLabel[i]=new Label();
correctLabel[i].Location=new Point(10,10);
correctLabel[i].Size=new System.Drawing.Size(15,15);
correctLabel[i].Text=="singleRadioButton["+i.ToString()+"][1]";
correctLabel[i].ForeColor=Color.Red;
correctLabel[i].Visible=false;
correctLabel[i].Name="Label["+i.ToString()+"]";
mht[correctLabel[i].Name]=correctLabel[i];// ◆
testGroupBox[i].Controls.Add(correctLabel[i]);
#endregion
#region 动态添加RadioButton控件
RadioButton [] singleRadioButton=new RadioButton[4];
for(int j=0;j<4;j++)
{
singleRadioButton[j]=new RadioButton();
singleRadioButton[j].Name="singleRadioButton["+i.ToString()+"]["+j.ToString()+"]";
mht[singleRadioButton[j].Name]=singleRadioButton[j];// ◆
singleRadioButton[j].Location=new Point(10+210*j,10);
singleRadioButton[j].Size=new System.Drawing.Size(200,50);
switch (j)
{
case 0:
singleRadioButton[j].Text="A:";
break;
case 1:
singleRadioButton[j].Text="B:";
break;
case 2:
singleRadioButton[j].Text="C:";
break;
case 3:
singleRadioButton[j].Text="D:";
break;
}
testGroupBox[i].Controls.Add(singleRadioButton[j]);
#endregion
}
}
二、操作动态添加的控件。
为操作动态添加的控件,特声明一中第一行的代码和注释为“◆”的所在行的代码。
操作代码如下:
strRadioButtonName=((Label)mht["Label["+i.ToString()+"]"]).Text;
if(mht.Contains(strRadioButtonName))// 判断该控件是否存在
{
if(((RadioButton)mht[strRadioButtonName]).Checked==true)
{
((Label)mht["Label["+i.ToString()+"]"]).Text="●";
}
else
{
((Label)mht["Label["+i.ToString()+"]"]).Text="Х";
}
((RadioButton)mht[strRadioButtonName.Substring(0,strRadioButtonName.Length-3)+"[0]"]).Enabled=false;
((RadioButton)mht[strRadioButtonName.Substring(0,strRadioButtonName.Length-3)+"[1]"]).Enabled=false;
((RadioButton)mht[strRadioButtonName.Substring(0,strRadioButtonName.Length-3)+"[2]"]).Enabled=false;
((RadioButton)mht[strRadioButtonName.Substring(0,strRadioButtonName.Length-3)+"[3]"]).Enabled=false;
}
else
{
((Label)mht["Label["+i.ToString()+"]"]).Text="?";
}
((Label)mht["Label["+i.ToString()+"]"]).Visible=true;
相关文章推荐
- 窗体动态添加控件时拉大问题
- android 动态添加控件控制属性问题
- 解决 easyui 动态添加控件时无法渲染的问题
- javascript控制服务器控件——实例:为DropdownList动态添加删除选项
- 动态添加控件要注意的问题
- WEB窗体用户控件的变量问题(动态)
- WPF感悟(3)——关于向窗体动态添加控件
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
- WPF感悟(3)——关于向窗体动态添加控件
- WinForm动态添加控件及其事件
- C#解决动态添加控件时,控件闪烁的问题
- 【转】C# 中动态添加控件及其事件
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
- [代码问题解决录A1]c#Listview控件动态添加数据并阻止重复添加
- 笔记(RemoveAll;DataGridView用linq查询;动态添加控件时闪烁问题)
- VB.NET在窗体上添加任意动态控件
- WPF感悟(3)——关于向窗体动态添加控件
- Eclipse下开发QT4应用,看不见添加的控件的问题及其解决方法
- 关于LinnerLayout动态添加的控件 并且给子控件动态设置Id 、点击的问题
- Android RecyclerView item动态添加控件重复问题