首页 文章

C#将标签文本更改为列表框选择文本

提问于
浏览
1

所以我一直在浏览谷歌的伟大思想,但没有找到一个有效的解决方案;我有一个列表框(instanceSelection)和一个标签(instanceTxt) . 我希望当我选择集合中的项目时,instanceTxt与instanceSelection的文本相同 .

这是我认为之前可以使用的代码行:

private void instanceSelection_SelectedIndexChanged(object sender, EventArgs e)
    {
        instanceTxt.Text = (string)this.instanceSelection.SelectedValue.Text;
    }

但有一次它没有改变,另一个代码阻止它改为“0” . 使用“ToString”时,有时也会出现null错误 .

谢谢,威廉

1 回答

  • 1

    试试这个:

    private void instanceSelection_SelectedIndexChanged(object sender, EventArgs e)
            {
    
    if(instanceSelection.SelectedIndex > -1)
         instanceTxt.Text = instanceSelection.Items[instanceSelection.SelectedIndex].ToString();
            }
    

相关问题