C#ListBox用法
来源:互联网 发布:软件培训可靠吗 编辑:程序博客网 时间:2024/05/17 10:04
ListBox控件
1.功能
ListBox控件显示较长的选项列表,用户可从中选择一项或多项。如果项总数超出可以显示的项数,则自动向ListBox控件添加滚动条。ListBox控件列表中的每个元素称为项。图1所示为ListBox控件。
图1 ListBox控件
2.属性
ListBox控件常用属性及说明如表1所示。
表1 ListBox控件常用属性及说明
下面对比较重要的属性进行详细介绍。
(1)Items属性。该属性用于查看列表框中的项。
语法:
说明:
① 该属性使用户可以获取对当前存储在 ListBox 中的项列表的引用。通过此引用,可以在集合中添加项、移除项和获得项的计数。
② 可以使用DataSource属性来操控ListBox的项。如果使用DataSource属性向ListBox添加项,则可以使用Items属性查看ListBox中的项,但不能使用ListBox.ObjectCollection的方法向该列表添加项或从中移除项。
(2)SelectedItem属性。该属性表示当前选中的项。
语法:
public Object SelectedItem { get; set; }属性值:表示控件中当前选定内容的对象。
说明:对于标准 ListBox,可以使用此属性确定在ListBox中选定了哪个项。如果 ListBox的SelectionMode属性设置为SelectionMode.MultiSimple或SelectionMode.MultiExtended(它指示多重选择ListBox),并在该列表中选定了多个项,则此属性可返回任何选定的项。
示例
把左边的文本框中的内容移到右边的文本框中
本示例主要使用Items属性向ListBox1控件添加项,然后使用SelectedItem属性将ListBox1控件中的选中项添加到ListBox2控件中。示例运行结果如图2和图3所示。
图2 ListBox1中项移动前
图3 ListBox1中项移动后
程序主要代码如下:
SqlConnection con = new SqlConnection("server=ZHY\\zhy;uid=sa;pwd=;database=student");
con.Open();
SqlCommand com = new SqlCommand("select * from student",con);
SqlDataReader dr = com.ExecuteReader();
this.listBox1.Items.Clear();
while (dr.Read())
{
// this.listBox1.Items.Add(dr[0].ToString());
this.listBox1.Items.Add(dr[1].ToString());
// this.listBox1.Items.Add(dr[2].ToString());
}
dr.Close();
con.Close();注意:此示例使用了数据库,所以需要引用命名空间using System.Data.SqlClient。
完整程序代码如下:
★★★★★主程序文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _8_05
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmListBox());
}
}
}★★★★★Form1窗体设计文件完整程序代码★★★★★(责任编辑:admin)
. 属性列表:
SelectionMode 组件中条目的选择类型,即多选(Multiple)、单选(Single)
Rows 列表框中显示总共多少行
Selected 检测条目是否被选中
SelectedItem 返回的类型是ListItem,获得列表框中被选择的条目
Count 列表框中条目的总数
SelectedIndex 列表框中被选择项的索引值
Items 泛指列表框中的所有项,每一项的类型都是ListItem
2. 取列表框中被选中的值
ListBox.SelectedValue
3. 动态的添加列表框中的项:
ListBox.Items.Add("所要添加的项");
4. 移出指定项:
//首先判断列表框中的项是否大于0
If(ListBox.Items.Count > 0 )
{
//移出选择的项
ListBox.Items.Remove(ListBox.SelectedItem);
}
5. 清空所有项:
//首先判断列表框中的项是否大于0
If(ListBox.Items.Count > 0 )
{
//清空所有项
ListBox.Items.Clear();
}
6. 列表框可以一次选择多项:
只需设置列表框的属性 SelectionMode="Multiple",按Ctrl可以多选
7. 两个列表框联动,即两级联动菜单
//判断第一个列表框中被选中的值
switch(ListBox1.SelectValue)
{
//如果是"A",第二个列表框中就添加这些:
case "A"
ListBox2.Items.Clear();
ListBox2.Items.Add("A1");
ListBox2.Items.Add("A2");
ListBox2.Items.Add("A3");
//如果是"B",第二个列表框中就添加这些:
case "B"
ListBox2.Items.Clear();
ListBox2.Items.Add("B1");
ListBox2.Items.Add("B2");
ListBox2.Items.Add("B3");
}
8. 实现列表框中项的移位
即:向上移位、向下移位
具体的思路为:创建一个ListBox对象,并把要移位的项先暂放在这个对象中。
如果是向上移位,就是把当前选定项的的上一项的值赋给当前选定的项,然后
把刚才新加入的对象的值,再附给当前选定项的前一项。
具体代码为:
//定义一个变量,作移位用
index = -1;
//将当前条目的文本以及值都保存到一个临时变量里面
ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue);
//被选中的项的值等于上一条或下一条的值
ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex + index].Text;
//被选中的项的值等于上一条或下一条的值
ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex + index].Value;
//把被选中项的前一条或下一条的值用临时变量中的取代
ListBox.Items[ListBox.SelectedIndex].Test=lt.Test;
//把被选中项的前一条或下一条的值用临时变量中的取代
ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
//把鼠标指针放到移动后的那项上
ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
9. 移动指针到指定位置:
(1).移至首条
//将被选中项的索引设置为0就OK了
ListBox.SelectIndex=0;
(2).移至尾条
//将被选中项的索引设置为ListBox.Items.Count-1就OK了
ListBox.SelectIndex=ListBox.Items.Count-1;
(3).上一条
//用当前被选中的索引去减 1
ListBox.SelectIndex=ListBox.SelectIndex - 1;
(4).下一条
//用当前被选中的索引去加 1
ListBox.SelectIndex=ListBox.SelectIndex + 1;
this.ListBox1.Items.Insertat(3,new ListItem("插入在第3行之后项",""));
this.ListBox1.Items.Insertat(index,ListItem)
ListBox1.Items.Insert(0,new ListItem("text","value"));
- C#listbox用法
- C#listbox用法
- C#listbox控件用法
- C#listbox用法
- C#ListBox用法
- C#listbox用法
- C#listbox用法
- C#ListBox用法
- ListBox用法
- ListBox 用法
- ListBox用法
- listbox用法摘要
- ListBOX用法总结
- ListBOX用法总结
- ListBox的用法
- vb-listbox用法
- ListBOX用法总结
- listbox基本用法
- 最新九月百度人搜,阿里巴巴,腾讯华为京东面试二十题(更新至09.19)
- 数学之美番外篇:平凡而又神奇的贝叶斯方法(2)——刘未鹏
- java IO(应用篇)
- 用qemu-system-arm搭建arm11环境
- centos6.2 安装总结:
- C#ListBox用法
- nginx
- 酒弄人生
- MFC 对话框圆角矩形的实现方法
- 那些年,我们一起学WCF--(4)数据契约
- SAP BW DSO与InfoCube比较
- 神奇的LocalAlloc
- eclipse+makefile开发hadoop
- Windows下Git安装与配置