TextBox AutoCompleteStringCollection建议 [英] TextBox AutoCompleteStringCollection Suggest

查看:320
本文介绍了TextBox AutoCompleteStringCollection建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用C#创建了一个带有用于文本框的CustomSource的表单:

I have created a form in C# with a CustomSource for a textbox:

public partial class FormLookup : Form
{

    AutoCompleteStringCollection source = new AutoCompleteStringCollection();


    public FormLookup()
    {
        InitializeComponent();
        source.Add("Test");
        source.Add("TestItem");
        source.Add("TestValue");
        this.textBox1.AutoCompleteCustomSource = source;
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }
}

结果如下:

我要寻找的目的是从自动建议列表中选择多个值.当用户选择第一个值时,将使用诸如;"之类的分隔符.应该再次触发自动建议.

The purpose of what I am looking for is to select multiple values from the auto suggestion list. When the user selected the first value, a seperator like ';' should trigger the auto suggestion again.

它应该看起来像这样:

也许_TextChanged方法中有一些代码/想法? 在C#中是否可以像在pic2中那样突出显示所选的值?

Maybe some code/idea in the _TextChanged method? Is it possible in C# to highlight the selected value like in pic2?

欢迎您提出想法!

推荐答案

您需要为此要求创建自定义控件. 我已经创建了一个类似的.发布逻辑和代码-希望它可以帮助您获得基本的想法.

You need to create your custom control for this requirement. I have created similar one. posting logic and code - hope it may help you in getting basic Idea.

您需要创建两个用户控件.

You need to create two user controls.

  1. 自定义控件(第二个用户控件的容器+向您显示有问题的文本框)
  2. 标签控件(将包含显示标签文本的标签和链接标签"x"以将其删除) 它将一起创建单个单元自定义控件的可视化.您可以在其中键入(带有下拉建议),然后按,",它将创建一个标签并将其存储在其中.
  1. Custom Control (container for second user control + text box which you show us in question)
  2. Tag control (will contain label to show Text of tag and link label 'x' to remove it) Together it will create a visualization of single unit custom control. where you can type (with you drop down suggestion) and once you press ',' , it will create a tag and store it within.

如下所示,

这是代码.

默认情况下,自定义控件将具有以下cnntrols

Custom Control will have following cnntrols by default

private System.Windows.Forms.FlowLayoutPanel flayoutCustomControlContainer;
public System.Windows.Forms.TextBox textBox1; //Marking this public so it can be directly accessed by external code.

此处flayoutCustomControlContainer默认包含textBox1.

textBox1将具有Key Up事件,如下所示.

textBox1 will have Key Up event, which will be like below.

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Oemcomma) //we will perform this on every ',' press
            {
                flayoutCustomControlContainer.Controls.Remove(textBox1);

                TagControl tag = new TagControl(); //creating new Tag control
                tag.lblTagName.Text = textBox1.Text.TrimEnd(",".ToCharArray());
                tag.Remvoed += tag_Remvoed; //subscribing "Removed" event of Tag

                tag.Width = tag.lblTagName.Width + 50;
                tag.Height = tag.lblTagName.Height + 5;

                flayoutCustomControlContainer.Controls.Add(tag);

                textBox1.Text = "";

                flayoutCustomControlContainer.Controls.Add(textBox1);

                textBox1.Focus();
            }
        }


void tag_Remvoed(object sender, EventArgs e)
{
    this.flayoutCustomControlContainer.Controls.Remove((Control)sender);
    textBox1.Focus();
}

自定义控件的构造函数,如您所显示的,

Constructor of Custom Control, as you shown in question,

    public CustomControl()
    {
        InitializeComponent();

        source.Add("Test");
        source.Add("TestItem");
        source.Add("TestValue");
        this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
        this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
        this.textBox1.AutoCompleteCustomSource = source;
    }

现在,标签控制.

    private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
    public  System.Windows.Forms.Label lblTagName;
    private System.Windows.Forms.LinkLabel llblRemove;

链接标签,llblRemove将具有链接标签click事件,该事件将引发一个事件,该事件将在Custom控件中列出.

link label, llblRemove will have link lable click event which will raise an event, which Custom control is listing.

private void llblRemove_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
     if (Remvoed != null)
         Remvoed(this, EventArgs.Empty);
}

现在,在您要使用的任何地方使用此自定义控件.

Now, use this Custom Control anywhere you want to use.

我已经在GitHub上上传了工作示例项目.

I have uploaded working example project on GitHub.

找到> 链接

这篇关于TextBox AutoCompleteStringCollection建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