Winform笔记1129 checkbox listbox combabox checkedlistbox datapacker

checkbox

csharp 复制代码
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 WinFormsApp1.ControlForms
{
    public partial class FormCheckBox : Form
    {
        public FormCheckBox()
        {
            InitializeComponent();
            foreach (Control c in panelPosition.Controls)
            {//遍历控件集合
                CheckBox checkBox = c as CheckBox;
                checkBox.CheckedChanged += CheckBox_CheckedChanged;
            }
        }
        List<string> selPositions = new List<string>();  
        private void CheckBox_CheckedChanged(object? sender, EventArgs e)
        {
            CheckBox checkBox = sender as CheckBox;
            if (checkBox.Checked)//选中
            {
                selPositions.Add(checkBox.Text);
            }
            else { 
            selPositions.Remove(checkBox.Text);
            }
            string strPositions=string.Join(",", selPositions);
            labelPositionCh.Text = strPositions;
                        
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {

        }
        //check change
        private void cBoxState_CheckedChanged(object sender, EventArgs e)
        {
            if (cBoxState.Checked)
            {
                labelRes.Text = "Position is:normal";
            }
            else
            {
                labelRes.Text = "Position is:frozen";
            }
        }
        //后发生 选中状态改变
        private void cBoxState_CheckStateChanged(object sender, EventArgs e)
        {
            if (cBoxState.CheckState == CheckState.Checked)
            {
                labelRes.Text = "Position is:normal";
            }
            else if (cBoxState.CheckState == CheckState.Unchecked)
            {
                labelRes.Text = "Position is:frozen";
            }
            else if (cBoxState.CheckState == CheckState.Indeterminate)
            {
                labelRes.Text = "Position is:unsure";
            }
        }

        //获取选择结果
        private void butSub_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (Control c in panelPosition.Controls)
            {//遍历控件集合
                CheckBox checkBox = c as CheckBox;
                if (checkBox.Checked)
                {
                    list.Add(checkBox.Text);
                }
            }
            string strPositions = string.Join(",", list);
            labelPositionCh.Text = strPositions;

        }

        private void label2_Click_1(object sender, EventArgs e)
        {

        }
    }
}

listbox







csharp 复制代码
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 WinFormsApp1.ControlForms
{
    public partial class FormListBox : Form
    {
        public FormListBox()
        {
            InitializeComponent();
        }

        private void listBoxNames_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void FormListBox_Load(object sender, EventArgs e)
        {
            //clear
            listBoxNames.Items.Clear();
            //add
            listBoxNames.Items.Add("kk");//添加单个项
            listBoxNames.Items.AddRange(new string[] { "ko", "ko1", "ko2" });
            listBoxNames.Items.Add(444);
            listBoxNames.Items.Add(9.5);
            //insert
            listBoxNames.Items.Insert(2,"po");


        }
    }
}
csharp 复制代码
        private void butAdd_Click(object sender, EventArgs e)
        {
            listBoxNames.Items.Clear();
            List<string> names = new List<string>();
            names.Add("okl");
            names.Add("okl1");
            names.Add("okl2");
            names.Add("okl3");
            names.Add("okl4");
            for (int i = 0; i < names.Count; i++)
            {
                listBoxNames.Items.Add(names[i]);
            }
        }


csharp 复制代码
        }
        //动态添加的项 可以移除 数据绑定的 items.remove .add无效
        private void button1_Click(object sender, EventArgs e)
        {
            listBoxNames.Items.Remove("okl4");
            listBoxNames.Items.Remove(1);//指定位置
            
        }
    }



csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormsApp1.Models
{
    public class VideoStore
    {
        public int Id { get; set; }//编号
        public string Name { get; set; }//名称
    }
}
csharp 复制代码
private void butStress_Click(object sender, EventArgs e)
{
    List<VideoStore> videolist=new List<VideoStore>();
    videolist.Add(new VideoStore() { Id = 142, Name = "royd-142" });
    videolist.Add(new VideoStore() { Id = 143, Name = "royd-143" });
    videolist.Add(new VideoStore() { Id = 144, Name = "royd-144" });
    videolist.Add(new VideoStore() { Id = 145, Name = "royd-145" });
    //绑定
  listBoxVideos.DataSource= videolist; 
    listBoxVideos.DisplayMember="Name";
    listBoxVideos.ValueMember = "id";
}
csharp 复制代码
        private void butStress_Click(object sender, EventArgs e)
        {
            List<VideoStore> videolist=new List<VideoStore>();
            videolist.Add(new VideoStore() { Id = 142, Name = "royd-142" });
            videolist.Add(new VideoStore() { Id = 143, Name = "royd-143" });
            videolist.Add(new VideoStore() { Id = 144, Name = "royd-144" });
            videolist.Add(new VideoStore() { Id = 145, Name = "royd-145" });
            //绑定
          listBoxVideos.DataSource= videolist; 
            listBoxVideos.DisplayMember="Name";
            listBoxVideos.ValueMember = "id";

            //绑定后无法修改
            listBoxVideos.Items.Add("kk");
            listBoxVideos.Items.Remove(142);
        }







csharp 复制代码
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 WinFormsApp1.Models;

namespace WinFormsApp1.ControlForms
{
    public partial class FormListBox : Form
    {
        public FormListBox()
        {
            InitializeComponent();
        }

        private void listBoxNames_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void FormListBox_Load(object sender, EventArgs e)
        {
            //clear
            listBoxNames.Items.Clear();
            //add
            listBoxNames.Items.Add("kk");//添加单个项
            listBoxNames.Items.AddRange(new string[] { "ko", "ko1", "ko2" });
            listBoxNames.Items.Add(444);
            listBoxNames.Items.Add(9.5);
            //insert
            listBoxNames.Items.Insert(2, "po");


        }

