我正在尝试向winforms combobox添加一个类似于包含的自动完成功能。我从this thread的Hovhannes Hakobyan的想法开始。我不得不稍微调整一下,因为自动补全不知道到哪里去搜索。让我从描述我的设置开始:
我有一个'Part‘类,combobox将显示它的'Name’属性(DisplayMember)。‘'Name’也是自动完成应该搜索包含给定字符串的项目的位置:
public class Part
{
public int PartId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
在窗体的代码隐藏中,我创建了新的AutoCompleteBehavior对象,它将为我处理所有事件,并传递组合框和对象列表。虽然我在这里引用的是'Part‘类,但我试图构建通用的解决方案,所以我尽可能地使用泛型
new AutoCompleteBehavior<Part>(this.cmbPart, parts.Items);
cmbPart.DisplayMember = "Name";
cmbPart.ValueMember = "PartId";
下面是完整的AutoCompleteBehavior类:
public class AutoCompleteBehavior<T>
{
private readonly ComboBox comboBox;
private string previousSearchterm;
private T[] originalList;
public AutoCompleteBehavior(ComboBox comboBox, List<T>Items)
{
this.comboBox = comboBox;
this.comboBox.AutoCompleteMode = AutoCompleteMode.Suggest; // crucial otherwise exceptions occur when the user types in text which is not found in the autocompletion list
this.comboBox.TextChanged += this.OnTextChanged;
this.comboBox.KeyPress += this.OnKeyPress;
this.comboBox.SelectionChangeCommitted += this.OnSelectionChangeCommitted;
object[] items = Items.Cast<object>().ToArray();
this.comboBox.DataSource = null;
this.comboBox.Items.AddRange(items);
}
private void OnSelectionChangeCommitted(object sender, EventArgs e)
{
if (this.comboBox.SelectedItem == null)
{
return;
}
var sel = this.comboBox.SelectedItem;
this.ResetCompletionList();
comboBox.SelectedItem = sel;
}
private void OnTextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.comboBox.Text) || !this.comboBox.Visible || !this.comboBox.Enabled)
{
return;
}
this.ResetCompletionList();
}
private void OnKeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r' || e.KeyChar == '\n')
{
e.Handled = true;
if (this.comboBox.SelectedIndex == -1 && this.comboBox.Items.Count > 0
&& this.comboBox.Items[0].ToString().ToLowerInvariant().StartsWith(this.comboBox.Text.ToLowerInvariant()))
{
this.comboBox.Text = this.comboBox.Items[0].ToString();
}
this.comboBox.DroppedDown = false;
// Guardclause when detecting any enter keypresses to avoid a glitch which was selecting an item by means of down arrow key followed by enter to wipe out the text within
return;
}
// Its crucial that we use begininvoke because we need the changes to sink into the textfield Omitting begininvoke would cause the searchterm to lag behind by one character That is the last character that got typed in
this.comboBox.BeginInvoke(new Action(this.ReevaluateCompletionList));
}
private void ResetCompletionList()
{
this.previousSearchterm = null;
try
{
this.comboBox.SuspendLayout();
if (this.originalList == null)
{
this.originalList = this.comboBox.Items.Cast<T>().ToArray();
}
if (this.comboBox.Items.Count == this.originalList.Length)
{
return;
}
while (this.comboBox.Items.Count > 0)
{
this.comboBox.Items.RemoveAt(0);
}
this.comboBox.Items.AddRange(this.originalList.Cast<object>().ToArray());
}
finally
{
this.comboBox.ResumeLayout(true);
}
}
private void ReevaluateCompletionList()
{
var currentSearchterm = this.comboBox.Text.ToLowerInvariant();
if (currentSearchterm == this.previousSearchterm)
{
return;
}
this.previousSearchterm = currentSearchterm;
try
{
this.comboBox.SuspendLayout();
if (this.originalList == null)
{
this.originalList = this.comboBox.Items.Cast<T>().ToArray(); // backup original list
}
T[] newList;
if (string.IsNullOrEmpty(currentSearchterm))
{
if (this.comboBox.Items.Count == this.originalList.Length)
{
return;
}
newList = this.originalList;
}
else
{
newList = this.originalList.Where($"{comboBox.DisplayMember}.Contains(@0)", currentSearchterm).ToArray();
//newList = this.originalList.Where(x => x.ToString().ToLowerInvariant().Contains(currentSearchterm)).ToArray();
}
try
{
// clear list by loop through it otherwise the cursor would move to the beginning of the textbox
while (this.comboBox.Items.Count > 0)
{
this.comboBox.Items.RemoveAt(0);
}
}
catch
{
try
{
this.comboBox.Items.Clear();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
this.comboBox.Items.AddRange(newList.Cast<object>().ToArray()); // reset list
}
finally
{
if (currentSearchterm.Length >= 1 && !this.comboBox.DroppedDown)
{
this.comboBox.DroppedDown = true; // if the current searchterm is empty we leave the dropdown list to whatever state it already had
Cursor.Current = Cursors.Default; // workaround for the fact the cursor disappears due to droppeddown=true This is a known bu.g plaguing combobox which microsoft denies to fix for years now
this.comboBox.Text = currentSearchterm; // Another workaround for a glitch which causes all text to be selected when there is a matching entry which starts with the exact text being typed in
this.comboBox.Select(currentSearchterm.Length, 0);
}
this.comboBox.ResumeLayout(true);
}
}
}
现在,自动补全在某种程度上是有效的-它寻找包含给定字符串的项,并且做得很好。问题是,由于某些原因,在combobox中选择了一个项目后,combobox的SelectedValue==null
和SelectedText=""
。同时,SelectedItem
包含适当的'Part‘对象,SelectedIndex
也具有适当的值...
不幸的是,当我在填写表单时将combobox.SelectedValue设置为某个值时,组合框中没有选中任何项。此外,当我尝试获取combobox.SelectedValue时,它也显示为null (即使选择了一个项)。我甚至尝试手动设置基于SelectedItem的SelectedValue,但是我不能设置它(它仍然是空的):
private void OnSelectionChangeCommitted(object sender, EventArgs e)
{
if (this.comboBox.SelectedItem == null)
{
return;
}
var sel = this.comboBox.SelectedItem;
this.ResetCompletionList();
comboBox.SelectedItem = sel;
string valueName = comboBox.ValueMember;
comboBox.ValueMember = "";
comboBox.SelectedValue = typeof(T).GetProperty(valueName).GetValue(sel);
}
我想可能是因为我没有使用combobox.DataSource属性,所以我不能设置/获取SelectedValue/SelectedText,但我在这里可能是错的。欢迎任何想法!:)
发布于 2019-05-24 08:33:11
将组合框样式设置为ComboBoxStyle.DropDownList
始终将""
(空字符串)作为SelectedText
(reference source)返回
public string SelectedText
{
get
{
if (DropDownStyle == ComboBoxStyle.DropDownList)
return "";
return Text.Substring(SelectionStart, SelectionLength);
}
{
// see link
}
}
SelectedValue
是从ListControl
继承的成员,需要对数据进行(reference source)管理。
public object SelectedValue {
get
{
if (SelectedIndex != -1 && dataManager != null )
{
object currentItem = dataManager[SelectedIndex];
object filteredItem = FilterItemOnProperty(currentItem, valueMember.BindingField);
return filteredItem;
}
return null;
}
set
{
// see link
}
发布于 2019-05-24 20:01:22
我设法使用扩展方法和反射让它工作。虽然我仍然希望找到更好的解决方案,但它工作得很好。我已经创建了扩展类:
using System.Linq.Dynamic;
namespace JDE_Scanner_Desktop.Static
{
static class Extensions
{
public static int GetSelectedValue<T>(this ComboBox combobox)
{
return (int)typeof(T).GetProperty(combobox.ValueMember).GetValue(combobox.SelectedItem);
}
public static void SetSelectedValue<T>(this ComboBox combobox, int? selectedValue)
{
if(selectedValue != null)
{
combobox.SelectedItem = combobox.Items.Cast<T>().Where(combobox.ValueMember + $"={selectedValue}").FirstOrDefault();
}
}
}
}
然后,我使用cmbPart.SetSelectedValue<Part>(this.PartId);
将项目设置为选中,并使用cmbPart.GetSelectedValue<Part>();
获取所选项目的SelectedValue。
当然,我对其他解决方案持开放态度!
https://stackoverflow.com/questions/56280648
复制相似问题