功能

添加、删除、修改选中的项、上移、下移、清空、保存列表、加载列表、判断内容是否重复、查找、模糊查找、取消选择、上一条、下一条、第一条、最后一条

下载地址:https://download.csdn.net/download/u012663700/11994849

 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 ListBoxDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
SetupButtonEnabled();
} //设置按钮的可用和不可用
private void SetupButtonEnabled()
{
string s = textBox1.Text.Trim();
int i = listBox1.SelectedIndex;
int count = listBox1.Items.Count; //添加
btnAdd.Enabled = !listBox1.Items.Contains(s) && s.Length > && !s.Contains(","); //删除
btnDel.Enabled = i != -; //重命名
btnRename.Enabled = i != - && listBox1.Items[i].ToString() != s; //上移
btnMoveUp.Enabled = i - >= ; //下移
btnMoveDown.Enabled = i + < listBox1.Items.Count && i != -; //第一条
btnFirst.Enabled = count > && i != ; //最后一条
btnLast.Enabled = i != count - ; //上一条
btnPre.Enabled = count > && i != && i != -; //下一条
btnNext.Enabled = i != count - && i != -; //搜索
btnFind.Enabled = textBox1.Text.Trim().Length > ; //模糊搜索
btnFind1.Enabled = btnFind.Enabled; //取消选择
btn取消选择.Enabled = i != -;
} private void 添加文本_Click(object sender, EventArgs e)
{
string s = textBox1.Text.Trim();
if (s == "")
{
MessageBox.Show("内容不能为空或空格", "操作取消", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
/*
* 不添加列表中已经存在的内容,
*/
listBox1.Items.Add(s);
}
textBox1.Text = "";
textBox1.Focus();
} private void 修改选中的项_Click(object sender, EventArgs e)
{
/*
* 可以通过这样直接修改指定的项的值 listBox1.Items[i] = "指定值";
*
*/
string s = textBox1.Text;
int i = listBox1.SelectedIndex;
if (i != -)
{
if (listBox1.Items[i].ToString() != s)
{
listBox1.Items[i] = s; s = listBox1.Items[i].ToString();
}
else
{
MessageBox.Show("名字相同", "不用重新命名", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
} private void 删除选中的项_Click(object sender, EventArgs e)
{
/*
* 这里的操作是 一次只删除一项
*/
int i = listBox1.SelectedIndex;
if (i != -)
{
DialogResult d = MessageBox.Show("是否确定删除该分组?", "删除操作", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (d == DialogResult.Yes)
{
listBox1.Items.RemoveAt(i);
textBox1.Text = ""; if (listBox1.Items.Count > )
{
if (i == listBox1.Items.Count) //这一句是避免抛出异常
{
i = ;
}
listBox1.SelectedIndex = i;
listBox1.Focus();
}
}
}
} public static void Swap(ref string a, ref string b)
{
string c = a;
a = b;
b = c;
} /*
* 上移 和下移 实际上是交换2个数值
* 上移就是[当前选中]的项和 [上一个项] 对换下值
* 下移就是[当前选中]的项和 [下一个项] 对换下值
*
*/ private void btnMoveUp_Click(object sender, EventArgs e)
{
//[上移]
string s = textBox1.Text;
int i = listBox1.SelectedIndex;
if (i != -)
{
if (i - >= )
{
string a = listBox1.Items[i].ToString();
string b = listBox1.Items[i - ].ToString();
Swap(ref a, ref b);
listBox1.Items[i] = a;
listBox1.Items[i - ] = b;
listBox1.SelectedIndex = i - ;
}
}
} private void btnMoveDown_Click(object sender, EventArgs e)
{
//[下移]
string s = textBox1.Text;
int i = listBox1.SelectedIndex;
if (i != -)
{
if (i + < listBox1.Items.Count)
{
string a = listBox1.Items[i].ToString();
string b = listBox1.Items[i + ].ToString();
Swap(ref a, ref b);
listBox1.Items[i] = a;
listBox1.Items[i + ] = b;
listBox1.SelectedIndex = i + ;
}
}
} private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
object obj = listBox1.SelectedItem;
string s = obj != null ? obj.ToString() : ""; //listbox如果没有选中则返回null
textBox1.Text = s;
SetupButtonEnabled();
} private void textBox1_TextChanged(object sender, EventArgs e)
{
//当文本框中的内容和选中的内容一样则不用重命名
SetupButtonEnabled();
} //用来存放listBox的内容
string myStr = "";
private void btnSave_Click(object sender, EventArgs e)
{
/*
* 将listBox的值转换成这种格式 aa,bb,cc,ddd,ee,ff 然后写入文件
* ,为分割符,为了避免把逗号号进来, SetupButtonEnabled中设置了如果内容包含,则不能添加
*/
string s = "";
StringBuilder sb = new StringBuilder();
int length = listBox1.Items.Count;
for (int i = ; i < length; i++)
{
s = listBox1.Items[i].ToString();
sb.Append(s + ",");
}
s = sb.ToString();
if (s.EndsWith(","))
s = s.Substring(, s.Length - );
myStr = s;
MessageBox.Show(s);
} private void btnRead_Click(object sender, EventArgs e)
{
/*
* 读取
* 将这样的 aa,bb,cc,ddd,ee,ff 字符串 转换成字符串数组然后 添加到listBox中
*/
string[] arr = myStr.Split(new char[] { ',' });
listBox1.Items.Clear();
listBox1.Items.AddRange(arr);
} private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
//双击删除选中的项
int i = listBox1.SelectedIndex;
if (i != -)
{
if (e.Clicks == )
{
listBox1.Items.RemoveAt(i);
}
}
} private void 清空_Click(object sender, EventArgs e)
{ //清空
listBox1.Items.Clear();
} private void 第一条_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > )
{
listBox1.SelectedIndex = ;
label1.Text = listBox1.SelectedItem.ToString();
} SetupButtonEnabled();
} private void 上一条_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex - >= )
{
int i = listBox1.SelectedIndex;
i--;
listBox1.SelectedIndex = i; label1.Text = listBox1.SelectedItem.ToString();
}
SetupButtonEnabled();
} private void 下一条_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex + <= listBox1.Items.Count)
{
int i = listBox1.SelectedIndex;
i++;
listBox1.SelectedIndex = i; label1.Text = listBox1.SelectedItem.ToString();
}
SetupButtonEnabled();
} private void 最后一条_Click(object sender, EventArgs e)
{
if ( listBox1.Items.Count >)
{
listBox1.SelectedIndex = listBox1.Items.Count - ;
label1.Text = listBox1.SelectedItem.ToString();
}
SetupButtonEnabled();
} private void 查找内容_Click(object sender, EventArgs e)
{
/*精确搜索*/
string s = textBox1.Text;
int i = listBox1.Items.IndexOf(s); // int i = listBox1.FindStringExact(s);
if (i != -)
{
listBox1.SelectedIndex = i;
}
else
{
MessageBox.Show("列表中没有此项");
}
} private void 模糊查询_Click(object sender, EventArgs e)
{
//模糊查询 只要项目包含关键字就行
string s = textBox1.Text;
bool b = false;
for (int i = ; i < listBox1.Items.Count; i++)
{
b = false;
string x = listBox1.Items[i].ToString();
if (x.Contains(s))
{
listBox1.SelectedIndex = i;
b = true;
break;
}
}
if (b == false)
{
MessageBox.Show("列表中没有此项");
}
} private void button1_Click(object sender, EventArgs e)
{
listBox1.SelectedIndex = -;
}
}
}