        private void butAdd_Click(object sender, EventArgs e)
        {
            listBoxNames.Items.Clear();
            List<string> names = new List<string>();
            names.Add("okl");
            names.Add("okl1");
            names.Add("okl2");
            names.Add("okl3");
            names.Add("okl4");
            //  
            for (int i = 0; i < names.Count; i++)
            {
                listBoxNames.Items.Add(names[i]);
            }
            listBoxNames.SelectedIndex = 0;//第一项选择
        }
        //动态添加的项 可以移除 数据绑定的 items.remove .add无效
        private void button1_Click(object sender, EventArgs e)
        {
            listBoxNames.Items.Remove("okl4");
            listBoxNames.Items.Remove(1);//指定位置

        }

        private void butStress_Click(object sender, EventArgs e)
        {
            List<VideoStore> videolist=new List<VideoStore>();
            videolist.Add(new VideoStore() { Id = 142, Name = "royd-142" });
            videolist.Add(new VideoStore() { Id = 143, Name = "royd-143" });
            videolist.Add(new VideoStore() { Id = 144, Name = "royd-144" });
            videolist.Add(new VideoStore() { Id = 145, Name = "royd-145" });
            //绑定
          listBoxVideos.DataSource= videolist; 
            listBoxVideos.DisplayMember="Name";
            listBoxVideos.ValueMember = "id";
            listBoxVideos.SelectedIndex = 1;
            //绑定后无法修改
            //listBoxVideos.Items.Add("kk");
            //listBoxVideos.Items.Remove(142);
        }
    }
}





index应该是位置 不是id

csharp 复制代码
        private void butSub_Click(object sender, EventArgs e)
        {
            //项类型:字符串
            if (listBoxNames.SelectedItem != null) {
                var stringItem = listBoxNames.SelectedItem.ToString();
            }
            string sname=listBoxNames.Text;
          //  var val = listBoxNames.SelectedValue;
          //项类型:对象类型
            //1
            var selectItem=listBoxVideos.SelectedItem as VideoStore;
            string name = selectItem.Name;
            int id= selectItem.Id;
            //2.编号
           int vid =Convert.ToInt32( listBoxNames.SelectedValue);
           // int vid =listBoxVideos.SelectedIndex;
        }


csharp 复制代码
private void butSub_Click(object sender, EventArgs e)
{//单选
    {//项类型:字符串
        //if (listBoxNames.SelectedItem != null)
        //{
        //    var stringItem = listBoxNames.SelectedItem.ToString();
        //}
        //string sname = listBoxNames.Text;
        ////  var val = listBoxNames.SelectedValue;
        ////项类型:对象类型
        ////1
        //var selectItem = listBoxVideos.SelectedItem as VideoStore;
        //string name = selectItem.Name;
        //int id = selectItem.Id;
        ////2.编号
        //int vid = Convert.ToInt32(listBoxNames.SelectedValue);
        //// int vid =listBoxVideos.SelectedIndex;
    }
    //多选
    {
        //选择项的
        List<string>vsNames=new List<string>();
        foreach(var item in listBoxVideos.SelectedItems)
        {
            VideoStore vdInfo=item as VideoStore;
            vsNames.Add(vdInfo.Name);
        }
        listBoxSelVideos.Items.AddRange(vsNames.ToArray());
    }
csharp 复制代码
                //编号
                List<int>vsIds= new List<int>();
                foreach (int index in listBoxVideos.SelectedIndices)
                {
                    int id = (listBoxVideos.Items[index]as VideoStore).Id;
                    vsIds.Add(id);
                }
                //值类型 对象类型
                listBoxSelVideos.Items.AddRange(vsIds.Select(id=>id.ToString()).ToArray());
            }


csharp 复制代码
  private void listBoxNames_SelectedIndexChanged(object sender, EventArgs e)
  {
      var selectItem = listBoxVideos.SelectedItem as VideoStore;
      textBoxStoreInfo.Text = $"the store of your choice:num" +
          $"{selectItem.Id} Name {selectItem.Name}";
  }
csharp 复制代码
   private void listBoxNames_SelectedIndexChanged(object sender, EventArgs e)
   {
       string name = listBoxNames.Text;
       textBoxStoreInfo.Text = $"the people you choice:name {name}";
   }
csharp 复制代码
 private void listBoxNames_SelectedIndexChanged(object sender, EventArgs e)
 {
     // string name = listBoxNames.Text;
     string name = listBoxNames.SelectedItem.ToString();
     textBoxStoreInfo.Text = $"the people you choice:name {name}";
 }


csharp 复制代码
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 WinFormsApp1.Models;

namespace WinFormsApp1.ControlForms
{
    public partial class FormListBox : Form
    {
        public FormListBox()
        {
            InitializeComponent();
        }

        private void listBoxNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            // string name = listBoxNames.Text;
            if (listBoxNames.SelectedItem != null)
            {
                string name = listBoxNames.SelectedItem.ToString();
                textBoxStoreInfo.Text = $"the people you choice:name {name}";
            }
            else
            {
                textBoxStoreInfo.Text = "please choose a choice";
            }
        }

        private void FormListBox_Load(object sender, EventArgs e)
        {
            //clear
            listBoxNames.Items.Clear();
            //add
            listBoxNames.Items.Add("kk");//添加单个项
            listBoxNames.Items.AddRange(new string[] { "ko", "ko1", "ko2" });
            listBoxNames.Items.Add(444);
            listBoxNames.Items.Add(9.5);
            //insert
            listBoxNames.Items.Insert(2, "po");


        }

