C# boxlist用法(全).doc_第1页
C# boxlist用法(全).doc_第2页
C# boxlist用法(全).doc_第3页
C# boxlist用法(全).doc_第4页
C# boxlist用法(全).doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

属性列表: SelectionMode 组件中条目的选择类型,即多选(Multiple)、单选(Single) Rows 列表框中显示总共多少行 Selected 检测条目是否被选中 SelectedItem 返回的类型是ListItem,获得列表框中被选择的条目 Count 列表框中条目的总数 SelectedIndex 列表框中被选择项的索引值 Items 泛指列表框中的所有项,每一项的类型都是ListItem2. 取列表框中被选中的值 ListBox.SelectedValue3. 动态的添加列表框中的项: 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.ItemsListBox.SelectedIndex.Text=ListBox.ItemsListBox.SelectedIndex + index.Text; /被选中的项的值等于上一条或下一条的值 ListBox.ItemsListBox.SelectedIndex.Value=ListBox.ItemsListBox.SelectedIndex + index.Value; /把被选中项的前一条或下一条的值用临时变量中的取代 ListBox.ItemsListBox.SelectedIndex.Test=lt.Test; /把被选中项的前一条或下一条的值用临时变量中的取代 ListBox.ItemsListBox.SelectedIndex.Value=lt.Value; /把鼠标指针放到移动后的那项上 ListBox.ItemsListBox.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);protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) BindLsb(); #region 绑定listbox protected void BindLsb() SqlConnection conn = CDB.Getconn(constr,1); conn.Open(); CDB.BindListBox(ListBox1, select * from caes_memberinfo_column_dictionary, columnname_inchinese, columnid, conn); #endregion #region 按钮 /全部增加 protected void Button1_Click(object sender, EventArgs e) ListBox2.Items.Clear(); for (int i=0;i 0) for (int j = 0; j ListBox2.Items.Count; j+) if (ListBox2.Itemsj.Value = ListBox1.SelectedItem.Value) state = 1; ListBox1.SelectedItem.Selected = false; Response.Write(alert(已经有该选项了,请重新选择!); break; if (state = 0) ListBox2.Items.Add(ListBox1.SelectedItem); ListBox2.Items.FindByValue(ListBox1.SelectedItem.Value).Selected = false; else ListBox2.Items.Add(ListBox1.SelectedItem); ListBox2.Items.FindByValue(ListBox1.SelectedItem.Value).Selected = false; /删除一项 protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e) for (int i = 0; i ListBox2.Items.Count; i+) if (ListBox2.Itemsi.Selected = true) ListBox2.Items.Remove(ListBox2.SelectedItem); /删除全部 protected void Button4_Click(object sender, EventArgs e) for (int i = 0; i 0) int index = ListBox1.SelectedIndex; string temp = ListBox1.Itemsindex - 1.ToString(); ListBox1.Itemsindex - 1 = ListBox1.SelectedItem.ToString(); ; ListBox1.Itemsindex = temp; ListBox1.SelectedIndex = index - 1; private void moveDownListBox(ListBox ListBox1) /*/*/*/向下移动 /若不是第最后一行则下移 if (ListBox1.SelectedIndex -1) listbox.SelectedIndex = idx; else listbox.SelectedIndex = indexoftarget; private void ListBox1_MouseDown(object sender, MouseEventArgs e) sourcelbl = (ListBox)sender; int indexofsource = sourcelbl.SelectedIndex; if (indexofsource -1) sourcelbl.DoDragDrop(sourcelbl.Itemsindexofsource.ToString(), DragDropEffects.All); ListBox C# 每行的背景色private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) Brush br; int r; Math.DivRem(e.Index,2,out r); if(r = 0) br = Brushes.Red; else br = Brushes.Blue; e.DrawBackground(); if(e.State & DrawItemState.Selec

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论