簡體   English   中英

選擇項目后替換組合框中的文本

[英]Replace text in ComboBox upon selecting an item

我有一個應包含路徑的可編輯ComboBox。 用戶可以從下拉列表中選擇幾個默認路徑(或輸入自己的默認路徑),例如%ProgramData%\\\\Microsoft\\\\Windows\\\\Start Menu\\\\Programs\\\\ (All Users) 下拉列表中的項目包含簡短說明,例如前面示例中的(All Users)部分。 選擇此類項目后,我想刪除此說明,以便在ComboBox中顯示有效路徑。

我目前將解釋從字符串中刪除,並嘗試通過設置ComboBox的Text屬性來更改文本。 但這不起作用,字符串已正確解析,但顯示的文本不會更新(與解釋中的下拉列表相同)。

private void combobox_TextChanged(object sender, EventArgs e) {
            //..             

               string destPath = combobox.GetItemText(combobox.SelectedItem);                  
               destPath = destPath.Replace("(All Users)", "");                   
               destPath.Trim();                   
               combobox.Text = destPath; 

            //..
}

我建議您創建PathEntry類以存儲Path及其Description

public sealed class PathEntry
{
    public string Path { get; private set; }
    public string Description { get; private set; }

    public PathEntry(string path)
      : this(path, path)
    {
    }

    public PathEntry(string path, string description)
    {
        this.Path = path;
        this.Description = description;
    }
}

然后創建BindingList<PathEntry>的實例,以存儲所有已知的路徑和描述。 稍后,您可以向其添加用戶定義的路徑。

private readonly BindingList<PathEntry> m_knownPaths =
  new BindingList<PathEntry>();

並如下更新窗體的構造函數:

public YourForm()
{
    InitializeComponent();

    m_knownPaths.Add(new PathEntry("%ProgramData%\\Microsoft\\Windows\\Start Menu\\Programs",
      "(All Users)"));
    // TODO: add other known paths here

    combobox.ValueMember = "Path";
    combobox.DisplayMember = "Description";
    combobox.DataSource = m_knownPaths;

}

private void combobox_DropDown(object sender, EventArgs e)
{
    combobox.DisplayMember = "Description";
}

private void combobox_DropDownClosed(object sender, EventArgs e)
{
    combobox.DisplayMember = "Path";
}

您可能想從MSDN了解更多關於DataSourceDisplayMemberValueMember信息。

通過使用BeginInvoke() 在類似的問題中找到了解決方案

使用尼古拉的解決方案,我的方法現在看起來像這樣:

private void combobox_SelectedIndexChanged(object sender, EventArgs e) {
            if (combobox.SelectedIndex != -1) {
                //Workaround (see below)
                var x = this.Handle;
                this.BeginInvoke((MethodInvoker)delegate { combobox.Text = combobox.SelectedValue.ToString(); });                  
            }
}

這是必需的解決方法,因為BeginInvoke要求加載或顯示控件,如果程序剛剛啟動,則不一定是這種情況。 這里得到的。

這可能不是最優雅的解決方案,但對於像我這樣的初學者來說,這是一個簡單的解決方案,他們不想在更了解它們之前先使用那些InvokeMethods。

布爾值是必需的,因為在為Items數組中的位置分配新的字符串(對象)時,將再次遞歸觸發處理程序。

因為我正在研究完全相同的問題,所以我現在才知道。

只是分享:)

    bool preventDoubleChange = false;
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (preventDoubleChange){
            preventDoubleChange = false;
            return;
        }

        preventDoubleChange = true;

        switch (comboBox1.SelectedIndex)
        {
            case 0:
                comboBox1.Items[0] = "ChangeToThisText";
                break;
            case 1:
                comboBox1.Items[1] = "ChangeToThisText";
                break;
            default:
                preventDoubleChange = false;
                break;
        }            
    }

...或者,如果您願意使用“標記”字段,則可以避免布爾值造成的麻煩。 我認為第二種變化也更干凈。

    public Form1()
    {
        InitializeComponent();

        comboBox1.Items.Add("Item One");                        //Index 0
        comboBox1.Items.Add("Item Two");                        //Index 1
        comboBox1.Items.Add("Item Three");                             //Index 2
        comboBox1.Items.Add("Item Four");                       //Index 3
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.Tag != null && (int)comboBox1.Tag == comboBox1.SelectedIndex)
            return;

        switch (comboBox1.SelectedIndex)
        {
            case 0:
                break;
            case 1:
                comboBox1.Tag = comboBox1.SelectedIndex;
                comboBox1.Items[comboBox1.SelectedIndex] = "changed item 2";
                break;
            case 2:
                comboBox1.Tag = comboBox1.SelectedIndex;
                comboBox1.Items[comboBox1.SelectedIndex] = "changed item 3";
                break;
            case 3:
                break;
            default:
                break;
        }
    }

雷德法肯

您不能以這種方式更改文本。 您需要做的是獲取selectedindex值,然后設置Text屬性。 像這樣:

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var index = DropDownList1.SelectedIndex;

        DropDownList1.Items[index].Text = "changed";
    }

`private void combobox_TextChanged(object sender,EventArgs e){// ..

           string destPath = combobox.SelectedItem.Text;                  
           destPath = destPath.Replace("(All Users)", "");                   
           destPath.Trim();                   
           combobox.SelectedItem.Text = destPath; 

        //..

}`

暫無
暫無

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

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