        private void butAdd_Click(object sender, EventArgs e)
        {
            listBoxNames.Items.Clear();
            List<string> names = new List<string>();
            names.Add("okl");
            names.Add("okl1");
            names.Add("okl2");
            names.Add("okl3");
            names.Add("okl4");
            //  
            for (int i = 0; i < names.Count; i++)
            {
                listBoxNames.Items.Add(names[i]);
            }
            //  listBoxNames.SelectedIndex = 0;//第一项选择
            listBoxNames.SelectedItem = "okl4";
        }
        //动态添加的项 可以移除 数据绑定的 items.remove .add无效
        private void button1_Click(object sender, EventArgs e)
        {
            listBoxNames.Items.Remove("okl4");
            listBoxNames.Items.Remove(1);//指定位置

        }

        List<VideoStore> vslist = new List<VideoStore>();
        private void butStress_Click(object sender, EventArgs e)
        {
            List<VideoStore> videolist = new List<VideoStore>();
            videolist.Add(new VideoStore() { Id = 142, Name = "royd-142" });
            videolist.Add(new VideoStore() { Id = 143, Name = "royd-143" });
            videolist.Add(new VideoStore() { Id = 144, Name = "royd-144" });
            videolist.Add(new VideoStore() { Id = 145, Name = "royd-145" });
            //绑定
            listBoxVideos.DataSource = videolist;
            listBoxVideos.DisplayMember = "Name";
            listBoxVideos.ValueMember = "Id";
            listBoxVideos.SelectedIndex = 1;
            //绑定后无法修改
            //listBoxVideos.Items.Add("kk");
            //listBoxVideos.Items.Remove(142);
        }

        private void butSub_Click(object sender, EventArgs e)
        {//单选
            {//项类型:字符串
                //if (listBoxNames.SelectedItem != null)
                //{
                //    var stringItem = listBoxNames.SelectedItem.ToString();
                //}
                //string sname = listBoxNames.Text;
                ////  var val = listBoxNames.SelectedValue;
                ////项类型:对象类型
                ////1
                //var selectItem = listBoxVideos.SelectedItem as VideoStore;
                //string name = selectItem.Name;
                //int id = selectItem.Id;
                ////2.编号
                //int vid = Convert.ToInt32(listBoxNames.SelectedValue);
                //// int vid =listBoxVideos.SelectedIndex;
            }
            //多选
            {
                //选择项的名称
                //List<string>vsNames=new List<string>();
                //foreach(var item in listBoxVideos.SelectedItems)
                //{
                //    VideoStore vdInfo=item as VideoStore;
                //    vsNames.Add(vdInfo.Name);
                //}
                //listBoxSelVideos.Items.AddRange(vsNames.ToArray());
                //编号
                List<int> vsIds = new List<int>();
                foreach (int index in listBoxVideos.SelectedIndices)
                {
                    // int id = (listBoxVideos.Items[index]as VideoStore).Id;
                    int id = vslist[index].Id;//编号
                    vsIds.Add(id);
                }
                //值类型 对象类型
                listBoxSelVideos.Items.AddRange(vsIds.Select(id => id.ToString()).ToArray());
            }

        }

        //private void listBoxNames_SelectedIndexChanged_1(object sender, EventArgs e)
        //{

        //}

        private void listBoxVideosChanged(object sender, EventArgs e)
        {
            var selectItem = listBoxVideos.SelectedItem as VideoStore;
            textBoxStoreInfo.Text = $"the store of your choice:num" +
                $"{selectItem.Id} Name {selectItem.Name}";
        }

        private void butRight_Click(object sender, EventArgs e)
        {
            if (listBoxNames.SelectedItem != null)
            {
                var list = new List<object>();
                foreach(var item in listBoxNames.SelectedItems)
                {
                    list.Add(item);
                }
               foreach(var item in list)
                {
                    listBoxNames.Items.Remove(item);
                    listBoxSelNames.Items.Add(item);
                }
            }

        }

        private void butRightAll_Click(object sender, EventArgs e)
        {

        }

        private void butLeft_Click(object sender, EventArgs e)
        {

        }

        private void butLeftAll_Click(object sender, EventArgs e)
        {

        }
    }
}
csharp 复制代码
 private void butLeft_Click(object sender, EventArgs e)
 {
     if (listBoxSelNames.SelectedItem != null)
     {
         var list = new List<object>();
         foreach (var item in listBoxSelNames.SelectedItems)
         {
             list.Add(item);
         }
         foreach (var item in list)
         {

             listBoxSelNames.Items.Remove(item);
             listBoxNames.Items.Add(item);
         }
     }
 }
csharp 复制代码
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 WinFormsApp1.Models;

namespace WinFormsApp1.ControlForms
{
    public partial class FormListBox : Form
    {
        public FormListBox()
        {
            InitializeComponent();
        }

        private void listBoxNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            // string name = listBoxNames.Text;
            if (listBoxNames.SelectedItem != null)
            {
                string name = listBoxNames.SelectedItem.ToString();
                textBoxStoreInfo.Text = $"the people you choice:name {name}";
            }
            else
            {
                textBoxStoreInfo.Text = "please choose a choice";
            }
        }

        private void FormListBox_Load(object sender, EventArgs e)
        {
            //clear
            listBoxNames.Items.Clear();
            //add
            listBoxNames.Items.Add("kk");//添加单个项
            listBoxNames.Items.AddRange(new string[] { "ko", "ko1", "ko2" });
            listBoxNames.Items.Add(444);
            listBoxNames.Items.Add(9.5);
            //insert
            listBoxNames.Items.Insert(2, "po");


        }

        private void butAdd_Click(object sender, EventArgs e)
        {
            listBoxNames.Items.Clear();
            List<string> names = new List<string>();
            names.Add("okl");
            names.Add("okl1");
            names.Add("okl2");
            names.Add("okl3");
            names.Add("okl4");
            //  
            for (int i = 0; i < names.Count; i++)
            {
                listBoxNames.Items.Add(names[i]);
            }
            //  listBoxNames.SelectedIndex = 0;//第一项选择
            listBoxNames.SelectedItem = "okl4";
        }
        //动态添加的项 可以移除 数据绑定的 items.remove .add无效
        private void button1_Click(object sender, EventArgs e)
        {
            listBoxNames.Items.Remove("okl4");
            listBoxNames.Items.Remove(1);//指定位置

        }

