Get the last index/value from a listbox

时间:2016-11-26 18:29:38

标签: c# wpf listbox

I'm trying to get the last value within my listbox, so the user is able to enter number into the listbox. And I want to be able to output to a label text, what that value is. So it would show the last index from the listbox. Although, its just outputting the index number, -1.

if (lstHoldValue.SelectedIndices.Count > 0)
{
 label1.Text =  Convert.ToString(this.lstHoldValue.SelectedIndex = this.lstHoldValue.Items.Count - 1);
}

1 个答案:

答案 0 :(得分:1)

要获取您使用的最后一项lstHoldValue.Items[lstHoldValue.Items.Count - 1]并连同检查(以查看列表框中是否至少有一项,我们在if语句中执行代码之前)它将看起来像这样:

if (lstHoldValue.Items.Count > 0)       
{
    label1.Text = lstHoldValue.Items[lstHoldValue.Items.Count - 1].ToString();
}