C# ComboBox.Text=空字符串,而不是实际显示的字符串

C# ComboBox.Text=空字符串,而不是实际显示的字符串,c#,string,text,combobox,null,C#,String,Text,Combobox,Null,免责声明-我只使用C#大约一周了,所以希望这不是一个n00b问题。我确实环顾了四周,但找不到有效的解决方案,包括线程的结果 我在Windows窗体上有一个组合框。组合框的数据由Access数据库填充。我设置的相关属性是-AutoCompleteMode=Append;AutoCompleteSource=列表项;DropDownStyle=下拉菜单。用户必须能够在combobox中键入,并且它会自动完成,因此DropDownList的DropDownStyle将不起作用。我没有使用默认的下拉箭头

免责声明-我只使用C#大约一周了,所以希望这不是一个n00b问题。我确实环顾了四周,但找不到有效的解决方案,包括线程的结果

我在Windows窗体上有一个组合框。组合框的数据由Access数据库填充。我设置的相关属性是-AutoCompleteMode=Append;AutoCompleteSource=列表项;DropDownStyle=下拉菜单。用户必须能够在combobox中键入,并且它会自动完成,因此DropDownList的DropDownStyle将不起作用。我没有使用默认的下拉箭头,而是用一个动态的PictureBox来代替它。单击PictureBox或触发Enter事件会将组合框的DropDowned属性设置为true

按照目前的情况,用户可以很好地选择项目或键入项目,然后按enter键或键入项目并离开字段,等等。。。。在所有这些不同类型的交互过程中,我能够确定combobox中的正确值。我有一些触发器来确保SelectedValue和显示的文本始终同步

在我能想到的每一种可能的交互作用下,我都能得到正确的值,除了一个。如果用户开始键入字符串(dropdown属性为true),并点击右箭头键使字符串自动完成,则组合框中的字符串始终为空字符串

视觉:

所选文本

上面字符串中的粗体文本是组合框中突出显示的文本。如果用户点击右箭头键,使组合框中的文本看起来像

选定的文本

(请注意,此时DropDowned仍然为true)ComboBox.Text值始终为“”

下面是一个组合框的DropDownClosed事件的代码,这是用户按enter键后触发的第一个事件

private void cmbxYear_DropDownClosed(object sender, EventArgs e)
    {
        try
        {
            if (!cmbxYear.Text.Equals(cmbxYear.SelectedValue.ToString()))
            {
                if (!bUpdated & !bErrorFound)
                {
                    validateData(cmbxYear, clrYear, false, imgFilter1, imgNoFilter1);
                    updateTable();
                }
            }
            imgFilter1.Visible = false;
            imgNoFilter1.Visible = true;
        }
        catch
        {
            imgNoFilter1.Visible = false;
            imgFilter1.Visible = true;
        }
    }
我还发现,当下拉属性=true且用户输入了某个内容,然后按“Enter”时,ComboBox.Text始终是空字符串。如果DropDowned属性为false,则情况并非如此。出现这种情况时,将返回正确的字符串

我甚至尝试让程序选择文本框中的所有文本;但是,为SelectionLength指定大于ComboBox.Text.Length的值似乎不起作用。我还尝试引用SelectedValue;但是,SelectedValue为空

出于所有目的,应用程序确信combobox中存在空字符串

如何检索实际字符串


在这种情况下,我有以下事件的代码:单击、数据源更改、下拉、下拉关闭、输入、按键、离开和验证。

这可能是一个错误:

我知道这和你的情况不一样。检查“变通方法”选项卡,查看张贴在那里的代码是否有助于您解决问题。这可能只是使用正确事件的问题


我在一些Windows窗体控件的事件顺序和选定属性方面的经验不太好。

我成功地解决了这个明显的错误。下面是我的解决方法;希望这段代码能帮助其他人。注意:您可能需要添加其他事件处理程序来微调所有Comobox的用户交互,但这将适用于我在问题中描述的问题

要使用此代码,您需要在表单上有一个名为cmbxTest的组合框。您还需要添加适当的事件处理程序。假设您的表单名为frmMain,如下所示,在fmrMain.Designer.cs文件中添加此代码(注意,您还需要其他项目,但这些是需要添加到组合框cmbxTest的新项目,该组合框应该已经在您的测试表单frmMain上):

在表单的类文件(本例中为frmMain.cs)中,将其设置为如下所示:

public partial class frmMain : Form
{
    // Intial Declerations and initializations
    Boolean bReady = false;       // Secondary trigger for DataGridView selection
    string clrTest = "Test";      // Default display text - Clear filter text; Used to ignore the setting if this text is visible
    string currentText;           // Comboboxes' currently displayed text

    // Form execution
    public frmMain()
    {
        InitializeComponent();
    }

    // Some code...

    //
    // ComboBoxes on KeyPress event
    //      - Used as a workaround for MS ComboBoxes not updating their text correctly
    //      - Shared across all ComboBoxes (as applied by the individual controls' event handlers)
    //
    private void ComboBoxKeyUp(object sender, KeyEventArgs e)
    {
        ComboBox cmb = (ComboBox)sender;
        currentText = cmb.Text;
    }

    // Any trigger you want that requires the ComboBoxes text
    private void cmbxTest_DropDownClosed(object sender, EventArgs e)
    {
        if (!cmbxTest.Text.Equals(clrTest))
        {
            cmbxTest.Text = currentText;
        }
        // Do any other code that requires the cmbxTest.Text
        Console.WriteLine(cmbxTest.Text);
    }
}

你能展示一些代码吗?您似乎已经写了很多东西来解释什么似乎是一个简单的解决方案。在哪个事件中,您正在检索SelectedText?完全有可能SelectedText尚未填充(因为下拉列表为true)。可能控件在下拉操作完成之前不会建立该值?我一直尝试在DropDownClosed事件中检索文本(只是添加了代码)。您使用的是文本还是SelectedText属性?你在链接的帖子中尝试了最高评分的答案吗?(访问SelectedItem属性)值得注意的是,如果在组合框中键入内容,然后按tab键,则所有内容都会相应更新。我认为问题在于SelectedValueChanged事件没有触发,并且两者之间的同步丢失,但我不知道如何解决这个问题,因为即使是Microsoft的答案也是使用SelectedItem属性……这不是我遇到的确切错误,但它很接近。我认为他们的变通办法行不通,但我自己在努力。如果我有工作,我会发回一次。
public partial class frmMain : Form
{
    // Intial Declerations and initializations
    Boolean bReady = false;       // Secondary trigger for DataGridView selection
    string clrTest = "Test";      // Default display text - Clear filter text; Used to ignore the setting if this text is visible
    string currentText;           // Comboboxes' currently displayed text

    // Form execution
    public frmMain()
    {
        InitializeComponent();
    }

    // Some code...

    //
    // ComboBoxes on KeyPress event
    //      - Used as a workaround for MS ComboBoxes not updating their text correctly
    //      - Shared across all ComboBoxes (as applied by the individual controls' event handlers)
    //
    private void ComboBoxKeyUp(object sender, KeyEventArgs e)
    {
        ComboBox cmb = (ComboBox)sender;
        currentText = cmb.Text;
    }

    // Any trigger you want that requires the ComboBoxes text
    private void cmbxTest_DropDownClosed(object sender, EventArgs e)
    {
        if (!cmbxTest.Text.Equals(clrTest))
        {
            cmbxTest.Text = currentText;
        }
        // Do any other code that requires the cmbxTest.Text
        Console.WriteLine(cmbxTest.Text);
    }
}