        List<VideoStore> vslist = new List<VideoStore>();
        private void butStress_Click(object sender, EventArgs e)
        {
            List<VideoStore> videolist = new List<VideoStore>();
            videolist.Add(new VideoStore() { Id = 142, Name = "royd-142" });
            videolist.Add(new VideoStore() { Id = 143, Name = "royd-143" });
            videolist.Add(new VideoStore() { Id = 144, Name = "royd-144" });
            videolist.Add(new VideoStore() { Id = 145, Name = "royd-145" });
            //绑定
            listBoxVideos.DataSource = videolist;
            listBoxVideos.DisplayMember = "Name";
            listBoxVideos.ValueMember = "Id";
            listBoxVideos.SelectedIndex = 1;
            //绑定后无法修改
            //listBoxVideos.Items.Add("kk");
            //listBoxVideos.Items.Remove(142);
        }

        private void butSub_Click(object sender, EventArgs e)
        {//单选
            {//项类型:字符串
                //if (listBoxNames.SelectedItem != null)
                //{
                //    var stringItem = listBoxNames.SelectedItem.ToString();
                //}
                //string sname = listBoxNames.Text;
                ////  var val = listBoxNames.SelectedValue;
                ////项类型:对象类型
                ////1
                //var selectItem = listBoxVideos.SelectedItem as VideoStore;
                //string name = selectItem.Name;
                //int id = selectItem.Id;
                ////2.编号
                //int vid = Convert.ToInt32(listBoxNames.SelectedValue);
                //// int vid =listBoxVideos.SelectedIndex;
            }
            //多选
            {
                //选择项的名称
                //List<string>vsNames=new List<string>();
                //foreach(var item in listBoxVideos.SelectedItems)
                //{
                //    VideoStore vdInfo=item as VideoStore;
                //    vsNames.Add(vdInfo.Name);
                //}
                //listBoxSelVideos.Items.AddRange(vsNames.ToArray());
                //编号
                List<int> vsIds = new List<int>();
                foreach (int index in listBoxVideos.SelectedIndices)
                {
                    // int id = (listBoxVideos.Items[index]as VideoStore).Id;
                    int id = vslist[index].Id;//编号
                    vsIds.Add(id);
                }
                //值类型 对象类型
                listBoxSelVideos.Items.AddRange(vsIds.Select(id => id.ToString()).ToArray());
            }

        }

        //private void listBoxNames_SelectedIndexChanged_1(object sender, EventArgs e)
        //{

        //}

        private void listBoxVideosChanged(object sender, EventArgs e)
        {
            var selectItem = listBoxVideos.SelectedItem as VideoStore;
            textBoxStoreInfo.Text = $"the store of your choice:num" +
                $"{selectItem.Id} Name {selectItem.Name}";
        }

        private void butRight_Click(object sender, EventArgs e)
        {
            if (listBoxNames.SelectedItem != null)
            {
                var list = new List<object>();
                foreach(var item in listBoxNames.SelectedItems)
                {
                    list.Add(item);
                }
               foreach(var item in list)
                {
                    listBoxNames.Items.Remove(item);
                    listBoxSelNames.Items.Add(item);
                }
            }

        }

        private void butRightAll_Click(object sender, EventArgs e)
        {
            listBoxSelNames.Items.AddRange(listBoxNames.Items);
            listBoxNames.Items.Clear();
        }
        

        private void butLeft_Click(object sender, EventArgs e)
        {
            if (listBoxSelNames.SelectedItem != null)
            {
                var list = new List<object>();
                foreach (var item in listBoxSelNames.SelectedItems)
                {
                    list.Add(item);
                }
                foreach (var item in list)
                {

                    listBoxSelNames.Items.Remove(item);
                    listBoxNames.Items.Add(item);
                }
            }
        }

        private void butLeftAll_Click(object sender, EventArgs e)
        {
            listBoxNames.Items.AddRange(listBoxSelNames.Items);
            listBoxSelNames.Items.Clear();
        }
    }
}

combabox






csharp 复制代码
            //动态添加
            comboBoxStates.Items.Add("has been checked");
            comboBoxStates.Items.AddRange(new string[] {"has been stopped" });
csharp 复制代码
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 WinFormsApp1.Models;

