listbox demo

功能
下载地址: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的更多相关文章
- ZK框架的分析与应用
前言:本文是在下的在学习ZK官方文档时整理出来的初稿.本来里面有很多的效果图片和图片代码的.奈何博客园中图片不能粘贴上去,所以感兴趣的筒子们就将就吧.内容中,如有不好的地方,欢迎斧正! ZK框架的分析 ...
- WPF 自定义列表筛选 自定义TreeView模板 自定义ListBox模板
有很多项目,都有数据筛选的操作.下面提供一个案例,给大家做参考. 左侧是数据源,搜索框加TreeView控件,右侧是ListBox控件.在左侧数据列点击添加数据,然后点击确定,得到所筛选的数据. 下面 ...
- 实现一个纵向排列的 ListBox ,并具有操作按钮
需要实现的效果如下: 要想把 ListBox 的内容纵向显示很简单,只需把 ListBox 的内容控件为 WrapPanel 就可以了: <ListBox.ItemsPanel> < ...
- MVVM开发模式简单实例MVVM Demo【续】
本文将接着上篇文章,介绍一下三点:(Universal App) 1.将添加Product集合,绑定到列表 2.给点击ListBox的添加选项改变时的事件(要附加依赖属性,和Button点击事件不同) ...
- MVVM开发模式简单实例MVVM Demo
本文主要是翻译Rachel Lim的一篇有关MVVM模式介绍的博文 A Simple MVVM Example 并具体给出了一个简单的Demo(原文是以WPF开发的,对于我自己添加或修改的一部分会用红 ...
- WPF ,listbox,平滑滚动的2种方式。
一,烤地瓜版本的.. 这个版本不安装内容滚动,,鼠标滑轮滚动一次距离相同, 具体步骤参照他的博客,说点注意的,, 1,ScrollViewer.CanContentScroll="Fals ...
- silverlight ListBox 多列图片效果
这个功能之前用wpf写过一次这次用Silverlight写一次 这两种写法上基本上没有太大的差别 这个Demo并不完美,只是给大家提供一个思路 源码:SilverLightListPricture.r ...
- windows phone listbox虚拟化(下)
之前写过一篇关于listbox虚拟化的文章,那里采用的方法都是自己早期研究的一些思路,然后发现当数据很大的时候,其实性能效果还是不太理想,下面让我们来仔细想一想到底是基于什么原因,我们回去破坏默认的虚 ...
- 自定义可判断选项是否正确listbox
截图如下: 1.实现Converter 获取到listbox,并得到listitem在listbox中的index public class ItemContainerToZIndex ...
随机推荐
- orangepi获取cpu温度
cat /sys/devices/virtual/hwmon/hwmon1/temp1_input
- viewer 图片点击放大 用法汇总
A 不用viewer插件 1弹出框 https://www.cnblogs.com/web1/p/8989967.html 2表格中 https://www.jianshu.com/p/c17f4f6 ...
- linux各种服务的搭建
https://blog.csdn.net/qq_33571718/article/details/81543408 VPN --linux服务搭建 https://blog.csdn.net/ ...
- Linux命令——watch
参考:Linux watch Command Tutorial for Beginners (5 Examples) 前言 有的时候我们想重复执行某一命令,通过该命令的输出进而获知系统某些信息.wat ...
- 白话解说TCP/IP协议三次握手和四次挥手
白话解说TCP/IP协议三次握手和四次挥手 1.背景 和女朋友异地恋一年多,为了保持感情我提议每天晚上视频聊天一次. 从好上开始,到现在,一年多也算坚持下来了. 1.1.问题 有时候聊天的过程中,我的 ...
- 农业银行网上支付平台-商户接口编程-demo调试
调试的时候会报一个这样的错误. ReturnCode = [1999]ErrorMessage = [系统发生无法预期的错误 - 第1个证书无法读取证书文档] 网上其他资料说是权限问题,有的人可能是权 ...
- 一个在开源中国博客上讲解的AC自动机
原文出处:http://my.oschina.net/amince/blog/196426 原 荐 AC(Aho—Corasiek) 多模式匹配算法 摘要 如何在一篇文章中,搜索多个关键字,如何快速查 ...
- LeetCode - 86、分隔链表
给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1->4-&g ...
- 基于Java+Selenium的WebUI自动化测试框架(十二)-----读取Excel文件(POI)(2)
上一篇我们讲了怎么利用Java的反射机制,将Excel的读取到的数据,赋值给我们构造函数中定义的变量. 接下来就简单了,我们将实际实现这个读取的简单过程.来看下面一段代码. private stati ...
- 苹果cms和海洋cms通用的百度主动推送工具
百度主动推送的代码,不需要每天手动去添加地址推送,只要浏览器打开推送请求,不要关掉浏览器,程序自动帮你推送.(该插件只推送内容页,支持动态.伪静态.静态页面的推送,但这三种地址规则需要去代码里面自行拼 ...