C#ListBox控件

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);

        }


    }
}

 

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

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

ListBox---Item属性

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

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

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

 

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<listBox1.Items.Count)
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            
        }


    }
}

  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值