c# listbox使用

1.listbox在显示时很重要,尤其涉及到日志输出,串口输出,读取数据库的内容,结合定时器的使用,就更加好了。

2.所以写了一小例子,仅供参考,本篇也参考了别人写的代码,http://www.cnblogs.com/xielong/p/6744805.html

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

namespace listbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string peopleText = textBox1.Text.Trim().ToString();
//获取listbox1的对象
ListBox list1 = this.listBox1;
//判断人员是否已经添加过
if (!list1.Items.Contains(peopleText))
{
//list1.Items.Add(peopleText);
list1.Items.Insert(0, peopleText);
}
else
{
MessageBox.Show("该人员已经添加过,无法重复添加!");
}
}

private void button2_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("未选中采购人员,无法转移销售部门!");
}
}

private void button3_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("未选中销售人员,无法转移到采购部门!");
}
}
}
}

时间: 2024-08-01 15:33:44

c# listbox使用的相关文章

WPF ItemsControl ListBox ListView比较

在进行列表信息展示时,WPF中提供多种列表可供选择.这篇博客将对WPF ItemsControl, ListBox, ListView进行比较. 相同点: 1. 这三个控件都是列表型控件,可以进行列表绑定(ItemsSource): 2. 这三个控件均使用ItemsPresenter来展示列表信息: 不同点: 控件层次关系: ItemsControl: System.Object  System.Windows.Threading.DispatcherObject System.Windows.

Python3 Tkinter基础 Scrollbar Listbox 在listbox中创建一个垂直滚动条 yscrollcommand yview

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: from tkinter import * root=Tk() scrolly=Scrollbar(root) scrolly.pack(side=RIGHT,fill=Y) mylb=Listbox(root, yscrollcommand=scrolly.s

Python3 Tkinter基础 Listbox for循环与insert 将一个列表中元素添加到Listbox中

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: from tkinter import * root=Tk() myLB=Listbox(root) myLB.pack() myList=["贪狼","巨门","禄存","文曲","

WPF ListBox 中使用网格布局

<Grid> <ListBox Height="181" HorizontalAlignment="Left" Margin="12,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="687" UseLayoutRounding="False" Panel.ZIndex="2&qu

WPF学习笔记——ListBox用ItemsSource绑定数据源

<Grid> <ListBox x:Name="Lst"> <ListBox.ItemTemplate> <DataTemplate> <Button MouseDoubleClick="Button_MouseDoubleClick"> <Grid> <Image Source="{Binding Path=BackGround}" /> <TextBlo

C# 向listbox添加大量数据项的实践心得

使用 ListBox.Items.Add 方法添加项时,可以使用 BeginUpdate 方法,以防止每次向列表添加项时控件都重新绘制 ListBox.完成向列表添加项的任务后,调用 EndUpdate 方法来使 ListBox 能够重新绘制.当向列表添加大量的项时,使用这种方法添加项可以防止绘制 ListBox 时闪烁. 参考内容

《ASP.NET》数据绑定—DropDownList、ListBox

DropDownList和ListBox实现两级联动功能,他们也可以将从后台数据库中搜选的出来的信息加以绑定,这里要实现的功能是在DropDownList中选择"省",然后让ListBox自动将其省份下的"市"显示出来,这就是所谓的两级联动功能,这个功能我们在很多注册网页上看见,今天咱们就用ASP.NET解开其神秘的面纱. 一.设置前台界面,在Web窗体中添加DropDownList和ListBox两个控件.界面图如下所示. 二.编写后台代码 在这,后台代码编写在其

Web页面中两个listbox的option的转移

Html: <div><span>所选时间:</span><select id="xuanyongTimelb" style="width: 200px; height: 130px;" multiple="multiple"></select></div> <div class="label"><span id="rightt

wpf CollectionViewSource与ListBox的折叠、分组显示,及输入关键字 Filter的筛选

在wpf中虽然ObservableCollection<T>作为ListBox的Itemsource,很好,很强大!但是CollectionViewSource与ListBox才是天作之合! wpf中ListBox支持分组显示,CollectionViewSource.GroupDescriptions为其实现了分组.废话不多说,下面上ListBox分组显示的Demo代码: XAML: <Window x:Class="WpfListGroup.MainWindow"

标准控件(五)——Label,ListBox

Label 父类:WebControl 在呈现的时候,两边会呈现为span标签. 属性: Width                   (Unit)可设为像素,也可设为百分比 Height                  (Unit)可设为像素,也可设为百分比 BackColor               Color ForeColor               Color BorderColor             Color BorderWidth             Uni