WInform记录的添加和显示

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、运行效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值