簡體   English   中英

當組合框綁定到數據源時使用ComboBox.Text

[英]Using ComboBox.Text when combobox is tied to DataSource

我們有一個項目,該項目設置組合框的數據源,但允許用戶從此列表中選擇某項,或者鍵入列表中未包含的項。 基本上,有一個包含街道的Geobase,但是不需要用戶從列表中選擇街道。 ComboBox.DropDownStyle設置為DropDown。

如果用戶編輯的記錄在地理數據庫中(因此不在ComboBox.DataSource中)包含一條不在街道中的街道,那么我們將無法正確填充表單。

這是我們的問題的簡化形式:

    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類的內容如下:

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 = streets frm.Show() ,而第二次調用frm.Show()

為什么顯示表單時會觸發此事件,這就是為什么我的手動設置選擇被刪除的原因? 我應該如何糾正這種行為?

另外,我是否以為我應該能夠以這種方式使用組合框是不對的?

提前致謝。

您應該能夠將SelectedIndex設置為-1,以便在列表中沒有選擇任何項目。 獲得“密歇根大道”沒有任何問題。 用此方法顯示在組合框中。

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

另外,您可以顯示表單,然后設置文本。 除非那是一個問題,否則用戶可能不會注意到。

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

我想不出我們在winforms代碼中在構造函數中設置數據源的任何實例,因此我無法真正解決您為什么看到自己所看到的事情。

但是,我可以告訴您我們如何一直處理這種類型的問題,以避免不可避免的計時問題:我們在表單上創建一個方法,該方法接受我們要在表單中使用的參數,並讓表單顯示本身,然后設置字段值。

幾年前,當我們與第三方控件打交道時就開始使用此模式,這些控件會自行重置或處於無效狀態,直到實際可見該窗體為止。

如果該表單由於某種原因決定不顯示該模式,則該模式也非常有用(存在另一個打開了相同類型的表單,訪問該表單使用的資源時出錯,用戶沒有適當的權限)等)。

該模式對於從模式對話框返回比標准內置模式對話框值更有用的值也很有用。

對於您而言,我們將按照以下方式重寫您的表單:

public partial class ComboBoxTrialForm : Form
{
    public ComboBoxTrialForm()
    {
        InitializeComponent();
    }

    public void ShowForm(string comboBoxValue)
    {
        this.Show();

        List<string> streets = new List<string>() { "Main St.", "First St.", "Second St." };
        comboBox1.DataSource = streets;

        SetComboTextValue(comboBoxValue);
    }

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

您的button1_click事件將變為:

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();

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

為什么不使用WCF Windows服務(而不是組合框)?當用戶在文本框中鍵入內容時,該服務就會被調用。 這樣,您可以使用文本框,但仍使用戶能夠從數據庫中選擇類似內容

這應該為您提供Windows服務的代碼

http://wcftutorial.net/WCF-Windows-Service-Hosting.aspx

http://msdn.microsoft.com/en-us/library/bb332338.aspx

您可以從keyup事件中調用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM