【发布时间】:2012-03-14 19:58:19
【问题描述】:
我有一个 ListBox 控件,其中填充了大型零售连锁店的分支机构。使用系统的员工必须登录到相关分行,我希望他们能够搜索列表框找到他们的分行。
我已经为搜索框中的文本更改创建了一个事件处理程序,并且已经尝试在 StackOverflow 上使用代码声音:
private int lastMatch = 0;
private void txtSearch_TextChanged(object sender, EventArgs e)
{
int x = 0;
string match = txtSearch.Text;
if (txtSearch.Text.Length != 0)
{
bool found = true;
while (found)
{
if (lbBranches.Items.Count == x)
{
lbBranches.SetSelected(lastMatch, true);
found = false;
}
else
{
lbBranches.SetSelected(x, true);
match = lbBranches.SelectedValue.ToString();
if (match.Contains(txtSearch.Text))
{
lastMatch = x;
found = false;
}
x++;
}
}
}
}
当我编译并开始在搜索框中输入时,我收到此错误:
Object reference not set to an instance of an object.
有问题的行是:
match = lbBranches.SelectedValue.ToString();
我不知道那里可能出了什么问题,有人知道吗?
谢谢!
【问题讨论】:
-
通常,该错误意味着您正在尝试引用空对象的属性。在给定的行中,
lbBranches或其SelectedValue属性似乎为空。从给出的代码中很难判断为什么会这样。