110,561
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
string str;
string cbbname;
private int i = 0;
ComboBox cbb;
public Form2()
{
InitializeComponent();
}
//button1为动态添加ComboBox键
private void button1_Click(object sender, EventArgs e)
{
i++;
cbb = new ComboBox();
cbb.Name = "cbb" + i;
cbb.Location = new System.Drawing.Point(90, i * 30);
cbb.Size = new System.Drawing.Size(120, 21);
SqlConnection conn = new SqlConnection(Properties.Settings.Default.测试专用ConnectionString);
conn.Open();
string str = "select DeptName from Table_1";
SqlCommand cmd = new SqlCommand(str, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
cbb.Items.Add(reader["DeptName"]);
}
reader.Close();
conn.Close();
this.Controls.Add(cbb);
//添加一个触发一次button3,不知道对不对?
cbb.SelectedIndexChanged += new EventHandler(button3_Click);
}
//链接“测试专用”数据库
private void Form2_Load(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(Properties.Settings.Default.测试专用ConnectionString);
conn.Open();
string str = "select DeptName from Table_1";
SqlCommand cmd = new SqlCommand(str, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
this.comboBox1.Items.Add(reader["DeptName"]);
}
reader.Close();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//将所有comboBox中的选中内容合并到richTextBox中
private void button3_Click(object sender, EventArgs e)
{
if (i == 0)
{
richTextBox1.Text = comboBox1.Text;
}
else
{
foreach (Control c in this.Controls)
{
if (c is ComboBox)
{
ComboBox cbb = (ComboBox)c;
cbbname = ("cbb" + i).ToString();
if (cbb.Name.ToString() == cbbname.ToString())
{
i--;
//this.Controls.Remove(c);
str += "," + cbb.Text;
richTextBox1.Text = comboBox1.Text + str;
}
}
}
}
}
//删除键:实现逐一删除ComboBox控件
private void button4_Click(object sender, EventArgs e)
{
foreach(Control c in this.Controls)
{
if (c is ComboBox)
{
ComboBox cbb = (ComboBox)c;
cbbname = ("cbb" + i).ToString();
if (cbb.Name.ToString() == cbbname.ToString())
{
i--;
this.Controls.Remove(c);
}
}
}
}
}
}
ArrayList array = new ArrayList();
在button1中加句:
[code=csharp]array.Add(cbb.Text);
cbb.TextChanged += cbb_TextChanged;
然后void cbb_TextChanged(object sender, EventArgs e)
{
foreach (Control text in this.Controls)
{
if (text is ComboBox)
{
ComboBox coo = (ComboBox)text;
if (coo.Focused)
{
index = coo.TabIndex;
array.RemoveAt((index - 6));
array.Insert((index - 6), coo.Text);
//MessageBox.Show(aaa.ToString());
}
}
}
//throw new NotImplementedException();
}
//删除键:
private void button4_Click(object sender, EventArgs e)
{
if (i == 0)
MessageBox.Show("已经无法删除了");
cbbname = ("cbb" + i).ToString();//剩下的最后一个创建的combobox控件的名称
//按照控制顺序来删除combobox
foreach (Control c in this.Controls)
{
if (c is ComboBox)
{
ComboBox cbb = (ComboBox)c; //当前选择的combobox控件
if (cbb.Name.ToString() == cbbname.ToString())
{
int ee = i-1;
i--;
array.RemoveAt(ee);
this.Controls.Remove(c);
}
}
}
} [/code]