功能

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

下载地址: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. python day 8: re模块补充,导入模块,hashlib模块,字符串格式化,模块知识拾遗,requests模块初识

    目录 python day 8 1. re模块补充 2. import模块导入 3. os模块 4. hashlib模块 5. 字符串格式:百分号法与format方法 6. 模块知识拾遗 7. req ...

  2. Java 之 数据库连接池

    一.数据库连接池 1.连接池概念 连接池其实就是一个容器(集合),存放数据库连接的容器. 当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问之 ...

  3. go语言实现限流器

    本文:https://chai2010.cn/advanced-go-programming-book/ch5-web/ch5-06-ratelimit.html Ratelimit 服务流量限制 计 ...

  4. 浅谈 form 表单提交

    原创文章,转载请注明出处:http://www.cnblogs.com/weix-l/p/7675230.html 若有错误,请评论指出,谢谢! Form 对象代表一个 HTML 表单.在 HTML ...

  5. SpringMVC框架笔记01_SpringMVC的使用案例和架构组件_SpringMVC和Mybatis整合_接收参数

    目录 第1章:SpringMVC简介 1.1 什么是SpringMVC 1.2 SpringMVC的处理流程 第2章:SpringMVC入门程序 2.1 场景描述 2.2 步骤分析 2.3 步骤一:创 ...

  6. 批量导入数据到InnoDB表速度优化

    1.使用Load data: 2. SET autocommit=0; ... SQL import statements ... COMMIT; 3. SET unique_checks=0; .. ...

  7. sqlite3入门之sqlite3_get_table,sqlite3_free_table

    sqlite3_get_table sqlite3_get_table函数原型: int sqlite3_get_table( sqlite3 *db, /* An open database */ ...

  8. 本地安装部署ActiveCollab

    ActiveCollab是一个非常易于使用.基于Web.开源的协作开发与项目管理工具. 我们公司一直在用这款工具,进行任务分配和时间填写,十分简便 ActiveCollab可以利用它轻松地搭建一个包括 ...

  9. 铁力项目mysql异常处理过程记录

    地区:铁力 故障:2019-06-26 10:19:34 139921514837760 [ERROR] mysqld: Error writing file 'mysql-bin' (errno: ...

  10. Random类产生随机数

    Random 类作为JAVA中用于产生的随机数 ,new  Random(10)  :10是种子数. 注意:Random 的一个特点是:相同种子数的Random对象,对应相同次数生成的随机数字是完全相 ...