国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
C#ListBox用法


2012-09-20 15:42
ListBox控件顯示較長的選項列表,用戶可從中選擇一項或多項。如果項總數(shù)超出可以顯示的項數(shù),則自動向ListBox控件添加滾動條。ListBox控件列表中的每個元素稱為項。圖1所示為ListBox控件

ListBox控件
1.功能
ListBox控件顯示較長的選項列表,用戶可從中選擇一項或多項。如果項總數(shù)超出可以顯示的項數(shù),則自動向ListBox控件添加滾動條。ListBox控件列表中的每個元素稱為項。圖1所示為ListBox控件。


圖1 ListBox控件
2.屬性
ListBox控件常用屬性及說明如表1所示。

表1 ListBox控件常用屬性及說明
下面對比較重要的屬性進行詳細介紹。
(1)Items屬性。該屬性用于查看列表框中的項。
語法:

public ObjectCollection Items { get; }
屬性值:ListBox.ObjectCollection表示ListBox中的項。
說明:
① 該屬性使用戶可以獲取對當前存儲在 ListBox 中的項列表的引用。通過此引用,可以在集合中添加項、移除項和獲得項的計數(shù)。
② 可以使用DataSource屬性來操控ListBox的項。如果使用DataSource屬性向ListBox添加項,則可以使用Items屬性查看ListBox中的項,但不能使用ListBox.ObjectCollection的方法向該列表添加項或從中移除項。
(2)SelectedItem屬性。該屬性表示當前選中的項。
語法:
 public Object SelectedItem { get; set; }
屬性值:表示控件中當前選定內(nèi)容的對象。
說明:對于標準 ListBox,可以使用此屬性確定在ListBox中選定了哪個項。如果 ListBox的SelectionMode屬性設(shè)置為SelectionMode.MultiSimple或SelectionMode.MultiExtended(它指示多重選擇ListBox),并在該列表中選定了多個項,則此屬性可返回任何選定的項。
示例
把左邊的文本框中的內(nèi)容移到右邊的文本框中
本示例主要使用Items屬性向ListBox1控件添加項,然后使用SelectedItem屬性將ListBox1控件中的選中項添加到ListBox2控件中。示例運行結(jié)果如圖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();
注意:此示例使用了數(shù)據(jù)庫,所以需要引用命名空間using System.Data.SqlClient。
完整程序代碼如下:
★★★★★主程序文件完整程序代碼★★★★★
 using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _8_05
{
static class Program
{
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmListBox());
}
}
}
★★★★★Form1窗體設(shè)計文件完整程序代碼★★★★★(責任編輯:admin)

. 屬性列表:

    SelectionMode    組件中條目的選擇類型,即多選(Multiple)、單選(Single)
    Rows             列表框中顯示總共多少行
    Selected         檢測條目是否被選中
    SelectedItem     返回的類型是ListItem,獲得列表框中被選擇的條目
    Count            列表框中條目的總數(shù)
    SelectedIndex    列表框中被選擇項的索引值
    Items            泛指列表框中的所有項,每一項的類型都是ListItem

2. 取列表框中被選中的值

     ListBox.SelectedValue

3. 動態(tài)的添加列表框中的項:

     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. 列表框可以一次選擇多項:
   
     只需設(shè)置列表框的屬性 SelectionMode="Multiple",按Ctrl可以多選

7. 兩個列表框聯(lián)動,即兩級聯(lián)動菜單

     //判斷第一個列表框中被選中的值
     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. 實現(xiàn)列表框中項的移位
     即:向上移位、向下移位
     具體的思路為:創(chuàng)建一個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).移至首條
          //將被選中項的索引設(shè)置為0就OK了
          ListBox.SelectIndex=0;
      (2).移至尾條
          //將被選中項的索引設(shè)置為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"));

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
listbox控件的一些操作
vb.net入門——ComboBox 控件的使用
DropDownList和ListBox下拉列表學習
在C#中實現(xiàn)listbox的項上下移動(winform)
V8.A11ComboBox 控件、ListBox 控件和CheckedListBox 控件
SelectedValue與SelectedItem.Value的區(qū)別
更多類似文章 >>
生活服務(wù)
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服