吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1682|回复: 3
收起左侧

[其他转载] C# ComboBox 下拉框自动匹配笔记

[复制链接]
Cool_Breeze 发表于 2021-6-15 11:36
image.png

[C#] 纯文本查看 复制代码
using System;
using System.Drawing;
using System.Windows.Forms;

partial class ComboBoxExample : Form
{
    [STAThreadAttribute]
    public static void Main()
    {
        Application.Run(new ComboBoxExample());
    }
}

partial class ComboBoxExample
{
    private ComboBox GradeCbox;
    private Label TipsLbl;
    
    public ComboBoxExample()
    {
        this.Size = new Size(500, 380);
        this.StartPosition = FormStartPosition.CenterScreen;
        
        GradeCbox = new ComboBox();
        GradeCbox.Size = new Size(400, 60);
        GradeCbox.Location = new Point((500-400) / 2, (380-60) / 2);
        GradeCbox.Font = new Font("楷体", 12, FontStyle.Bold);
        GradeCbox.DataSource = new string[]
        {   
            "AutoCompleteMode.Append",
            "AutoCompleteMode.None",
            "AutoCompleteMode.Suggest",
            "AutoCompleteMode.SuggestAppend",
            "AutoCompleteSource.AllSystemSource",
            "AutoCompleteSource.AllUrl",
            "AutoCompleteSource.CustomSource",
            "AutoCompleteSource.FileSystem",
            "AutoCompleteSource.FileSystemDirectories",
            "AutoCompleteSource.HistoryList",
            "AutoCompleteSource.ListItems",
            "AutoCompleteSource.None",
            "AutoCompleteSource.RecentlyUsedList"
        };
        
        // 指定自动匹配数据源
        // GradeCbox.AutoCompleteSource = AutoCompleteSource.CustomSource;
        // GradeCbox.AutoCompleteCustomSource.AddRange(new string[] {"AAA", "BBB", "ABC", "BCA"});
        
        // 指定自己的数据项为自动匹配的数据源
        GradeCbox.AutoCompleteSource = AutoCompleteSource.ListItems;
        
        GradeCbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        
        GradeCbox.SelectedIndexChanged += new EventHandler(GradeCboxSelectIndexChanged);
        // GradeCbox.DropDownStyle = ComboBoxStyle.DropDownList;
        this.Controls.Add(GradeCbox);
        
        TipsLbl = new Label();
        TipsLbl.Text = "未选中";
        TipsLbl.Size = new Size(200, 60);
        TipsLbl.Location = new Point((500-200) / 2, ((380-60) / 2 - 60) / 2);
        TipsLbl.Font = new Font("楷体", 12, FontStyle.Bold);
        this.Controls.Add(TipsLbl);
        
    }
    
    private void GradeCboxSelectIndexChanged(object sender, EventArgs e)
    {
        if (GradeCbox.SelectedIndex != -1)
        {
            TipsLbl.Text = GradeCbox.Items[GradeCbox.SelectedIndex].ToString();
        }
    }
}

免费评分

参与人数 5吾爱币 +5 热心值 +4 收起 理由
牧濑伊莉雅 + 1 + 1 谢谢@Thanks!
kingaero + 1 我很赞同!
nanaqilin + 1 + 1 我很赞同!
jidesheng6 + 2 用心讨论,共获提升!
cjcmxc + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

kingaero 发表于 2021-6-15 14:10
学习一下
nappywu 发表于 2021-6-23 12:42
 楼主| Cool_Breeze 发表于 2021-6-23 18:04
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 16:55

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表