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

时间:2012-08-09 16:38:25

标签: c# string text combobox null

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

我在Windows窗体上有一个组合框。组合框的数据从Access数据库填充。我设置的相关属性是 - AutoCompleteMode = Append; AutoCompleteSource = ListItems; DropDownStyle = DropDown。用户必须能够输入组合框并自动完成,因此DropDownList的DropDownStyle将无法工作。我没有使用默认下拉箭头,而是使用动态PictureBox替换它。单击PictureBox或触发Enter事件会将组合框的DropDowned属性设置为true。

目前,用户可以选择正常的项目或输入项目,然后按Enter键或输入项目并离开该字段等....在所有这些不同类型的交互过程中,我能够确定组合框中的正确值是。我有一些触发器来确保SelectedValue和显示的Text始终保持同步。

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

视觉:

Selected_ 文字

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

Selected_Text

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

以下是其中一个ComboBox的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;
        }
    }

我还发现,当DropDowned属性= true并且用户输入了某些内容然后按“Enter”时,ComboBox.Text始终为空字符串。如果DropDowned属性= false,则不是这种情况。发生这种情况时,会返回正确的字符串。

我甚至试过让程序选择comobox中的所有文本;但是,为SelectionLength赋予一个大于ComboBox.Text.Length的值似乎不起作用。我也尝试过引用SelectedValue;但是,SelectedValue为空。

对于所有密集目的,应用程序确信组合框中存在空字符串。

如何检索实际字符串?


如果这有助于我获得以下事件的代码:Click,DataSourceChanged,DropDown,DropDownClosed,Enter,KeyDown,Leave和Validated。

2 个答案:

答案 0 :(得分:1)

这可能是一个错误:Wrong SelectedItem in DropDownClosed event handler of ComboBox when pressing Tab to leave an opened dropdown

我知道这与你的情况不一样。检查变通方法选项卡,看看那里发布的代码是否可以帮助您解决问题。这可能只是使用正确的事件。

我对某些Windows窗体控件的事件订单和所选属性的体验不太理想。

答案 1 :(得分:0)

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

要使用此代码,您需要在表单上使用名为cmbxTest的ComboBox。您还需要添加适当的事件处理程序。假设您的表单名称为frmMain,如下所示,在fmrMain.Designer.cs文件中,添加此代码(请注意您还需要其他项目,但这些是需要添加的 new 项目到了comboBox,cmbxTest,它应该已经在你的测试表格上,frmMain):

this.cmbxTest.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Append;
this.cmbxTest.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.cmbxTest.DropDownClosed += new System.EventHandler(this.cmbxTest_DropDownClosed);
this.cmbxTest.KeyUp += new System.Windows.Forms.KeyEventHandler(this.ComboBoxKeyUp);
this.cmbxTest.Text = "Test";  // Used in the below example - The default displayed text

在表单的类文件(本例中为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);
    }
}
相关问题