namespace WinFormsApp1.ControlForms
{
    public partial class FormComboBox : Form
    {
        public FormComboBox()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        List<ClassInfo>classList=new List<ClassInfo>();
        private void FormComboBox_Load(object sender, EventArgs e)
        {   //初始化
            List<Grade> gradeList = new List<Grade>();
            gradeList.Add(new Grade() { GradeId = 2020, GradeName = "2020" });
            gradeList.Add(new Grade() { GradeId = 2021, GradeName = "2021" });
            gradeList.Add(new Grade() { GradeId = 2022, GradeName = "2022" });
            gradeList.Add(new Grade() { GradeId = 2023, GradeName = "2023" });
            gradeList.Add(new Grade() { GradeId = 2024, GradeName = "2024" });
            gradeList.Add(new Grade() { GradeId = 2025, GradeName = "2025" });
            comboBoxGrade.DisplayMember = "GradeName";
            comboBoxGrade.ValueMember = "GradeId";
            comboBoxGrade.DataSource = gradeList;
            comboBoxGrade.SelectedIndex = 0;//选择项

            //2020
            classList.Add(new ClassInfo() { ClassId = 202001,ClassName="2020-1",GradeId=2020 });
            classList.Add(new ClassInfo() { ClassId = 202002, ClassName = "2020-2", GradeId = 2020 });
            classList.Add(new ClassInfo() { ClassId = 202003, ClassName = "2020-3", GradeId = 2020 });
            //classList.Add(new ClassInfo() { ClassId = 1, ClassName = "2020-1", GradeId = 2020 });

            //2021
            classList.Add(new ClassInfo() { ClassId = 202101, ClassName = "2021-1", GradeId = 2021 });
            classList.Add(new ClassInfo() { ClassId = 202102, ClassName = "2021-2", GradeId = 2021 });
            classList.Add(new ClassInfo() { ClassId = 202103, ClassName = "2021-3", GradeId = 2021 });
            //2022
            classList.Add(new ClassInfo() { ClassId = 202201, ClassName = "2022-1", GradeId = 2022 });
            classList.Add(new ClassInfo() { ClassId = 202202, ClassName = "2022-2", GradeId = 2022 });
            classList.Add(new ClassInfo() { ClassId = 202203, ClassName = "2022-3", GradeId = 2022 });
            //2023
            classList.Add(new ClassInfo() { ClassId = 202301, ClassName = "2023-1", GradeId = 2023 });
            classList.Add(new ClassInfo() { ClassId = 202302, ClassName = "2023-2", GradeId = 2023 });
            classList.Add(new ClassInfo() { ClassId = 202303, ClassName = "2023-3", GradeId = 2023 });
            classList.Add(new ClassInfo() { ClassId = 202304, ClassName = "2023-4", GradeId = 2023 });
            classList.Add(new ClassInfo() { ClassId = 202305, ClassName = "2023-5", GradeId = 2023 });
            //2024
            classList.Add(new ClassInfo() { ClassId = 202401, ClassName = "2024-1", GradeId = 2024 });
            classList.Add(new ClassInfo() { ClassId = 202402, ClassName = "2024-2", GradeId = 2024 });
            classList.Add(new ClassInfo() { ClassId = 202403, ClassName = "2024-3", GradeId = 2024 });
            //2025
            classList.Add(new ClassInfo() { ClassId = 202501, ClassName = "2025-1", GradeId = 2025 });
            classList.Add(new ClassInfo() { ClassId = 202502, ClassName = "2025-2", GradeId = 2025 });
            classList.Add(new ClassInfo() { ClassId = 202503, ClassName = "2025-3", GradeId = 2025 });
            classList.Add(new ClassInfo() { ClassId = 202504, ClassName = "2025-4", GradeId = 2025 });
            classList.Add(new ClassInfo() { ClassId = 202505, ClassName = "2025-5", GradeId = 2025 });
            
            
            //动态添加
            //comboBoxStates.Items.Add("has been checked");
            //comboBoxStates.Items.AddRange(new string[] {"has been stopped" });
            comboBoxStates.Items.Clear();
            List<string> states = new List<string>();
            states.Add("not been checked");
            states.AddRange(new string[] { "has been checked", "has been droped", "has been shut" });
            foreach (string state in states)
            {
                comboBoxStates.Items.Add(state);
            }
        }

        private void butSubmit_Click(object sender, EventArgs e)
        { //name
            string gradeName = comboBoxGrade.Text;
            if (comboBoxGrade.Items != null)
            {
                string gradeName1 = (comboBoxGrade.SelectedItem as Grade).GradeName;
            }
            //id

            if (comboBoxGrade.SelectedValue != null)
            {
                int gradeId = Convert.ToInt32(comboBoxGrade.SelectedValue);
            }

        }

        //
        private void comboBoxGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            int gradeId=Convert.ToInt32(comboBoxGrade.SelectedValue);
            comboBoxClass.DataSource = null;
            //class list
            List<ClassInfo> curClassList=classList.Where(c=>c.GradeId==gradeId).ToList();
            if (curClassList.Count > 0) {
                comboBoxClass.DisplayMember = "ClassName";
                comboBoxClass.ValueMember = "ClassId";
                comboBoxClass.DataSource = curClassList;
                comboBoxClass.SelectedIndex = 0;
            }//如何理解
            else
            {
                MessageBox.Show("this grade has no class");
                return;
            }
            
        }
    }
}

级联选择

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormsApp1.Models
{
    public class ClassInfo
    {
        public int ClassId { get; set; }
        public string ClassName { get; set; }
        public int GradeId { get; set; }
        public string GradeName { get; set; }
    }
}
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormsApp1.Models
{
    public class Grade
    {
        public int GradeId { get; set; }
        public string GradeName { get; set; }
    }
}
csharp 复制代码
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 WinFormsApp1.Models;

