C# 将标签文本更改为列表框选择文本
C# Changing Label Text to Listbox Selection Text
所以我一直在浏览 Google 的伟大思想,但没有找到可行的解决方案;我有一个列表框 (instanceSelection) 和一个标签 (instanceTxt)。当我 select 集合中的一个项目时,我希望 instanceTxt 与 instanceSelection 的文本相同。
这是我认为可以更早运行的代码行:
private void instanceSelection_SelectedIndexChanged(object sender, EventArgs e)
{
instanceTxt.Text = (string)this.instanceSelection.SelectedValue.Text;
}
但是有一次没变,有一次代码块变成了“0”。使用 "ToString".
时有时也会出现空错误
谢谢,
威廉
试试这个:
private void instanceSelection_SelectedIndexChanged(object sender, EventArgs e)
{
if(instanceSelection.SelectedIndex > -1)
instanceTxt.Text = instanceSelection.Items[instanceSelection.SelectedIndex].ToString();
}
所以我一直在浏览 Google 的伟大思想,但没有找到可行的解决方案;我有一个列表框 (instanceSelection) 和一个标签 (instanceTxt)。当我 select 集合中的一个项目时,我希望 instanceTxt 与 instanceSelection 的文本相同。
这是我认为可以更早运行的代码行:
private void instanceSelection_SelectedIndexChanged(object sender, EventArgs e)
{
instanceTxt.Text = (string)this.instanceSelection.SelectedValue.Text;
}
但是有一次没变,有一次代码块变成了“0”。使用 "ToString".
时有时也会出现空错误谢谢, 威廉
试试这个:
private void instanceSelection_SelectedIndexChanged(object sender, EventArgs e)
{
if(instanceSelection.SelectedIndex > -1)
instanceTxt.Text = instanceSelection.Items[instanceSelection.SelectedIndex].ToString();
}