C#.net动态GUI [英] c#.net dynamic gui

查看:50
本文介绍了C#.net动态GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c#.net中动态添加多个复选框
我有三张桌子
1.包含标签的标签
2.包含在指定标签中带有的组的组
3.包含指定组中的按钮等控件的项目

代码是

公共无效CreateCheckBoxes()
{
int TabID,GroupID,ItemID,ControlID;
字符串TabName,TabCaption,GroupName,GroupCaption,ItemName,ItemCaption,ControlName,ControlCaption;
//System.Windows.Forms.CheckBox check = null;
//System.Windows.Forms.CheckBox check1 = null;
//System.Windows.Forms.CheckBox check2 = null;
OleDbConnection conn =新的OleDbConnection(gb.autocreateconmsacess());
conn.Open();
字符串str = @从选项卡中选择*";
OleDbCommand cmd =新的OleDbCommand(str,conn);
OleDbDataReader DataReaderr = cmd.ExecuteReader();
Console.WriteLine(DataReaderr.FieldCount);

而(DataReaderr.Read())
{
CheckBox checkj = new CheckBox();
TabID = Convert.ToInt32(DataReaderr [0]);
TabName = DataReaderr [1] .ToString();
TabCaption = DataReaderr [2] .ToString();
//CheckBox checkj = new CheckBox();
checkj.Text = TabCaption;

this.groupBox1.Controls.Add(checkj);


//TabHome.Id = TabID;
OleDbDataReader ReadGroup;
字符串str1 =从其中active = yes和Tab_ID =" + TabID;的组中选择*;

OleDbDataAdapter da =新的OleDbDataAdapter(str1,conn);
DataSet dsGroups = new DataSet();
da.Fill(dsGroups,"Groups");

for(int i = 0; i& lt; dsGroups.Tables [0] .Rows.Count; i ++)
{
CheckBox check1 = new CheckBox();
GroupID = Convert.ToInt32(dsGroups.Tables [0] .Rows [i] ["ID"]);
GroupName = Convert.ToString(dsGroups.Tables [0] .Rows [i] ["Name"]);
GroupCaption = Convert.ToString(dsGroups.Tables [0] .Rows [i] ["Caption"]));
check1.Text = GroupCaption;
this.groupBox2.Controls.Add(check1);





字符串str2 =从其中active = yes和Group_id =" + GroupID;的项目中选择*;
//OleDbCommand CmdControls = new OleDbCommand(str2,conns);

OleDbDataAdapter daa =新的OleDbDataAdapter(str2,conn);
DataSet dsItems = new DataSet();
daa.Fill(dsItems,"Items");
CheckBox check2 = new CheckBox();
for(int j = 0; j& lt; dsItems.Tables [0] .Rows.Count; j ++)
{

ControlID = Convert.ToInt32(dsItems.Tables [0] .Rows [j] ["Item_id"]);
ControlName = dsItems.Tables [0] .Rows [j] ["Caption"].ToString();
ControlCaption = dsItems.Tables [0] .Rows [j] ["Name"].ToString();
check2.Text = ControlCaption;
this.groupBox3.Controls.Add(new CheckBox());
check2.Contains(new CheckBox());
//check2.Text = ControlCaption;
//check2.Tag = j;
//this.groupBox3.Controls.Add(check2);



}
}

创建的复选框,但在三个文本框中只有1-1

表示需要最后一个标签
最后一组
最后一项

谢谢你的回答先生.克里斯蒂安·格雷斯
我认为您现在告诉我更好了.

i want to add more than one check boxes dynamically in c#.net
i have three tables
1. tab which contain tabs
2. groups which contain groups with in specified tab
3. items which hold controls like buttons etc within specified groups

the code is

public void CreateCheckBoxes()
{
int TabID, GroupID, ItemID, ControlID;
String TabName, TabCaption, GroupName, GroupCaption, ItemName, ItemCaption, ControlName, ControlCaption;
//System.Windows.Forms.CheckBox check=null;
//System.Windows.Forms.CheckBox check1 = null;
//System.Windows.Forms.CheckBox check2 = null;
OleDbConnection conn = new OleDbConnection(gb.autocreateconmsacess());
conn.Open();
string str = @"Select * from Tabs";
OleDbCommand cmd = new OleDbCommand(str, conn);
OleDbDataReader DataReaderr = cmd.ExecuteReader();
Console.WriteLine(DataReaderr.FieldCount);

while (DataReaderr.Read())
{
CheckBox checkj = new CheckBox();
TabID = Convert.ToInt32(DataReaderr[0]);
TabName = DataReaderr[1].ToString();
TabCaption = DataReaderr[2].ToString();
// CheckBox checkj = new CheckBox();
checkj.Text = TabCaption;

this.groupBox1.Controls.Add(checkj);


// TabHome.Id = TabID;
OleDbDataReader ReadGroup;
string str1 = "select * from Groups where active=yes and Tab_ID=" + TabID;

OleDbDataAdapter da = new OleDbDataAdapter(str1, conn);
DataSet dsGroups = new DataSet();
da.Fill(dsGroups, "Groups");

for (int i = 0; i < dsGroups.Tables[0].Rows.Count; i++)
{
CheckBox check1 = new CheckBox();
GroupID = Convert.ToInt32(dsGroups.Tables[0].Rows[i]["ID"]);
GroupName = Convert.ToString(dsGroups.Tables[0].Rows[i]["Name"]);
GroupCaption = Convert.ToString(dsGroups.Tables[0].Rows[i]["Caption"]);
check1.Text=GroupCaption;
this.groupBox2.Controls.Add(check1);





string str2 = "select * from Items where active=yes and Group_id=" + GroupID;
// OleDbCommand CmdControls = new OleDbCommand(str2, conns);

OleDbDataAdapter daa = new OleDbDataAdapter(str2, conn);
DataSet dsItems = new DataSet();
daa.Fill(dsItems, "Items");
CheckBox check2 = new CheckBox();
for (int j = 0; j < dsItems.Tables[0].Rows.Count; j++)
{

ControlID = Convert.ToInt32(dsItems.Tables[0].Rows[j]["Item_id"]);
ControlName = dsItems.Tables[0].Rows[j]["Caption"].ToString();
ControlCaption = dsItems.Tables[0].Rows[j]["Name"].ToString();
check2.Text = ControlCaption;
this.groupBox3.Controls.Add(new CheckBox());
check2.Contains(new CheckBox());
// check2.Text = ControlCaption;
// check2.Tag = j;
// this.groupBox3.Controls.Add(check2);



}
}

the check boxes created but only 1-1 in three text boxes

means it take last tab
last group
last item

thanks for ur answer mr. christian grause
i think u tell me better now

推荐答案

好,所以您创建它们并将它们添加到表单的控件集合中.我认为这是winforms,如果是ASP.NET或WPF,您会告诉我们的,对吗?

这看起来很杂乱-您在三个位置添加了复选框,您是否单步执行了代码?试图删除代码以添加复选框以查看哪个复选框在工作?
OK, so you create them and add them to your form''s control collection. I assume this is winforms, if it was ASP.NET or WPF, you''d have told us, right ?

This looks messy - you add checkboxes in three places, have you stepped through the code ? Tried removing code to add checkboxes to see which one is working ?


这篇关于C#.net动态GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