【问题标题】:C# Textbox: Bind object to suggestion listC# 文本框:将对象绑定到建议列表
【发布时间】:2018-04-28 11:10:49
【问题描述】:

我目前正在从事一个需要管理城市及其邮政编码的项目。因此,我创建了休闲对象:

class Place
{
   Guid Id { get; set; }
   string Name { get; set; }
   int ZipCode { get; set; }

   ... further fields
}

并用Place 的编号填写List<Place>。现在我有一个Textbox,我可以在其中输入城市名称或其邮政编码。在这两种情况下,我都想获得一个与输入的输入相匹配的建议列表,如下所示

示例:输入 =“1234”或“城市”

  • 12341 同城
  • 12342 城市二号
  • 12343 三城
  • ...

如果我从这个建议列表中选择一个项目,我想获得相关的Place 作为返回值。如何在 C# 中使用 WindowsFormsApplication 实现此功能?

【问题讨论】:

标签: c# winforms textbox autosuggest


【解决方案1】:

您所问的不是一个简单或单一的问题。我认为您可以在用户输入文本的文本框下方有一个隐藏的列表框。

现在TextBox 有一个名为OnTextChanged 的事件。每次文本发生更改时都会触发此事件。这意味着当用户在相应的 TextBox 中添加或删除一个字符时,会触发此事件。

您可以使用此事件过滤列表以显示在您的 ListBox 中。

类似的东西

//Imagine the TextBox ID as CityZipTextBox and ListBox ID as CityZipSuggestionsListBox
protected override void OnTextChanged(EventArgs e)
{
    string txtCityZip = CityZipTextBox.Text;
    List<Place> suggestedPlaces = filterCode;//Code to filter the full list using the TextBox content
    CityZipSuggestionsListBox.DataSource = suggestedPlaces;
 }

这有帮助吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多