国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
C# 中列表框的類型?
如何用 C# 創(chuàng)建列表框?
1.設(shè)計(jì)時(shí)
2.運(yùn)行時(shí)
C# 中的列表框示例
示例#1 – 創(chuàng)建列表框并添加元素
示例 #2 – 用戶輸入值,通過單擊按鈕將其添加到列表框中
Example #3 – Delete, Change the font of List Box values
Conclusion

C# 中的列表框

Sep 03, 2024 pm 03:28 PM
c# c# tutorial

C#中的ListBox定義為將元素列表添加到ListBox中以對單個(gè)或多個(gè)元素進(jìn)行操作。下拉框和列表框的區(qū)別在于下拉框一次只能選擇一個(gè)元素,而列表框則可以一次選擇單個(gè)或多個(gè)元素。 ListBox 提供不同類型的方法、屬性和事件。此列表框是在 System 下指定的。 Windows.Forms 包(命名空間)。

ListBox 類再次包含 C# 中 3 種不同類型的集合。他們是

  1. ?ListBox.ObjectCollection:此集合類包含 ListBox 控件的所有元素。
  2. ?ListBox.SelectedObjectCollection:此集合類保存 ListBox 控件中所選項(xiàng)目的集合。
  3. ?ListBox.SelectedIndexCollection:?此集合類保存選定索引的集合;這些元素是 ListBox.ObjectCollection 索引的子集,并且是 ListBox 控件中專門選擇的索引。

C# 中列表框的類型?

  1. 單選列表框:ListBox 只能從列表中選擇單個(gè)元素。
  2. 多選ListBox:ListBox可以從列表中選擇多個(gè)元素。

C# 中 ListBox 的先決條件:

  • .Net 庫必須安裝在您的電腦上
  • Visual Studio 設(shè)置

如何用 C# 創(chuàng)建列表框?

ListBox 可以通過兩種方式創(chuàng)建:

  • 設(shè)計(jì)時(shí)
  • 運(yùn)行時(shí)

1.設(shè)計(jì)時(shí)

最初無需任何代碼即可輕松創(chuàng)建。創(chuàng)建項(xiàng)目的步驟

第 1 步:打開 Visual Studio

點(diǎn)擊文件=>新建=>項(xiàng)目

C# 中的列表框

選擇=>Windows Form Application,然后

請參閱下圖以更好地了解項(xiàng)目結(jié)構(gòu):

C# 中的列表框

為項(xiàng)目命名并單擊“確定”,您將獲得如下所示的 Form1.cs(Design) 選項(xiàng)卡

C# 中的列表框

第2步:在視覺工作室左側(cè)或從視圖中,選擇“工具箱”;接下來,將所需的元素拖放到 Form1.cs(Design) 上,如上圖所示。

C# 中的列表框

C# 中的列表框

第 3 步:拖放后,從 Visual Studio 右側(cè)選擇屬性,并為 Text 屬性指定一些名稱。這用于在 2nd 方法運(yùn)行時(shí).

中編寫代碼

C# 中的列表框

輸出:

C# 中的列表框

2.運(yùn)行時(shí)

這不是直接按照上面的方法來做。我們已經(jīng)編寫了一些程序來創(chuàng)建ListBox。這很簡單;首先,拖放所有必需的元素,如 ListBox、Label、TextField、Button 等。如果雙擊任何拖放的元素,我們會得到一些元素操作方法的 C# 代碼,我們必須將邏輯寫入我們想要的內(nèi)容。想要用這些元素來做。創(chuàng)建運(yùn)行時(shí)項(xiàng)目代碼以創(chuàng)建 ListBox

的步驟

第 1 步:使用 ListBox() 構(gòu)造函數(shù)創(chuàng)建 ListBox 控件。

語法:

ListBox listBox = new ListBox();

第2步:創(chuàng)建ListBox屬性后,如果我們想給ListBox的元素設(shè)置Font、Font.Size、Color等屬性

語法:

listBox.Location = new Point(200, 100);
listBox.Size = new Size(100, 90);
listBox.ForeColor = Color.Red;

第 3 步:將元素添加到 ListBox。

語法:

listBox.Items.Add("A");
listBox.Items.Add("B");
listBox.Items.Add("C");
listBox.Items.Add("D");

第 4 步:將此列表框添加到表單中。

語法:

this.Controls.Add(listBox);

C# 中的列表框示例

以下是下面提到的示例

示例#1 – 創(chuàng)建列表框并添加元素

代碼:

