TextBox AutoCompleteStringCollection建议

问题描述 投票:2回答:2

我在C#中使用CustomSource为文本框创建了一个表单:

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)
    {

    }
}

结果如下:

enter image description here

我正在寻找的目的是从自动建议列表中选择多个值。当用户选择第一个值时,分隔符如“;”应该再次触发自动建议。

它应该如下所示:

enter image description here

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

欢迎您的想法!

c# winforms autocomplete autosuggest
2个回答
1
投票

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

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

  1. 自定义控件(第二个用户控件的容器+您向我们展示的文本框)
  2. 标签控件(将包含标签以显示标签文本和链接标签'x'以将其删除)它将共同创建单个单元自定义控件的可视化。你可以在哪里打字(随你下拉建议),一旦按',',它就会创建一个标签并存储在其中。

它看起来像下面,enter image description here

这是代码。

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

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活动,如下所示。

        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();
}

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

    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;

链接标签,llb删除将有链接标签点击事件,它将引发一个事件,自定义控件列出。

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

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

我在GitHub上传了工作示例项目。

找一个Link


0
投票

您需要创建自己的组件。结构可以是:

Panel (the main container with white background and border)
 |-> FlowLayoutPanel (the container for already added tags); Dock = Left
 |    |-> TagControl (you custom control for tag label)
 |    |-> ...        (more tags if required)
 |-> TextBox (the one with autocompletion with no borders); Dock = Fill;

您可以封装到您自己的Control / UserControl中,并在设计器中使用Toolbox中的该事件。

© www.soinside.com 2019 - 2024. All rights reserved.