namespace WinFormsApp1.ControlForms
{
    public partial class FormComboBox : Form
    {
        public FormComboBox()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        List<ClassInfo>classList=new List<ClassInfo>();
        private void FormComboBox_Load(object sender, EventArgs e)
        {   //初始化
            List<Grade> gradeList = new List<Grade>();
            gradeList.Add(new Grade() { GradeId = 2020, GradeName = "2020" });
            gradeList.Add(new Grade() { GradeId = 2021, GradeName = "2021" });
            gradeList.Add(new Grade() { GradeId = 2022, GradeName = "2022" });
            gradeList.Add(new Grade() { GradeId = 2023, GradeName = "2023" });
            gradeList.Add(new Grade() { GradeId = 2024, GradeName = "2024" });
            gradeList.Add(new Grade() { GradeId = 2025, GradeName = "2025" });
            gradeList.Add(new Grade() { GradeId = 2025, GradeName = "2026" });
            comboBoxGrade.Items.Clear();
            comboBoxGrade.DisplayMember = "GradeName";//显示信息属性名
            comboBoxGrade.ValueMember = "GradeId";//值属性名 有啥区别
            comboBoxGrade.DataSource = gradeList;
            comboBoxGrade.SelectedIndex = 0;//选择项

            //2020
            classList.Add(new ClassInfo() { ClassId = 202001,ClassName="2020-1",GradeId=2020 });
            classList.Add(new ClassInfo() { ClassId = 202002, ClassName = "2020-2", GradeId = 2020 });
            classList.Add(new ClassInfo() { ClassId = 202003, ClassName = "2020-3", GradeId = 2020 });
            //classList.Add(new ClassInfo() { ClassId = 1, ClassName = "2020-1", GradeId = 2020 });

            //2021
            classList.Add(new ClassInfo() { ClassId = 202101, ClassName = "2021-1", GradeId = 2021 });
            classList.Add(new ClassInfo() { ClassId = 202102, ClassName = "2021-2", GradeId = 2021 });
            classList.Add(new ClassInfo() { ClassId = 202103, ClassName = "2021-3", GradeId = 2021 });
            //2022
            classList.Add(new ClassInfo() { ClassId = 202201, ClassName = "2022-1", GradeId = 2022 });
            classList.Add(new ClassInfo() { ClassId = 202202, ClassName = "2022-2", GradeId = 2022 });
            classList.Add(new ClassInfo() { ClassId = 202203, ClassName = "2022-3", GradeId = 2022 });
            //2023
            classList.Add(new ClassInfo() { ClassId = 202301, ClassName = "2023-1", GradeId = 2023 });
            classList.Add(new ClassInfo() { ClassId = 202302, ClassName = "2023-2", GradeId = 2023 });
            classList.Add(new ClassInfo() { ClassId = 202303, ClassName = "2023-3", GradeId = 2023 });
            classList.Add(new ClassInfo() { ClassId = 202304, ClassName = "2023-4", GradeId = 2023 });
            classList.Add(new ClassInfo() { ClassId = 202305, ClassName = "2023-5", GradeId = 2023 });
            //2024
            classList.Add(new ClassInfo() { ClassId = 202401, ClassName = "2024-1", GradeId = 2024 });
            classList.Add(new ClassInfo() { ClassId = 202402, ClassName = "2024-2", GradeId = 2024 });
            classList.Add(new ClassInfo() { ClassId = 202403, ClassName = "2024-3", GradeId = 2024 });
            //2025
            classList.Add(new ClassInfo() { ClassId = 202501, ClassName = "2025-1", GradeId = 2025 });
            classList.Add(new ClassInfo() { ClassId = 202502, ClassName = "2025-2", GradeId = 2025 });
            classList.Add(new ClassInfo() { ClassId = 202503, ClassName = "2025-3", GradeId = 2025 });
            classList.Add(new ClassInfo() { ClassId = 202504, ClassName = "2025-4", GradeId = 2025 });
            classList.Add(new ClassInfo() { ClassId = 202505, ClassName = "2025-5", GradeId = 2025 });
            
            
            //动态添加
            //comboBoxStates.Items.Add("has been checked");
            //comboBoxStates.Items.AddRange(new string[] {"has been stopped" });
            comboBoxStates.Items.Clear();
            List<string> states = new List<string>();
            states.Add("not been checked");
            states.AddRange(new string[] { "has been checked", "has been droped", "has been shut" });
            foreach (string state in states)
            {
                comboBoxStates.Items.Add(state);
            }
        }

        private void butSubmit_Click(object sender, EventArgs e)
        { //name
            string gradeName = comboBoxGrade.Text;
            if (comboBoxGrade.Items != null)
            {
                string gradeName1 = (comboBoxGrade.SelectedItem as Grade).GradeName;
            }
            //id

            if (comboBoxGrade.SelectedValue != null)
            {
                int gradeId = Convert.ToInt32(comboBoxGrade.SelectedValue);
            }

        }

        //
        private void comboBoxGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            int gradeId=Convert.ToInt32(comboBoxGrade.SelectedValue);
            comboBoxClass.DataSource = null;
            //class list
            List<ClassInfo> curClassList=classList.Where(c=>c.GradeId==gradeId).ToList();
            if (curClassList.Count > 0) {
                curClassList.Insert(0, new ClassInfo() { ClassId = 0, ClassName = "Pl choose" });
                comboBoxClass.DisplayMember = "ClassName";
                comboBoxClass.ValueMember = "ClassId";
                comboBoxClass.DataSource = curClassList;
                comboBoxClass.SelectedIndex = 0;
            }//如何理解
            else
            {
                MessageBox.Show("this grade has no class");
                return;
            }
            
        }
    }
}





formCheckedListBox


csharp 复制代码
 private void FormCheckedListBox_Load(object sender, EventArgs e)
 {
     //初始化项
     List<string> roles=new List<string>();
     roles.AddRange(new string[] {"manager","worker","farmer","faker","speaker" });
     foreach (string role in roles) { 
     checkedListRole.Items.Add(role);
     }
 }
csharp 复制代码
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 WinFormsApp1.ControlForms
{
    public partial class FormCheckedListBox : Form
    {
        public FormCheckedListBox()
        {
            InitializeComponent();
        }

        private void FormCheckedListBox_Load(object sender, EventArgs e)
        {
            //初始化项
            List<string> roles=new List<string>();
            roles.AddRange(new string[] {"manager","worker","farmer","faker","speaker" });
            //动态添加
            //foreach (string role in roles) { 
            //checkedListRole.Items.Add(role);
            //绑定
            checkedListRole.DataSource=roles;

        }


    }
    }
csharp 复制代码
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 WinFormsApp1.ControlForms
{
    public partial class FormCheckedListBox : Form
    {
        public FormCheckedListBox()
        {
            InitializeComponent();
        }

        private void FormCheckedListBox_Load(object sender, EventArgs e)
        {
            //初始化项
            List<string> roles = new List<string>();
            roles.AddRange(new string[] { "manager", "worker", "farmer", "faker", "speaker" });
            //动态添加
            //foreach (string role in roles) { 
            //checkedListRole.Items.Add(role);
            //绑定
            checkedListRole.DataSource = roles;

        }

        private void butAddRoles_Click(object sender, EventArgs e)
        {
        //绑定后无法直接添加
        //list是什么类?
        List<string> roles=checkedListRole.DataSource as List<string>;
            checkedListRole.DataSource = null;
            roles.Add("department manager");
            checkedListRole.DataSource = roles;

        }
    }
}


