1、程序
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;
namespace ComboBoxApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
RecordInitialize();
}
#region 输入16进制数测试
private void button1_Click(object sender, EventArgs e)
{
string str = comboBox1.Text;
Console.WriteLine(str);
}
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
ComboBox comboBox = sender as ComboBox;
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'a' && e.KeyChar <= 'f') || (e.KeyChar >= 'A' && e.KeyChar <= 'F') || e.KeyChar == '\b')
{
if (e.KeyChar >= 'a' && e.KeyChar <= 'f')
{
e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper());
}
e.Handled = false;
}
else
{
e.Handled = true;
}
}
#endregion
#region 记录增减测试
public int num = 1;
private void RecordInitialize()
{
cbbDepartment.Items.AddRange(new string[] { "销售部", "研发部", "人事部" });
cbbSex.Items.AddRange(new string[] { "男", "女" });
}
private void cbbDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
cbbPosition.Items.Clear();
int selectIndex=cbbDepartment.SelectedIndex;
switch(selectIndex)
{
case 0:
cbbPosition.Items.AddRange(new string[] { "销售经理", "销售专员" });
break;
case 1:
cbbPosition.Items.AddRange(new string[] { "研发经理", "硬件工程师" });
break;
case 2:
cbbPosition.Items.AddRange(new string[] { "人事经理", "人事专员" });
break;
}
}
private void BtnAdd_Click(object sender, EventArgs e)
{
if(tbxName==null && cbbDepartment.SelectedItem==null &&cbbPosition.SelectedItem==null && cbbSex.SelectedItem == null)
{
MessageBox.Show("内容未填写完成");
}
Panel panel = new Panel();
panel.Location = new Point(0, num * 30);
panel.Size = new Size(500, 30);
AddContent(panel);
panelContent.Controls.Add(panel);
num++;
}
private void BtnDelete_Click(object sender, EventArgs e)
{
if(panelContent.Controls.Count > 0)
{
Panel panel = (Panel)panelContent.Controls[panelContent.Controls.Count - 1];
panelContent.Controls.Remove(panel);
panel.Dispose();
num--;
}
}
public void AddContent(Panel panel)
{
List<Label> labels = new List<Label>();
Label label = new Label();
label.Text=num.ToString();
labels.Add(label);
label = new Label();
label.Text = tbxName.Text; ;
labels.Add(label);
label = new Label();
label.Text = cbbDepartment.Text; ;
labels.Add(label);
label = new Label();
label.Text = cbbPosition.Text; ;
labels.Add(label);
label = new Label();
label.Text = cbbSex.Text; ;
labels.Add(label);
int margin = 30;
foreach(Label item in labels)
{
item.Font = new Font("宋体", 10);
item.Size = new Size(100, 18);
item.Location=new Point(margin, 15);
panel.Controls.Add(item);
margin += 100;
}
}
#endregion
}
}
2、运行效果