110,571
社区成员
发帖
与我相关
我的任务
分享
#region[年级、院系对应表]
try
{
foreach (Control con in Controls)
if (con is ComboBox)
(con as ComboBox).DropDownStyle = ComboBoxStyle.DropDownList;
ds_Yuanxi = DBHelper.GetSet("select * from com_basedata where datatype='院系'");
for (int i = 0; i < ds_Yuanxi.Tables[0].Rows.Count; i++)
comboBoxYuanxi.Items.Add(ds_Yuanxi.Tables[0].Rows[i][1]);
}
catch (Exception)
{
MessageBox.Show("数据库连接错误 .\n可能的原因有:\n1.数据库服务没打开\n2.数据库文件未附加\nApp.Config配置未修改 .\n程序已退出 .", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
Close();
}
#endregion
//动态添加按钮
if (ds_Yuanxi.Tables[0].Rows.Count > 0)
{
int n = ds_Yuanxi.Tables[0].Rows.Count;
int a = 1;
for (; a <= n; a++)
{
Button btnYuanxi = new Button();
this.Controls.Add(btnYuanxi);
btnYuanxi.Text = ds_Yuanxi.Tables[0].Rows[a - 1][1].ToString();
btnYuanxi.SetBounds(10, 120 + 80 * (a - 1), 180, 50); //设置按钮位置
btnYuanxi.PerformClick();
btnYuanxi.Click += new EventHandler(btnYuanxi_Click);
}
}
else
{
MessageBox.Show("miss");
}
C# code
int x = 10, y = 10;
for (int i = 0; i < 20; i++)
{
Button btnAdd = new Button();
this.Controls.Add(btnAdd);
btnAdd.Text = i.ToString();
btnAdd.SetBounds(x, y, 180, 50);
x = 10 + (i / 6) * 180;// i/6代表现在是第几列,这里没有加上间距
y = 10 + (i % 6) * 50;// i%6代表现在是第几行,这里没有加上间距
}
请问这里的间距如何设置,对这个函数不太熟悉!
private void FormYuanxi_Load(object sender, EventArgs e)
{
groupBox1.BackColor = Color.Transparent; //GroupBox的背景设为透明
FindYuanxi findyuanxi = new FindYuanxi();
findyuanxi.DoubleBuffered = true; //加快运行速度,双缓冲处理
comboBoxYuanxi.Visible = false;
textBoxNianji.Text = cxl.nianji;
#region[年级、院系对应表]
try
{
foreach (Control con in Controls)
if (con is ComboBox)
(con as ComboBox).DropDownStyle = ComboBoxStyle.DropDownList;
ds_Yuanxi = DBHelper.GetSet("select * from com_basedata where datatype='院系'");
for (int i = 0; i < ds_Yuanxi.Tables[0].Rows.Count; i++)
comboBoxYuanxi.Items.Add(ds_Yuanxi.Tables[0].Rows[i][1]);
//comboBoxYuanxi.SelectedIndex = 0;
}
catch (Exception)
{
MessageBox.Show("数据库连接错误 .\n可能的原因有:\n1.数据库服务没打开\n2.数据库文件未附加\nApp.Config配置未修改 .\n程序已退出 .", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
Close();
}
#endregion
//动态添加按钮
if (ds_Yuanxi.Tables[0].Rows.Count > 0)
{
int n = ds_Yuanxi.Tables[0].Rows.Count;
int a = 1;
for (; a <= n; a++)
{
Button btnYuanxi = new Button();
this.Controls.Add(btnYuanxi);
btnYuanxi.Text = ds_Yuanxi.Tables[0].Rows[a - 1][1].ToString();
Font newFont = new Font("宋体", 12); //按钮字体大小
btnYuanxi.Font = newFont;
btnYuanxi.SetBounds(10, 120 + 80 * (a - 1), 180, 50); //设置按钮位置
btnYuanxi.PerformClick();
btnYuanxi.Click += new EventHandler(btnYuanxi_Click);
}
}
else
{
MessageBox.Show("miss");
}
}
private void btnYuanxi_Click(object sender, EventArgs e)
{
Button btnzhuanye = (Button)sender;
cxl.xueyuan = btnzhuanye.Text;
textBoxYuanxi.Text = cxl.xueyuan;
//MessageBox.Show(btnzhuanye.Text);
FindZhuanye findzhuanye = new FindZhuanye();
findzhuanye.ShowDialog();
}
这是全源码,btnYuanxi.PerformClick(); 这个是不能去掉的。
int x = 10, y = 10;
for (int i = 0; i < 20; i++)
{
Button btnAdd = new Button();
this.Controls.Add(btnAdd);
btnAdd.Text = i.ToString();
btnAdd.SetBounds(x, y, 180, 50);
x = 10 + (i / 6) * 180;// i/6代表现在是第几列,这里没有加上间距
y = 10 + (i % 6) * 50;// i%6代表现在是第几行,这里没有加上间距
}