c# - 替换列表框中的文本

Jak*_* F. 1 c# listbox

我怎么说呢,检测列表框中的特定文本并将其替换为特定文本。例如:

        private void timer1_Tick(object sender, EventArgs e)
    {
        if(listBox1.Text.Contains("Hi"))
        {
            // replace with Hello
        }
    }
Run Code Online (Sandbox Code Playgroud)

Ian*_*Ian 5

在 中WinForms,你可以这样做:

if(listBox1.Items.Cast<string>().Contains("Hi")){ //check if the Items has "Hi" string, case each item to string
    int a = listBox1.Items.IndexOf("Hi"); //get the index of "Hi"
    listBox1.Items.RemoveAt(a); //remove the element
    listBox1.Items.Insert(a, "Hello"); //re-insert the replacement element
}
Run Code Online (Sandbox Code Playgroud)