List

List是 C# 中最重要、最常用的集合类之一,属于 泛型集合。

csharp 复制代码
// 空列表
List<string> list1 = new List<string>();

// 指定初始容量
List<string> list2 = new List<string>(100);

// 从其他集合初始化
string[] array = { "A", "B", "C" };
List<string> list3 = new List<string>(array);
csharp 复制代码
List<string> fruits = new List<string> { "Apple", "Banana", "Orange" };

int count = fruits.Count;           // 元素数量:3
int capacity = fruits.Capacity;     // 当前容量
csharp 复制代码
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 WinFormsApp1.Models;

namespace WinFormsApp1.ControlForms
{
    public partial class FormCheckedListBox : Form
    {
        public FormCheckedListBox()
        {
            InitializeComponent();
        }

        private void FormCheckedListBox_Load(object sender, EventArgs e)
        {
            //初始化项
            List<string> roles = new List<string>();
            roles.AddRange(new string[] { "manager", "worker", "farmer", "faker", "speaker" });
            //动态添加
            //foreach (string role in roles) { 
            //checkedListRole.Items.Add(role);
            //绑定
            checkedListRole.DataSource = roles;

            //绑定数据源
            //DataSource DisplayMember

            List<ClassInfo> classList = new List<ClassInfo>();
            //2020
            classList.Add(new ClassInfo() { ClassId = 202001, ClassName = "2020-1"});
            classList.Add(new ClassInfo() { ClassId = 202002, ClassName = "2020-2" });
            classList.Add(new ClassInfo() { ClassId = 202003, ClassName = "2020-3" });
            //classList.Add(new ClassInfo() { ClassId = 1, ClassName = "2020-1", GradeId = 2020 });
            classList.Add(new ClassInfo() { ClassId = 202101, ClassName = "2021-1" });
            classList.Add(new ClassInfo() { ClassId = 202102, ClassName = "2021-2" });
            classList.Add(new ClassInfo() { ClassId = 202103, ClassName = "2021-3" });
            classList.Add(new ClassInfo() { ClassId = 202201, ClassName = "2022-1" });
            classList.Add(new ClassInfo() { ClassId = 202202, ClassName = "2022-2" });
            classList.Add(new ClassInfo() { ClassId = 202203, ClassName = "2022-3" });
            classList.Add(new ClassInfo() { ClassId = 202301, ClassName = "2023-1"});
            classList.Add(new ClassInfo() { ClassId = 202302, ClassName = "2023-2" });
            classList.Add(new ClassInfo() { ClassId = 202303, ClassName = "2023-3" });
            classList.Add(new ClassInfo() { ClassId = 202304, ClassName = "2023-4" });
            classList.Add(new ClassInfo() { ClassId = 202305, ClassName = "2023-5" });
            checkedListClassIn.DataSource = classList;
            checkedListClassIn.DisplayMember = "ClassName";
            checkedListClassIn.ValueMember = "ClassId";

        }

        private void butAddRoles_Click(object sender, EventArgs e)
        {
            //绑定后无法直接添加
            //list是什么类?
            List<string> roles = checkedListRole.DataSource as List<string>;
            checkedListRole.DataSource = null;
            roles.Add("department manager");
            checkedListRole.DataSource = roles;

        }

