1. 程式人生 > 實用技巧 >listBox修改 選中的內容時 能區分大小寫

listBox修改 選中的內容時 能區分大小寫

listBox 修改選中的值

int index = listBox1.SelectedIndex;
listBox1.Items[index] = "afx"; //如果當前選中的項也是AFX則這樣子修改不了內容 listBox會認為 afx和AFX是相同的內容


2020年7月25日 12:33:15


讓listBox命名選中的內容區分大小寫
/*
如果要修改的行內容為afx 而修改的內容為AFX

afx 和AFX 在ListBox中,它會認為它們是相同的內容,操作將取消
所以確保它能重新命名成功,如果相同先刪除選中的項,然後在選中的項插入要新增的內容,這樣就能區分大小寫了

*/

//假設當前要修改的行內容是AFX 然後將它修改為afx 
//會發現這樣做沒反應 listBox1.Items[listBox1.SelectedIndex] = "afx"; 
//換成下面的程式碼則正常操作
private void button1_Click(object sender,EventArgs e) {
    string s = "afx";
    int index = listBox1.SelectedIndex;
    listBox1.Items[index] = s; //這句竟然不是多餘的
    if (s == listBox1.Items[index].ToString()) {
        listBox1.Items.RemoveAt(index);
        listBox1.Items.Insert(index,s);
        listBox1.SelectedIndex = index;
    }
}