詳解C#如何對(duì)ListBox控件中的數(shù)據(jù)進(jìn)行操作
C#中對(duì)ListBox控件中的數(shù)據(jù)進(jìn)行的操作主要包括添加、刪除、清空、選擇、排序等。
1.添加數(shù)據(jù)
// 添加一個(gè)字符串?dāng)?shù)組 listBox1.Items.AddRange(new string[] { "item1", "item2", "item3" }); // 或者添加單個(gè)字符串 listBox1.Items.Add("item4");
2.刪除數(shù)據(jù)
// 刪除選定的項(xiàng) listBox1.Items.Remove(listBox1.SelectedItem); // 或者刪除第n項(xiàng) listBox1.Items.RemoveAt(n);
3.清空數(shù)據(jù)
listBox1.Items.Clear();
4.選擇項(xiàng)
// 選擇第n項(xiàng) listBox1.SelectedIndex = n; // 或者選擇包含特定文本的項(xiàng) for (int i = 0; i < listBox1.Items.Count; i++) { if (listBox1.Items[i].ToString() == "item4") { listBox1.SelectedIndex = i; break; } }
5.排序
//listBox1排序); listBox1.Sorted = true;
6.獲取選中的項(xiàng)
int selectedIndex = listBox1.SelectedIndex;
7.獲取ListBox中的所有項(xiàng)
List<string> allItems = new List<string>(); foreach (string item in listBox1.Items) { allItems.Add(item.ToString()); }
8.綜合示例
// ListBox控件操作 using System.Diagnostics; using System.Linq; namespace _148_2 { public partial class Form1 : Form { private static ListBox? listBox1; private Button? button1; private static TextBox? textBox1; private Button? button2; private Button? button3; private Button? button4; public Form1() { InitializeComponent(); StartPosition = FormStartPosition.CenterScreen; Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { // // listBox1 // listBox1 = new ListBox { FormattingEnabled = true, ItemHeight = 17, Location = new Point(12, 12), Name = "listBox1", Size = new Size(270, 174), TabIndex = 1 }; // // button1 // button1 = new Button { ForeColor = SystemColors.ActiveCaptionText, TabIndex = 2, Text = "操作", UseVisualStyleBackColor = true, Location = new Point(231, 221), Name = "button1", Size = new Size(50, 23) }; button1.Click += Button1_Click; // // textBox1 // textBox1 = new TextBox { Location = new Point(12, 192), Name = "textBox1", Size = new Size(270, 23), TabIndex = 3 }; // // button2 // button2 = new Button { ForeColor = SystemColors.ActiveCaptionText, TabIndex = 4, Text = "清空", UseVisualStyleBackColor = true, Location = new Point(166, 221), Name = "button2", Size = new Size(49, 23) }; button2.Click += Button2_Click; // // button3 // button3 = new Button { ForeColor = SystemColors.ActiveCaptionText, Location = new Point(12, 221), Name = "button3", Size = new Size(75, 23), TabIndex = 5, Text = "復(fù)制全部", UseVisualStyleBackColor = true }; button3.Click += Button3_Click; // // button4 // button4 = new Button { ForeColor = SystemColors.ActiveCaptionText, Location = new Point(103, 221), Name = "button4", Size = new Size(47, 23), TabIndex = 6, Text = "刪除", UseVisualStyleBackColor = true }; button4.Click += Button4_Click; // // Form1 // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(294, 255); Controls.Add(button4); Controls.Add(button3); Controls.Add(button2); Controls.Add(textBox1); Controls.Add(button1); Controls.Add(listBox1); ForeColor = SystemColors.ControlLightLight; Name = "Form1"; Text = "ListBox操作"; } private void Button1_Click(object? sender, EventArgs e) { ListBoxOperations(); } private static void ListBoxOperations() { // 創(chuàng)建一個(gè)字符串?dāng)?shù)組 string[] items = ["item3", "item2", "item1"]; // 添加字符串?dāng)?shù)組到ListBox listBox1!.Items.AddRange(items); // 添加單個(gè)字符串到ListBox listBox1.Items.Add("item4"); //listBox1排序 listBox1.Sorted = true; // 選擇第2個(gè)項(xiàng)(索引從0開始) listBox1.SelectedIndex = 1; // 獲取選中的項(xiàng) string selectedValue = listBox1.SelectedItem!.ToString()!; textBox1!.Text = "Selected Value: " + selectedValue; // 獲取選中的項(xiàng)的索引 int selectedIndex = listBox1.SelectedIndex; textBox1!.Text += " Selected Index: " + selectedIndex; } // 清空所有 private void Button2_Click(object? sender, EventArgs e) { listBox1!.Items.Clear(); } // 復(fù)制并添加全部 private void Button3_Click(object? sender, EventArgs e) { List<string> allItems = []; foreach (string item in listBox1!.Items) { allItems.Add(item.ToString()); } foreach (string item in allItems) { listBox1.Items.Add(item); } } // 刪除選中 private void Button4_Click(object? sender, EventArgs e) { listBox1!.Items.Remove(listBox1.SelectedItem!); } } }
結(jié)果如下
到此這篇關(guān)于詳解C#如何對(duì)ListBox控件中的數(shù)據(jù)進(jìn)行操作的文章就介紹到這了,更多相關(guān)C#操作ListBox數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# MVC 使用LayUI實(shí)現(xiàn)下拉框二級(jí)聯(lián)動(dòng)的功能
這篇文章主要介紹了C# MVC 如何使用LayUI實(shí)現(xiàn)下拉框二級(jí)聯(lián)動(dòng),文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06C# 中的 IReadOnlyDictionary 和 IReadOnlyLis
C# 中的IReadOnlyDictionary和IReadOnlyList是接口,用于表示只讀的字典和只讀的列表,這些接口提供了對(duì)集合的只讀訪問權(quán)限,即不允許對(duì)集合進(jìn)行修改操作,這篇文章主要介紹了C# 中的 IReadOnlyDictionary 和 IReadOnlyList實(shí)例詳解,需要的朋友可以參考下2024-03-03C# 數(shù)獨(dú)求解算法的實(shí)現(xiàn)
這篇文章主要介紹了C# 數(shù)獨(dú)求解算法的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-0110分鐘學(xué)會(huì)VS NuGet包私有化部署
本文主要介紹了10分鐘學(xué)會(huì)VS NuGet包私有化部署,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09C#使用WebService結(jié)合jQuery實(shí)現(xiàn)無刷新翻頁的方法
這篇文章主要介紹了C#使用WebService結(jié)合jQuery實(shí)現(xiàn)無刷新翻頁的方法,涉及C#中WebService與jQuery操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04C#利用iTextSharp組件給PDF文檔添加圖片/文字水印
這篇文章主要給大家介紹了關(guān)于如何C#利用iTextSharp組件給PDF文檔添加圖片/文字水印的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10