【问题标题】:TextBox AutoCompleteStringCollection SuggestTextBox AutoCompleteStringCollection 建议
【发布时间】:2018-10-13 15:34:11
【问题描述】:

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

    }
}

结果如下:

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

应该是这样的:

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

欢迎您的想法!

【问题讨论】:

  • 我认为您需要为该功能实现自己的文本框,或者使用一些 3rd 方控件

标签: c# winforms autocomplete autosuggest


【解决方案1】:

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

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

  1. 自定义控件(第二个用户控件的容器 + 您向我们展示的文本框)
  2. 标签控件(将包含标签以显示标签文本和链接标签“x”以删除它) 它将一起创建单个单元自定义控件的可视化。您可以在其中输入(带有下拉建议),一旦您按下 ',' ,它将创建一个标签并将其存储在其中。

如下所示,

这是代码。

自定义控件默认有以下控制

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;

链接标签,llblRemove 将具有链接标签点击事件,该事件将引发自定义控件正在列出的事件。

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

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

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

查找Link

【讨论】:

  • 感谢您的帮助 - 我会回复我的进度并将接受此答案。
  • 很棒的想法/解决方案 - 谢谢!
  • @TobiasAlt 我很高兴能帮上忙。
【解决方案2】:

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

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;

您可以封装到您自己的控件/用户控件中,并在设计器中使用工具箱中的该事件。

【讨论】:

    猜你喜欢
    • 2011-08-12
    • 2011-06-08
    • 1970-01-01
    • 2011-03-12
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    相关资源
    最近更新 更多