        private void checkedListRole_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}






csharp 复制代码
       private void butOK_Click(object sender, EventArgs e)
       {
           if (checkedListClassIn.SelectedItem != null)
           {//选择项  selectedItem 不管是否选中
               ClassInfo selClass = checkedListClassIn.SelectedItem as ClassInfo;
               if (selClass != null)
               {
                   labelSelClass.Text = "selected class is:" + selClass.ClassName;
               }

               //值--编号
               if(checkedListClassIn.SelectedValue!= null)
               {
                   int selClassId = Convert.ToInt32(checkedListClassIn.SelectedValue);
               }

           }


csharp 复制代码
   //值-多项
   List<int>selClassIds=new List<int>();
   foreach (var item in checkedListClassIn.CheckedItems)
   {
   ClassInfo classInfo=item as ClassInfo;
       selClassIds.Add(classInfo.ClassId);




csharp 复制代码
        private void checkedListClassIn_ItemCheck(object sender, ItemCheckEventArgs e)
        {//获取当前项文本
           // string curTrxt = (checkedListClassIn.Items[e.Index] as ClassInfo).ClassName;
           string curText = checkedListClassIn.GetItemText(checkedListClassIn.Items[e.Index]);
            //
            if (e.NewValue == CheckState.Checked)
            {
                listBox1.Items.Add(curText);

            }
            else
            {
                listBox1.Items.Remove(curText);
            }
        }
csharp 复制代码
     private void butMethod_Click(object sender, EventArgs e)
     {//第4项选中
         checkedListClassIn.SetItemChecked(3,true);
         //第

     }




csharp 复制代码
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 WinFormsApp1.Models;

namespace WinFormsApp1.ControlForms
{
    public partial class FormCheckedListBox : Form
    {
        public FormCheckedListBox()
        {
            InitializeComponent();
        }

        private void FormCheckedListBox_Load(object sender, EventArgs e)
        {
            //初始化项
            List<string> roles = new List<string>();
            roles.AddRange(new string[] { "manager", "worker", "farmer", "faker", "speaker" });
            //动态添加
            //foreach (string role in roles) { 
            //checkedListRole.Items.Add(role);
            //绑定
            checkedListRole.DataSource = roles;
            checkedListRole.SelectedIndex = 1;

            //绑定数据源
            //DataSource DisplayMember

            List<ClassInfo> classList = new List<ClassInfo>();
            //2020
            classList.Add(new ClassInfo() { ClassId = 202001, ClassName = "2020-1" });
            classList.Add(new ClassInfo() { ClassId = 202002, ClassName = "2020-2" });
            classList.Add(new ClassInfo() { ClassId = 202003, ClassName = "2020-3" });
            //classList.Add(new ClassInfo() { ClassId = 1, ClassName = "2020-1", GradeId = 2020 });
            classList.Add(new ClassInfo() { ClassId = 202101, ClassName = "2021-1" });
            classList.Add(new ClassInfo() { ClassId = 202102, ClassName = "2021-2" });
            classList.Add(new ClassInfo() { ClassId = 202103, ClassName = "2021-3" });
            classList.Add(new ClassInfo() { ClassId = 202201, ClassName = "2022-1" });
            classList.Add(new ClassInfo() { ClassId = 202202, ClassName = "2022-2" });
            classList.Add(new ClassInfo() { ClassId = 202203, ClassName = "2022-3" });
            classList.Add(new ClassInfo() { ClassId = 202301, ClassName = "2023-1" });
            classList.Add(new ClassInfo() { ClassId = 202302, ClassName = "2023-2" });
            classList.Add(new ClassInfo() { ClassId = 202303, ClassName = "2023-3" });
            classList.Add(new ClassInfo() { ClassId = 202304, ClassName = "2023-4" });
            classList.Add(new ClassInfo() { ClassId = 202305, ClassName = "2023-5" });
            checkedListClassIn.DataSource = classList;
            checkedListClassIn.DisplayMember = "ClassName";
            checkedListClassIn.ValueMember = "ClassId";
            // checkedListRole.SelectedIndex = 1;//没用吗
            //checkedListClassIn.SelectedItem = classList[2];
            checkedListClassIn.SelectedValue = 202305;//选择项的值
            checkedListClassIn.SetItemChecked(4, true);
        }

        private void butAddRoles_Click(object sender, EventArgs e)
        {
            //绑定后无法直接添加
            //list是什么类?
            List<string> roles = checkedListRole.DataSource as List<string>;
            checkedListRole.DataSource = null;
            roles.Add("department manager");
            checkedListRole.DataSource = roles;

        }

        private void checkedListRole_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void butOK_Click(object sender, EventArgs e)
        {
            if (checkedListClassIn.SelectedItem != null)
            {//选择项  selectedItem 不管是否选中
                ClassInfo selClass = checkedListClassIn.SelectedItem as ClassInfo;
                if (selClass != null)
                {
                    labelSelClass.Text = "selected class is:" + selClass.ClassName;
                }

                //值--编号
                if (checkedListClassIn.SelectedValue != null)
                {
                    int selClassId = Convert.ToInt32(checkedListClassIn.SelectedValue);
                }
                //值-多项
                List<int> selClassIds = new List<int>();
                //foreach (var item in checkedListClassIn.CheckedItems)
                //{
                //ClassInfo classInfo=item as ClassInfo;
                //    selClassIds.Add(classInfo.ClassId);
                //}

                foreach (int index in checkedListClassIn.CheckedIndices)
                {
                    ClassInfo classInfo = checkedListClassIn.Items[index] as ClassInfo;
                    selClassIds.Add(classInfo.ClassId);

                }
                List<ClassInfo> classes = checkedListClassIn.DataSource as List<ClassInfo>;
                foreach (int index in checkedListClassIn.CheckedIndices)
                {
                    ClassInfo classInfo = classes[index] as ClassInfo;
                    selClassIds.Add(classInfo.ClassId);

                }

            }
        }

        private void checkedListClassIn_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        //选中发生
        private void checkedListClassIn_ItemCheck(object sender, ItemCheckEventArgs e)
        {//获取当前项文本
         // string curTrxt = (checkedListClassIn.Items[e.Index] as ClassInfo).ClassName;
            string curText = checkedListClassIn.GetItemText(checkedListClassIn.Items[e.Index]);
            //
            if (e.NewValue == CheckState.Checked)
            {
                listBox1.Items.Add(curText);

            }
            else
            {
                listBox1.Items.Remove(curText);
            }
        }

        private void butMethod_Click(object sender, EventArgs e)
        {//第4项选中 bool
            checkedListClassIn.SetItemChecked(3,true);
            //第
            checkedListClassIn.SetItemChecked(4,false);
            checkedListClassIn.SetItemCheckState(2,CheckState.Checked);//check
            bool blval = checkedListClassIn.GetItemChecked(5);//bool
            //选中状态
            CheckState checkState = checkedListClassIn.GetItemCheckState(1);
        }
    }
}

datapacker




相关推荐
('-')1 小时前
《从根上理解MySQL是怎样运行的》第十二章学习笔记
笔记·学习·mysql
回忆彡美好3 小时前
OpenGL的3D编程个人笔记之材质贴图
笔记·3d·材质·opengl
一个平凡而乐于分享的小比特3 小时前
UCOSIII内核 VS FreeRTOS内核
笔记·freertos·ucosiii
星轨初途3 小时前
C++入门(算法竞赛类)
c++·经验分享·笔记·算法
prog_61034 小时前
【笔记】和各大AI语言模型写项目——手搓SDN后得到的经验
人工智能·笔记·语言模型
星轨初途5 小时前
C++的输入输出(上)(算法竞赛类)
开发语言·c++·经验分享·笔记·算法
再睡一夏就好6 小时前
string.h头文件中strcpy、memset等常见函数的使用介绍与模拟实现
c语言·c++·笔记·string·内存函数·strcpy
('-')6 小时前
《从根上理解MySQL是怎样运行的》第十三章笔记
数据库·笔记·mysql
LO嘉嘉VE6 小时前
学习笔记二十一:深度学习
笔记·深度学习·学习