listbox demo的更多相关文章

  1. ZK框架的分析与应用

    前言:本文是在下的在学习ZK官方文档时整理出来的初稿.本来里面有很多的效果图片和图片代码的.奈何博客园中图片不能粘贴上去,所以感兴趣的筒子们就将就吧.内容中,如有不好的地方,欢迎斧正! ZK框架的分析 ...

  2. WPF 自定义列表筛选 自定义TreeView模板 自定义ListBox模板

    有很多项目,都有数据筛选的操作.下面提供一个案例,给大家做参考. 左侧是数据源,搜索框加TreeView控件,右侧是ListBox控件.在左侧数据列点击添加数据,然后点击确定,得到所筛选的数据. 下面 ...

  3. 实现一个纵向排列的 ListBox ,并具有操作按钮

    需要实现的效果如下: 要想把 ListBox 的内容纵向显示很简单,只需把 ListBox 的内容控件为 WrapPanel 就可以了: <ListBox.ItemsPanel> < ...

  4. MVVM开发模式简单实例MVVM Demo【续】

    本文将接着上篇文章,介绍一下三点:(Universal App) 1.将添加Product集合,绑定到列表 2.给点击ListBox的添加选项改变时的事件(要附加依赖属性,和Button点击事件不同) ...

  5. MVVM开发模式简单实例MVVM Demo

    本文主要是翻译Rachel Lim的一篇有关MVVM模式介绍的博文 A Simple MVVM Example 并具体给出了一个简单的Demo(原文是以WPF开发的,对于我自己添加或修改的一部分会用红 ...

  6. WPF ,listbox,平滑滚动的2种方式。

    一,烤地瓜版本的..  这个版本不安装内容滚动,,鼠标滑轮滚动一次距离相同, 具体步骤参照他的博客,说点注意的,, 1,ScrollViewer.CanContentScroll="Fals ...

  7. silverlight ListBox 多列图片效果

    这个功能之前用wpf写过一次这次用Silverlight写一次 这两种写法上基本上没有太大的差别 这个Demo并不完美,只是给大家提供一个思路 源码:SilverLightListPricture.r ...

  8. windows phone listbox虚拟化(下)

    之前写过一篇关于listbox虚拟化的文章,那里采用的方法都是自己早期研究的一些思路,然后发现当数据很大的时候,其实性能效果还是不太理想,下面让我们来仔细想一想到底是基于什么原因,我们回去破坏默认的虚 ...

  9. 自定义可判断选项是否正确listbox

    截图如下:        1.实现Converter  获取到listbox,并得到listitem在listbox中的index public class ItemContainerToZIndex ...

随机推荐

  1. adb server is out of date. killing... ADB server didn't ACK * failed to start daemon *……

    问题 使用 adb 命令的时候报错如下: adb server is out of date. killing... ADB server didn't ACK * failed to start d ...

  2. ORACLE主键ID的生成

    转自:https://blog.csdn.net/yh_zeng2/article/details/83477880 一般常用的方法有两种,使用Sequence和使用SYS_GUID(); 方法一  ...

  3. UIAlertController 修改文字显示实现方法

    UIAlertController修改文字显示 不废话先上完整代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 UIAlertControll ...

  4. JDK的安装(mac)

    1.第一步安装brew 教学网址 2.用brew安装jdk. brew update brew cask install java(未翻墙时长很长,大概猴年马月两个小时) 安装完成后就可以执行JAVA ...

  5. Struts框架笔记04_拦截器_标签库

    目录 1. Struts2的拦截器 1.1 拦截器概述 1.2 拦截器的实现原理 1.3 Struts的执行流程 1.4 拦截器入门 1.4.1 环境搭建 1.4.2 编写拦截器 1.4.3 配置拦截 ...

  6. 阿里云SOP

    阿里云SOP 摘要 注册阿里云账号. 领取及配置ECS. 领取及配置RDS. 部署网站. 注册阿里云账号 在主页点击注册 填入相应的信息 领取及配置ECS 注册后领取免费的ECS,RDS. 打开控制台 ...

  7. Elasticsearch 术语介绍和CRUD实际操作入门

    一.Elastic Stack 核心Elasticsearch Elasticsearch 是一个分布式.RESTful 风格的搜索和数据分析引擎.Elasticsearch 是面向文档的,这就意味着 ...

  8. GAE相关

    Google App Engine for Java是可以在Google托管服务器基础架构上托管和运行用户Web应用程序.出于安全原因,这些应用程序在沙盒环境中执行. 沙箱本身由两层组成.第一层是GA ...

  9. 用java刷剑指offer(数字在排序数组中出现的次数)

    题目描述 统计一个数字在排序数组中出现的次数. 牛客网链接 java代码 //看见有序就用二分法 public class Solution { public int GetNumberOfK(int ...

  10. c++第四次作业

    继承与派生--访问控制 一.知识要点 (一)知识回顾: 基类的成员可以有public.protected.private三种访问属性.基类的自身成员可以对基类中任何一个其他成员进行访问,但是通过基类的 ...