当组合框绑定到数据源时使用ComboBox.Text [英] Using ComboBox.Text when combobox is tied to DataSource

查看:83
本文介绍了当组合框绑定到数据源时使用ComboBox.Text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个项目,该项目设置组合框的数据源,但允许用户从此列表中选择某项,或者键入列表中未包含的项。基本上,有一个包含街道的Geobase,但是不需要用户从列表中选择街道。 ComboBox.DropDownStyle设置为DropDown。

We have a project that sets the DataSource of a combobox, but allows users to either select something from this list OR type in an item not contained in the list. Basically, there is a Geobase that contains streets, but the users are not required to choose a street from the list. The ComboBox.DropDownStyle is set to DropDown.

如果用户编辑的记录包含不在地理数据库中(因此不在ComboBox.DataSource中)的街道,

If a user edits a record that contains a street NOT in the geobase (and therefore not in the ComboBox.DataSource) we are having issues populating the form correctly.

这里是我们的问题的简化形式:

Here is a greatly simplified form of our problem:

    private void button1_Click(object sender, EventArgs e)
    {
        // Create a new form.  In the constructor the DataSource of the ComboBox is set with three items:
        //      Main St., First St., and Second St.
        ComboBoxTrialForm frm = new ComboBoxTrialForm();

        // Set comboBox1.Text equal to an item NOT in the datasource
        frm.SetComboTextValue("Michigan Ave.");

        // Show the form, and the comboBox has the first item in its datasource selected
        frm.Show();
    }

ComboBoxTrial类的内容如下:

The ComboBoxTrial Class goes something like this:

public partial class ComboBoxTrialForm : Form
{
    public ComboBoxTrialForm()
    {
        InitializeComponent();
        List<string> streets = new List<string>() { "Main St.", "First St.", "Second St." };
        comboBox1.DataSource = streets;
    }

    public void SetComboTextValue(string text)
    {
        comboBox1.Text = text;
    }
}

我设置了断点,发现comboBox1.Text确实确实设置正确。有趣的是,在此简化示例中,我发现BindingContextChanged事件实际上被触发了两次。一次在构造函数中调用 comboBox1.DataSource = street 时,第二次在构造函数中调用 frm.Show()

I set break points and found that comboBox1.Text does indeed get set correctly. Interestingly I found that the BindingContextChanged event actually gets fired twice in this simplified example. Once in the constructor when comboBox1.DataSource = streets is called, and a second time when frm.Show() is called.

为什么在显示表单时触发此事件,这就是为什么我的手动设置选择被删除的原因?我应该如何去纠正这种行为?

Why is this event firing when the form is shown, and is this why my manually set selection is getting removed? How should I go about correcting this behavior?

此外,我以为我应该能够以这种方式使用组合框是不对的吗?

Also, am I incorrect in thinking that I should be able to use the combobox in this way?

谢谢。

推荐答案

您应该可以将SelectedIndex设置为-1,这样在列表中没有选择任何项目。到达密歇根大街没有任何问题。

You should be able to set the SelectedIndex to -1 so that no item is selected in the list. I didn't have any problem getting "Michigan Ave." to display in the combo box with this method.

public Form1()
{
    InitializeComponent();
    comboBox1.DataSource = new List<string>() { "Main St.", "First St.", "Second St." };
    comboBox1.SelectedIndex = -1;
}

此外,您可以显示表单,然后设置文本。除非有问题,否则用户可能不会注意到。

Also, you could show the form, and then set the text. Unless that's a problem, the user probably won't notice.

    frm.Show();
    frm.SetComboTextValue("Michigan Ave.");

这篇关于当组合框绑定到数据源时使用ComboBox.Text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