【C#】窗体透明度、ListBox的增删改查

这两个都是C#窗体的基础内容,下面用一个简单的例子说明这个问题,注意ListBox不是ListView就好。

如下图:


有两个按钮可以调节窗体的透明度,而在上方,如果ListBox的项被选中,则会呈现在文本框之中,可供修改,没有选中ListBox的项,或者选中之后,点击ListBox的空白处,则是为ListBox添加项的状态,同时可以删除相应的项。并且无论是在添加还是在删除状态,都是要求ListBox中项不能相等。

ListBox的具体布局如下图:


请自行利用对其工具排放好。同时为ListBox1添加一个listBox1_MouseClick事件,用于点击ListBox1的空白之处清空选择。因为这个组件如果你选择一项话,你不自己写一个事件,它会永远选中这一项,除非你选择别的项。换句话说,自从你选中其中一项之后,它会永远选中一项。

Form1的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DataGridView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;//设置Form窗体初始化放在屏幕中间    
        }

        //选中一项话,自动改变按钮1的文本,同时将选中的项放到textBox1
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count > 0)
            {
                textBox1.Text = listBox1.SelectedItems[0] + "";
                button1.Text = "修改";
            }
        }

        //点击ListBox1的空白之处清空选择
        private void listBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (listBox1.IndexFromPoint(e.X, e.Y) == -1)
            {
                listBox1.ClearSelected();
                button1.Text = "添加";
            }
        }

        //添加与修改按钮
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                if (textBox1.Text.Length < 9)//只接受文本长度在0-9之间的输入
                {
                    Boolean isExisted = false;//遍历ListBox,查找是否有与当前输入相同的项
                    for (int i = 0; i < listBox1.Items.Count; i++)
                    {
                        if ((textBox1.Text.ToString()).Equals(listBox1.Items[i].ToString()))
                        {
                            isExisted = true;
                            break;
                        }
                    }
                    if (!isExisted)//如果没有
                    {
                        if (button1.Text == "添加")
                        {
                            listBox1.Items.Add(textBox1.Text);//为ListBox添加一项
                            textBox1.Text = "";
                        }
                        else
                        {
                            listBox1.Items[listBox1.SelectedIndex] = textBox1.Text;//修改ListBox选中的项
                            textBox1.Text = "";
                            listBox1.ClearSelected();
                            button1.Text = "添加";
                        }//注意添加与修改之后,皆要初始化
                    }
                    else
                    {
                        MessageBox.Show("已存在相应的项,无法添加!", this.Text);
                    }

                }
                else
                {
                    MessageBox.Show("不得大于等于9个字!", this.Text);
                }
            }
            else
            {
                MessageBox.Show("不能为空!", this.Text);
            }
        }

        //删除ListBox的选中项
        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count > 0)//删除之前记得判断是否有选中项
            {
                listBox1.Items.Remove(listBox1.SelectedItems[0]);
                listBox1.ClearSelected();
                textBox1.Text = "";
                button1.Text = "添加";
            }
        }

        //窗体透明度的调整
        private void button3_Click(object sender, EventArgs e)
        {
            if (this.Opacity > 0.2)//防止用户调成0,再也看不到窗体了
            {
                this.Opacity -= 0.1;
                label1.Text = "透明度:" + this.Opacity * 100 + "%";//将透明度换成百分比
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.Opacity < 1)
            {
                this.Opacity += 0.1;
                label1.Text = "透明度:" + this.Opacity * 100 + "%";
            }
        }
    }
}


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值