簡體   English   中英

如何在 comboBox.SelectedIndexChanged 事件中更改 comboBox.Text?

[英]How do I Change comboBox.Text inside a comboBox.SelectedIndexChanged event?

代碼示例:

private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    if(some condition)
    {
        comboBox.Text = "new string"
    }
}

我的問題是組合框文本始終顯示所選索引的字符串值而不是新字符串。 這是一種方法嗎?

這段代碼應該可以工作...

public Form1()
{
    InitializeComponent();

    comboBox1.Items.AddRange(new String[] { "Item1", "Item2", "Item3" });
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    String text = "You selected: " + comboBox1.Text;

    BeginInvoke(new Action(() => comboBox1.Text = text));
}

希望它有幫助... :)

設置Text屬性時,您應該將SelectedIndex屬性重置為 -1。

將更改代碼移到組合框事件之外:

if(some condition)
{
    BeginInvoke(new Action(() => comboBox.Text = "new string"));
}

如果你能准確地解釋你想要做什么,也許會有所幫助。 我發現 SelectionChangeCommitted 事件對於您描述的目的比 SelectedIndexChanged 有用得多。 除此之外,還可以從 SelectionChangeCommitted 再次更改所選索引(例如,如果用戶的選擇無效)。 此外,更改代碼中的索引會再次觸發 SelectedIndexChanged,而 SelectionChangeCommitted 僅在響應用戶操作時觸發。

簡而言之,.NET 正在努力防止可能發生的無限循環。 當 Text 屬性發生更改時,.NET 將嘗試將該新值與當前項匹配並為您更改索引,從而再次觸發 SelectedIndexChanged 事件。

來這里尋找Delegates的VB實現的人可以參考下面的代碼

'Declares a delegate sub that takes no parameters
Delegate Sub ComboDelegate()

'Loads form and controls
Private Sub LoadForm(sender As System.Object, e As System.EventArgs) _
    Handles MyBase.Load
    ComboBox1.Items.Add("This is okay")
    ComboBox1.Items.Add("This is NOT okay")
    ResetComboBox()
End Sub

'Handles Selected Index Changed Event for combo Box
Private Sub ComboBoxSelectionChanged(sender As System.Object, e As System.EventArgs) _
    Handles ComboBox1.SelectedIndexChanged
    'if option 2 selected, reset control back to original
    If ComboBox1.SelectedIndex = 1 Then
        BeginInvoke(New ComboDelegate(AddressOf ResetComboBox))
    End If

End Sub

'Exits out of ComboBox selection and displays prompt text 
Private Sub ResetComboBox()
    With ComboBox1
        .SelectedIndex = -1
        .Text = "Select an option"
        .Focus()
    End With
End Sub

進一步閱讀:請參閱有關SelectedIndexChanged事件中更改組合框文本的這篇文章(我的),其中詳細介紹了為什么需要使用委托作為更改組合框文本的解決方法。

//100%有效

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
      BeginInvoke(new Action(() => ComboBox1.Text = "Cool!");
}

ComboBox 將綁定到您指定的任何對象集合,而不是簡單地具有您在 DropDownLists 中找到的文本/值組合。

您需要做的是進入 ComboBox 的 Items 集合,找到您要更新的項目,更新您綁定到 ComboBox 本身的 Text 字段的任何屬性,然后數據綁定應自動使用新項目刷新自身.

但是,我不是 100% 確定您實際上想要修改正在綁定的基礎數據對象,因此您可能想要創建一個 HashTable 或其他一些集合作為綁定到您的 ComboBox 的引用。

你應該使用:

BeginInvoke(new Action((text) => comboBox.Text = text), "要設置的新文本");

我正在尋找相同問題的解決方案。 但是通過處理 Format 事件來解決它:

cbWatchPath.Format += new System.Windows.Forms.ListControlConvertEventHandler(this.cbWatchPath_Format);

private void cbWatchPath_Format(object sender, ListControlConvertEventArgs e)
    {
        e.Value = "Your text here";
    }

暫無
暫無

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

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