C#ListBox控件

C#ListBox控件_第1张图片

GroupBox   RadioBox---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 WindowsFormsApplication2_Csharp
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }



        private void textBox1_TextChanged(object sender, EventArgs e)
        {
             if (radioButton1.Checked == true)
            {
                textBox1 .Text= "Windows";
            }
            if (radioButton2.Checked == true)
            {
                textBox1.Text = radioButton2.Text;
            }
            if (radioButton3.Checked == true)
            {
                textBox1.Text = radioButton3.Text;
            }
            if (radioButton4.Checked == true)
            {
                textBox1.Text = "MACOS";
          
            }
            
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            textBox1_TextChanged(sender, e);

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            textBox1_TextChanged(sender, e);

        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                textBox1.Text = radioButton1.Text;
            }
            if (radioButton2.Checked == true)
            {
                textBox1.Text = radioButton2.Text;
            }
            if (radioButton3.Checked == true)
            {
                textBox1.Text = radioButton3.Text;
            }
            if (radioButton4.Checked == true)
            {
                textBox1.Text = "MACOS";

            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            textBox1_TextChanged(sender, e);

        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            textBox1_TextChanged(sender, e);

        }


    }
}

C#ListBox控件_第2张图片

 

panel---面板---强调显示---2

C#ListBox控件_第3张图片

CheckBox---复选框---多选---3---和GroupBox当中的RadioBox一样,也是通过Checked属性检查是否选中。

C#ListBox控件_第4张图片

ListBox---Item属性

按下去得到 字符串集合编辑器---每换行得一个选项

C#ListBox控件_第5张图片

C#ListBox控件_第6张图片

-如果想要自定义添加到列表当中选项,可以点击添加,添加进去选项

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != String.Empty)
            {
                listBox1.Items.Add(textBox2.Text);
            }
        }

C#ListBox控件_第7张图片

 

ListBox存在一个SelectedIndexChanged()选择改变事件

时刻进行检测 异常,Listbox.SelectedIndex是否合理

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 WindowsFormsApplication2_Csharp
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }



        private void textBox1_TextChanged(object sender, EventArgs e)
        {
             if (radioButton1.Checked == true)
            {
                textBox1 .Text= "Windows";
            }
            if (radioButton2.Checked == true)
            {
                textBox1.Text = radioButton2.Text;
            }
            if (radioButton3.Checked == true)
            {
                textBox1.Text = radioButton3.Text;
            }
            if (radioButton4.Checked == true)
            {
                textBox1.Text = "MACOS";
          
            }
            
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            textBox1_TextChanged(sender, e);

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            textBox1_TextChanged(sender, e);

        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                textBox1.Text = radioButton1.Text;
            }
            if (radioButton2.Checked == true)
            {
                textBox1.Text = radioButton2.Text;
            }
            if (radioButton3.Checked == true)
            {
                textBox1.Text = radioButton3.Text;
            }
            if (radioButton4.Checked == true)
            {
                textBox1.Text = "MACOS";

            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            textBox1_TextChanged(sender, e);

        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            textBox1_TextChanged(sender, e);

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != String.Empty)
            {
                listBox1.Items.Add(textBox2.Text);
            }
        }

        private void button2_SizeChanged(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {//将Listbox选中的索引项内容输出到文本框内
            if (listBox1.SelectedIndex >= 0 && listBox1.SelectedIndex < listBox1.Items.Count)
                textBox2.Text =Convert.ToString( listBox1.Items[listBox1.SelectedIndex]);
        }

        private void button2_Click(object sender, EventArgs e)
        {//点击删除按钮,即可删除ListBox选中的索引项内容
            if(listBox1.SelectedIndex>=0&&listBox1.SelectedIndex

C#ListBox控件_第8张图片

C#ListBox控件_第9张图片

你可能感兴趣的:(C#ListBox控件)