有代码如下using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace Winter_vacation_homework
{
public partial class FormShopFloor : Form
{
public FormShopFloor()
{
InitializeComponent();
}
//加载窗体时根据数据库动态创建大类按钮
private void FormShopFloor_Load(object sender, EventArgs e)
{
try
{
Sqlconnectionfactory.GetSqlConnection();
String sql = "select name from category";
SqlDataReader dr = Sqlconnectionfactory.Select(sql);
int count = 0;
while (dr.Read())
{
string btnName = dr.GetString(0);
System.Windows.Forms.Button newbtn = new System.Windows.Forms.Button();
//根据逻辑创建第二个button 按钮时也是newbtn 但没出错,为什么?
newbtn.Name = "btn" +count;
newbtn.Size = new System.Drawing.Size(75, 23);
newbtn.TabIndex = count;
newbtn.Text = btnName;
newbtn.UseVisualStyleBackColor = true;
this.splitContainer1.Panel1.Controls.Add(newbtn);
newbtn.Location = new System.Drawing.Point(28,45+45 * count);
newbtn.Click+= new System.EventHandler(this.newbtn_Click);
count++;
}
}
catch (SqlException sqlex)
{
Console.WriteLine(sqlex.Message);
}
finally
{ Sqlconnectionfactory.Close();
}
} private void newbtn_Click(object sender, EventArgs e)
{
string sql= "select name from subcategory";
//上面语句没有实现具体的大类按钮所要对应创建子类的tabpage,问题是如何得到上面由数据库创建的各个按钮大类按钮名,我估计这个是重点出错的环节
int count=0;
try
{
Sqlconnectionfactory.GetSqlConnection();
SqlDataReader dr = Sqlconnectionfactory.Select(sql);
while(dr.Read())
{
string tbgName = dr.GetString(0);
System.Windows.Forms.TabPage newTbg = new System.Windows.Forms.TabPage();
//根据逻辑创建第二个大类按钮的子类tabpage时也是 newTbg 这里却出错,为什么?
this.tabControl1.Controls.Add(newTbg);
newTbg.Name = "tbg"+count;
newTbg.Size = new System.Drawing.Size(495, 326);
newTbg.TabIndex = count;
newTbg.Text = tbgName;
newTbg.UseVisualStyleBackColor = true;
}
}
catch (SqlException sqlex)
{
Console.WriteLine(sqlex.Message);
}
finally
{ Sqlconnectionfactory.Close();
}
}
}
}问题就注释里,请高手指点,小弟感激不尽
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace Winter_vacation_homework
{
public partial class FormShopFloor : Form
{
public FormShopFloor()
{
InitializeComponent();
}
//加载窗体时根据数据库动态创建大类按钮
private void FormShopFloor_Load(object sender, EventArgs e)
{
try
{
Sqlconnectionfactory.GetSqlConnection();
String sql = "select name from category";
SqlDataReader dr = Sqlconnectionfactory.Select(sql);
int count = 0;
while (dr.Read())
{
string btnName = dr.GetString(0);
System.Windows.Forms.Button newbtn = new System.Windows.Forms.Button();
//根据逻辑创建第二个button 按钮时也是newbtn 但没出错,为什么?
newbtn.Name = "btn" +count;
newbtn.Size = new System.Drawing.Size(75, 23);
newbtn.TabIndex = count;
newbtn.Text = btnName;
newbtn.UseVisualStyleBackColor = true;
this.splitContainer1.Panel1.Controls.Add(newbtn);
newbtn.Location = new System.Drawing.Point(28,45+45 * count);
newbtn.Click+= new System.EventHandler(this.newbtn_Click);
count++;
}
}
catch (SqlException sqlex)
{
Console.WriteLine(sqlex.Message);
}
finally
{ Sqlconnectionfactory.Close();
}
} private void newbtn_Click(object sender, EventArgs e)
{
string sql= "select name from subcategory";
//上面语句没有实现具体的大类按钮所要对应创建子类的tabpage,问题是如何得到上面由数据库创建的各个按钮大类按钮名,我估计这个是重点出错的环节
int count=0;
try
{
Sqlconnectionfactory.GetSqlConnection();
SqlDataReader dr = Sqlconnectionfactory.Select(sql);
while(dr.Read())
{
string tbgName = dr.GetString(0);
System.Windows.Forms.TabPage newTbg = new System.Windows.Forms.TabPage();
//根据逻辑创建第二个大类按钮的子类tabpage时也是 newTbg 这里却出错,为什么?
this.tabControl1.Controls.Add(newTbg);
newTbg.Name = "tbg"+count;
newTbg.Size = new System.Drawing.Size(495, 326);
newTbg.TabIndex = count;
newTbg.Text = tbgName;
newTbg.UseVisualStyleBackColor = true;
}
}
catch (SqlException sqlex)
{
Console.WriteLine(sqlex.Message);
}
finally
{ Sqlconnectionfactory.Close();
}
}
}
}问题就注释里,请高手指点,小弟感激不尽
解决方案 »
- 200分求个小实例,怎么自动登录网站
- console.writeline()没看懂
- (急!!!!!)无法在 System.Guid 和 System.String 上执行“=”操作
- Visul Studio .NET 2005熟练的进(急!!!!!!!!!!!!!!!!!!!!!!)
- winform中取值问题
- 请教一个string转float的问题,谢谢。
- 请问有没有关于下载机制的方法,或代码,小弟郁闷
- 关于Asp.Net 页面 @ Page 问题
- c# Tamir.SharpSsh连接sftp连不上,求助
- 急请教:快捷键问题
- windows应用程序怎么做压力测试?
- 怎么实现拖动的功能啊?急!!!!!!
this.tabControl1.TabPages.Add(newTbg);
试试
{
string sql= "select name from subcategory";
//上面语句没有实现具体的大类按钮所要对应创建子类的tabpage,问题是如何得到上面由数据库创建的各个按钮大类按钮名,我估计这个是重点出错的环节
int count=0;
try
{
Sqlconnectionfactory.GetSqlConnection();
SqlDataReader dr = Sqlconnectionfactory.Select(sql);
while(dr.Read())
{
string tbgName = dr.GetString(0);
System.Windows.Forms.TabPage newTbg = new System.Windows.Forms.TabPage();
//根据逻辑创建第二个大类按钮的子类tabpage时也是 newTbg 这里却出错,为什么?
this.tabControl1.Controls.Add(newTbg);
newTbg.Name = "tbg"+count;
newTbg.Size = new System.Drawing.Size(495, 326);
newTbg.TabIndex = count;
newTbg.Text = tbgName;
newTbg.UseVisualStyleBackColor = true;
}
}
的这个sql语句不知道怎么写,因为上面产生的button不知道怎么得到它的名字啊,如果能得到名字就可以写成 string sql= "select name from subcategory where categry='"+按钮显示的文本"'";