我有一个带有DropDownStyle" DropDown"的组合框。 当我输入文本时,comboBox1.Text不是我在comboBox的文本区域中看到的文本,而且SelectedIndex + SelectedItem与我在特定场景中插入的文本不对应。
似乎selectedItem(+ SelectedIndex)对应于打开下拉列表时的文本,而不是关闭时的文本。
创建一个新表单,在其上放置一个组合框和一个按钮。 将事件处理程序添加到comboBox1事件中: - textchanged - 落下 - 下拉关闭 并将组合框的以下属性写入输出窗口(或文本框): - comboBox1.Text - comboBox1.SelectedIndex - comboBox1.SelectedItem - comboBox1.SelectedValue - comboBox1.FormattingEnabled 我为按钮的click事件添加了一个eventhandler,显示了comboBox1.Text。
创建一个非常简单的类(例如TestItems),它有3个属性(代码,描述和DescriptionUppercase)。 我通过直接使用TestItems类的实例填充组合框项目并首先将它们添加到List,然后将组合框的DataSource设置为List<>来测试它。它的工作方式有点不同,但两者都没有像我期望的那样工作。我会坚持列表。 我在列表中填写了100个测试项目(代码=" I0" ..." I99"和描述"项目1" ......项目100")。我将DisplayMember设置为"描述"和ValueMember到" Code"。
我插入三次文本"项目578"使用稍微不同的场景。
场景1:SelectedIndex为-1,SelectedItem为null。 - 只需写下"项目578"在组合框中(不要打开下拉列表) 从输出窗口记录:
TextChanged:ComboBox.Text = [ITEM 0] - ComboBox SelectedIndex = [0] - ComboBox SelectedItem = [I0 + Item 0] - ComboBox SelectedValue = [I0] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [I] - ComboBox SelectedIndex = [ - 1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [It] - ComboBox SelectedIndex = [ - 1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Ite] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item 5] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item 57] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item 578] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True]
场景2:selectedIndex = 0,SelectedItem是"项目0" - 打开下拉列表(单击组合框右侧的箭头) - 输入"项目578" - 关闭下拉列表 日志记录:
TextChanged:ComboBox.Text = [ITEM 0] - ComboBox SelectedIndex = [0] - ComboBox SelectedItem = [I0 + Item 0] - ComboBox SelectedValue = [I0] - Combobox FormattingEnabled = [True] 下拉列表打开:ComboBox.Text = [ITEM 0] - ComboBox SelectedIndex = [0] - ComboBox SelectedItem = [I0 + Item 0] - ComboBox SelectedValue = [I0] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [I] - ComboBox SelectedIndex = [ - 1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [It] - ComboBox SelectedIndex = [ - 1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Ite] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item 5] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item 57] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item 578] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] DropDownClosed:ComboBox.Text = [Item 578] - ComboBox SelectedIndex = [0] - ComboBox SelectedItem = [I0 + Item 0] - ComboBox SelectedValue = [I0] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item 578] - ComboBox SelectedIndex = [0] - ComboBox SelectedItem = [I0 + Item 0] - ComboBox SelectedValue = [I0] - Combobox FormattingEnabled = [True]
场景3:SelectedIndex为57,SelectedItem为"项目57" - 插入"项目57" - 打开下拉列表 - 添加" 8"到最后 日志记录:
TextChanged:ComboBox.Text = [ITEM 0] - ComboBox SelectedIndex = [0] - ComboBox SelectedItem = [I0 + Item 0] - ComboBox SelectedValue = [I0] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [I] - ComboBox SelectedIndex = [ - 1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [It] - ComboBox SelectedIndex = [ - 1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Ite] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item 5] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [Item 57] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] 下拉列表打开:ComboBox.Text = [Item 57] - ComboBox SelectedIndex = [-1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [ITEM 578] - ComboBox SelectedIndex = [ - 1] - ComboBox SelectedItem = [] - ComboBox SelectedValue = [] - Combobox FormattingEnabled = [True] DropDownClosed:ComboBox.Text = [ITEM 578] - ComboBox SelectedIndex = [57] - ComboBox SelectedItem = [I57 + Item 57] - ComboBox SelectedValue = [I57] - Combobox FormattingEnabled = [True] TextChanged:ComboBox.Text = [ITEM 578] - ComboBox SelectedIndex = [57] - ComboBox SelectedItem = [I57 + Item 57] - ComboBox SelectedValue = [I57] - Combobox FormattingEnabled = [True]
离开组合框(失去焦点)并不会改变SelectedItem。 另外一句话:如果你设置&#34; FormattingEnabled&#34;组合框的属性为false,则combobox1.Text将是selectedItem(displaymember)的文本。因此,在第二个场景中,您将在组合框&#34;项目57&#34;的textarea中看到,但是当您单击显示combobox.Text的按钮时,它将显示&#34;项目0&#34;。< / p>
重现此代码的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace TestCombobox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.comboBox1.FormattingEnabled = true;
comboBox1.DropDownClosed += ComboBox1_DropDownClosed;
comboBox1.DropDown += ComboBox1_DropDown;
comboBox1.TextChanged += ComboBox1_TextChanged;
comboBox1.DisplayMember = "DescriptionUpperCase";
comboBox1.ValueMember = "Code";
FillCombo();
}
private void ComboBox1_DropDown(object sender, EventArgs e)
{
ShowDebugInfoCombobox("Dropdown opened");
}
private void ComboBox1_TextChanged(object sender, EventArgs e)
{
ShowDebugInfoCombobox("TextChanged");
}
private void ShowDebugInfoCombobox(string Info)
{
Debug.WriteLine($"{Info}: ComboBox.Text = <{comboBox1.Text}> - ComboBox SelectedIndex = <{comboBox1.SelectedIndex}> - ComboBox SelectedItem = <{comboBox1.SelectedItem}> - ComboBox SelectedValue = <{comboBox1.SelectedValue}> - Combobox FormattingEnabled = <{comboBox1.FormattingEnabled}>");
}
private void ComboBox1_DropDownClosed(object sender, EventArgs e)
{
ShowDebugInfoCombobox("DropDownClosed");
}
private void FillCombo()
{
List<TestItems> aList = new List<TestItems>();
for (int cnt = 0; cnt < 100; cnt++)
{
aList.Add(new TestItems($"I{cnt.ToString()}", $"Item {cnt.ToString()}"));
//comboBox1.Items.Add(new TestItems($"I{cnt.ToString()}", $"Item {cnt.ToString()}"));
}
comboBox1.DataSource = aList;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.comboBox1?.SelectedItem?.ToString() + " Text = " + this.comboBox1.Text);
}
}
public class TestItems
{
public string Code { get; set; }
public string Description { get; set; }
public string DescriptionUpperCase { get { return Description.ToUpper(); } }
public TestItems(string code, string description)
{
this.Code = code;
this.Description = description;
}
public override string ToString()
{
return this.Code + " + " + this.Description;
}
}
}
对我来说看起来像个错误,不确定你的意见是什么。
亲切的问候, 巴特
答案 0 :(得分:1)
首先,这是一大堆额外的信息,这就是为什么你还没有得到答案。其次,如果要将文本“输入”到comboBox中,则应使用KeyPress事件并从中获取文本,并通过回车键进行过滤。第三,如果要将所选项目设置为null(如果它不在源数据中),则必须添加条件代码才能执行此操作。这是一个使用通用字符串列表的简单示例:
List<string> source = new List<string>();
public Form1()
{
InitializeComponent();
source.Add("Item 1");
source.Add("Item 2");
comboBox1.Items.AddRange(source.ToArray());
}
// Enter key detection as shown in https://stackoverflow.com/a/1226740/4034168
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
if (!source.Contains(comboBox1.Text))
{
comboBox1.SelectedIndex = -1;
comboBox1.SelectedItem = null;
}
}
}
此外,如果您想阻止用户输入任何键(但仍然从下拉项中选择),只需使用这样的TextUpdate事件(添加删除KeyPress事件,因为你不需要它):
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
if (!source.Contains(comboBox1.Text)) comboBox1.SelectedItem = null;
}