欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C# Winfom 中ListBox的简单用法详解

时间:2021-05-11 08:51:36|栏目:.NET代码|点击:

1、如何添加listBox的值

this.listBox1.Items.Add("张晓东");

2、如何判断listBox集合是否添加过

//检查添加值是否添加过
if(this.listBox1.items.Contains("张晓东")){
  MessageBox.show("集合成员已添加过!");  
}
else{
   //执行添加集合成员
}

3、如何获取listBox选中的值

//判断所有选中项集合大于0
if(this.listBox1.SelectedItems.Count > 0){
   //获取选中的值
   this.listBox1.SelectedItem.ToString(); 
}
else{
  MessageBox.Show("未选中listbox集合的值"); 
}

4、如何移除listBox中存在的值

//移除listBox集合的项
this.listBox1.Items.Remove("张晓东");

5、综合使用例子

简单实现人员从部门1转移到部门2或部门2转移到部门1

1)界面设计

2)完整源码

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

    /// <summary>
    /// 添加人员到采购部门
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnInsert_Click(object sender, EventArgs e)
    {
      //获取添加人的值
      string peopleText = this.txtPeople.Text.Trim().ToString();
      //获取listbox1的对象
      ListBox list1 = this.listBox1;
      //判断人员是否已经添加过
      if (!list1.Items.Contains(peopleText))
      {
        list1.Items.Add(peopleText);
      }
      else {
        MessageBox.Show("该人员已经添加过,无法重复添加!");
      }
    }

    /// <summary>
    /// 将采购人员转移到销售部门
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnRightMove_Click(object sender, EventArgs e)
    {
      //获取listbox1的所有选中的项
      if (this.listBox1.SelectedItems.Count > 0)
      {
        string checkPeople = this.listBox1.SelectedItem.ToString();
        //判断是否添加到listbox2
        if (!this.listBox2.Items.Contains(checkPeople)) {
          //添加人员到listbox2中
          this.listBox2.Items.Add(checkPeople);
          //移除listbox1中
          this.listBox1.Items.Remove(checkPeople);
        }
        else
        {
          MessageBox.Show("该人员已经转移过,无法重复转移!");
        }

      }
      else {
        MessageBox.Show("未选中采购人员,无法转移销售部门!");
      }
    }

    /// <summary>
    /// 将销售人员转移到采购部门
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnLeftMove_Click(object sender, EventArgs e)
    {
      //获取listbox2的所有选中的项
      if (this.listBox2.SelectedItems.Count > 0)
      {
        string checkPeople = this.listBox2.SelectedItem.ToString();
        //判断是否添加到listbox1
        if (!this.listBox1.Items.Contains(checkPeople))
        {
          //添加人员到listbox1中
          this.listBox1.Items.Add(checkPeople);
          //移除listbox1中
          this.listBox2.Items.Remove(checkPeople);
        }
        else
        {
          MessageBox.Show("该人员已经转移过,无法重复转移!");
        }

      }
      else
      {
        MessageBox.Show("未选中销售人员,无法转移到采购部门!");
      }
    }
  }
}

3)界面演示

3.1)添加人员到部门1演示效果

3.2)部门1转移到部门2演示效果

3.3)部门2转移到部门1演示效果

上一篇:C#定义简单的反射工厂实例分析

栏    目:.NET代码

下一篇:.net 读取项目AssemblyInfo.cs属性值

本文标题:C# Winfom 中ListBox的简单用法详解

本文地址:http://www.codeinn.net/misctech/118869.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有