Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# listbox

作者:寂寞算什么   发布日期:2025-03-28   浏览:40

using System;
using System.Windows.Forms;

namespace ListBoxExample
{
    public class ListBoxDemo : Form
    {
        private ListBox listBox1;

        public ListBoxDemo()
        {
            // 初始化 ListBox 控件
            listBox1 = new ListBox();
            listBox1.Location = new System.Drawing.Point(20, 20);
            listBox1.Size = new System.Drawing.Size(200, 100);

            // 添加一些示例项到 ListBox
            listBox1.Items.Add("Item 1");
            listBox1.Items.Add("Item 2");
            listBox1.Items.Add("Item 3");

            // 设置 ListBox 的选择模式为多选
            listBox1.SelectionMode = SelectionMode.MultiExtended;

            // 将 ListBox 添加到窗体中
            this.Controls.Add(listBox1);

            // 设置窗体的属性
            this.Text = "ListBox Example";
            this.Size = new System.Drawing.Size(300, 200);
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new ListBoxDemo());
        }
    }
}

解释说明:

  1. 命名空间和类:代码使用了 SystemSystem.Windows.Forms 命名空间,并定义了一个名为 ListBoxDemo 的类,该类继承自 Form
  2. 构造函数:在 ListBoxDemo 类的构造函数中,初始化了一个 ListBox 控件,并设置了它的位置、大小和一些示例项。
  3. 添加项:通过 listBox1.Items.Add() 方法向 ListBox 中添加了三个示例项。
  4. 选择模式:将 ListBox 的选择模式设置为 SelectionMode.MultiExtended,允许用户进行多选。
  5. 控件添加:将 ListBox 控件添加到窗体中。
  6. 窗体属性:设置了窗体的标题和大小。
  7. 主程序入口:在 Main 方法中,启动应用程序并显示窗体。

上一篇:c# modbus tcp

下一篇:c#上位机开发

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c# rtsp

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站