//importing C# required libraries
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 is project name
namespace WindowsFormsApplication26
{
//creating class extends from Form class
public partial class Form1 : Form
{
//constrcutor
public Form1()
{
//initializing components
InitializeComponent();
//Creating list box and add some properties and values to the List Box
listBox2.ForeColor = Color.Red;
listBox2.Items.Add("Java");
listBox2.Items.Add("Python");
listBox2.Items.Add("C++");
listBox2.Items.Add("C");
listBox2.Items.Add("C#");
listBox2.Items.Add("Spring");
listBox2.Items.Add("JavaFX");
listBox2.SelectionMode = SelectionMode.MultiSimple;
}
//method for selectedIndex change operation
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}

輸出:

C# 中的列表框

示例 #2 – 用戶輸入值,通過單擊按鈕將其添加到列表框中

代碼:

//importing C# required libraries
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 is project name
namespace WindowsFormsApp25
{
//creating class extends from Form class
public partial class Form1 : Form
{
//constrcutor
public Form1()
{
//initializing components
InitializeComponent();
}
//saving the enter values into List box
private void buttonSave_Click(object sender, EventArgs e)
{
//If user enter any values then if block executes
if (this.textBoxName.Text != "")
{
NameList.Items.Add(this.textBoxName.Text);
this.textBoxName.Focus();
this.textBoxName.Clear();
}
//If user did not enter any values then else block executes
else
{
MessageBox.Show("Please enter a name to add..","Error",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.textBoxName.Focus();
}
}
}
}

輸出:

輸入值之前:

C# 中的列表框

在不輸入任何值的情況下嘗試單擊保存按鈕:

C# 中的列表框

輸入值后:

C# 中的列表框

After entering a value and clicking the save button:

C# 中的列表框

Example #3 – Delete, Change the font of List Box values

Code:

//importing C# required libraries
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 is project name
namespace WindowsFormsApp25
{
//creating class extends from Form class
public partial class Form1 : Form
{
//constrcutor
public Form1()
{
//initializing components
InitializeComponent();
}
//saving the enter values into List box
private void buttonSave_Click(object sender, EventArgs e)
{
//If user enter any values then if block executes
if (this.textBoxName.Text != "")
{
NameList.Items.Add(this.textBoxName.Text);
this.textBoxName.Focus();
this.textBoxName.Clear();
}
//If user did not enter any values then else block executes
else
{
MessageBox.Show("Please enter a name to add..","Error",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.textBoxName.Focus();
}
}
//Removing the selected elements
private void button2_Click(object sender, EventArgs e)
{
if (this.NameList.SelectedIndex >= 0)
{
this.NameList.Items.RemoveAt(this.NameList.SelectedIndex);
}
}
//Setting List box selected values font
private void button3_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
NameList.Font = fontDialog1.Font;
}
}
}
}

Output:

After adding 3 names:

C# 中的列表框

Deleting selected element:

C# 中的列表框

C# 中的列表框

Change the font of the values:

C# 中的列表框

C# 中的列表框

C# 中的列表框

C# 中的列表框

Conclusion

C# List box is used to add multiple elements to perform any specific operation. List Boxes are used to select a single value or multiple values at a time. In C# List Box can be created using Design-Time and Run-Time methods.

以上是C# 中的列表框的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

熱門話題

Laravel 教程
1601
29
PHP教程
1502
276
c#多線程和異步的區(qū)別 c#多線程和異步的區(qū)別 Apr 03, 2025 pm 02:57 PM

多線程和異步的區(qū)別在于,多線程同時(shí)執(zhí)行多個(gè)線程,而異步在不阻塞當(dāng)前線程的情況下執(zhí)行操作。多線程用于計(jì)算密集型任務(wù),而異步用于用戶交互操作。多線程的優(yōu)勢是提高計(jì)算性能,異步的優(yōu)勢是不阻塞 UI 線程。選擇多線程還是異步取決于任務(wù)性質(zhì):計(jì)算密集型任務(wù)使用多線程,與外部資源交互且需要保持 UI 響應(yīng)的任務(wù)使用異步。

C#與C:歷史,進(jìn)化和未來前景 C#與C:歷史,進(jìn)化和未來前景 Apr 19, 2025 am 12:07 AM

C#和C 的歷史與演變各有特色,未來前景也不同。1.C 由BjarneStroustrup在1983年發(fā)明,旨在將面向?qū)ο缶幊桃隒語言,其演變歷程包括多次標(biāo)準(zhǔn)化,如C 11引入auto關(guān)鍵字和lambda表達(dá)式,C 20引入概念和協(xié)程,未來將專注于性能和系統(tǒng)級編程。2.C#由微軟在2000年發(fā)布,結(jié)合C 和Java的優(yōu)點(diǎn),其演變注重簡潔性和生產(chǎn)力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注于開發(fā)者的生產(chǎn)力和云計(jì)算。

xml怎么轉(zhuǎn)換成json xml怎么轉(zhuǎn)換成json Apr 03, 2025 am 09:09 AM

將 XML 轉(zhuǎn)換為 JSON 的方法包括:使用編程語言(如 Python、Java、C#)編寫腳本或程序進(jìn)行轉(zhuǎn)換;使用在線工具(如 XML 轉(zhuǎn)換為 JSON、Gojko's XML 轉(zhuǎn)換器、XML 在線工具)粘貼或上傳 XML 數(shù)據(jù)并選擇 JSON 格式輸出;使用 XML 到 JSON 轉(zhuǎn)換器(如 Oxygen XML Editor、Stylus Studio、Altova XMLSpy)執(zhí)行轉(zhuǎn)換任務(wù);使用 XSLT 樣式表將 XML 轉(zhuǎn)換為 JSON;使用數(shù)據(jù)集成工具(如 Informatic

c#多線程編程是什么  c#多線程編程用處 c#多線程編程是什么 c#多線程編程用處 Apr 03, 2025 pm 02:45 PM

C# 多線程編程是一種讓程序同時(shí)執(zhí)行多項(xiàng)任務(wù)的技術(shù),它可以通過提升性能、提高響應(yīng)能力和實(shí)現(xiàn)并行處理來提高程序效率。雖然 Thread 類提供了直接創(chuàng)建線程的方法,但 Task 和 async/await 等高級工具可以提供更安全的異步操作和更簡潔的代碼結(jié)構(gòu)。多線程編程中常見的難題包括死鎖、競態(tài)條件和資源泄漏,需要仔細(xì)設(shè)計(jì)線程模型和使用適當(dāng)?shù)耐綑C(jī)制來避免這些問題。

C#.NET:使用.NET生態(tài)系統(tǒng)構(gòu)建應(yīng)用程序 C#.NET:使用.NET生態(tài)系統(tǒng)構(gòu)建應(yīng)用程序 Apr 27, 2025 am 12:12 AM

如何利用.NET構(gòu)建應(yīng)用?使用.NET構(gòu)建應(yīng)用可以通過以下步驟實(shí)現(xiàn):1)了解.NET基礎(chǔ)知識,包括C#語言和跨平臺開發(fā)支持;2)學(xué)習(xí)核心概念,如.NET生態(tài)系統(tǒng)的組件和工作原理;3)掌握基本和高級用法,從簡單控制臺應(yīng)用到復(fù)雜的WebAPI和數(shù)據(jù)庫操作;4)熟悉常見錯(cuò)誤與調(diào)試技巧,如配置和數(shù)據(jù)庫連接問題;5)應(yīng)用性能優(yōu)化與最佳實(shí)踐,如異步編程和緩存。

從網(wǎng)絡(luò)到桌面:C#.NET的多功能性 從網(wǎng)絡(luò)到桌面:C#.NET的多功能性 Apr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

.NET框架與C#:解碼術(shù)語 .NET框架與C#:解碼術(shù)語 Apr 21, 2025 am 12:05 AM

.NETFramework是一個(gè)軟件框架,C#是一種編程語言。1..NETFramework提供庫和服務(wù),支持桌面、Web和移動應(yīng)用開發(fā)。2.C#設(shè)計(jì)用于.NETFramework,支持現(xiàn)代編程功能。3..NETFramework通過CLR管理代碼執(zhí)行,C#代碼編譯成IL后由CLR運(yùn)行。4.使用.NETFramework可快速開發(fā)應(yīng)用,C#提供如LINQ的高級功能。5.常見錯(cuò)誤包括類型轉(zhuǎn)換和異步編程死鎖,調(diào)試需用VisualStudio工具。

c#多線程的好處有哪些 c#多線程的好處有哪些 Apr 03, 2025 pm 02:51 PM

多線程的好處在于能提升性能和資源利用率,尤其適用于處理大量數(shù)據(jù)或執(zhí)行耗時(shí)操作。它允許同時(shí)執(zhí)行多個(gè)任務(wù),提高效率。然而,線程過多會導(dǎo)致性能下降,因此需要根據(jù) CPU 核心數(shù)和任務(wù)特性謹(jǐn)慎選擇線程數(shù)。另外,多線程編程涉及死鎖和競態(tài)條件等挑戰(zhàn),需要使用同步機(jī)制解決,需要具備扎實(shí)的并發(fā)編程知識,權(quán)衡利弊并謹(jǐn)慎使用。

See all articles