【问题标题】:Weird listview behavior?奇怪的列表视图行为?
【发布时间】:2010-08-03 19:04:23
【问题描述】:

这个问题快把我逼疯了..

此代码引发消息框的索引超出范围异常:

    private void MainTabs_Selected(object sender, TabControlEventArgs e)
    {
        if (MainTabs.SelectedTab.Name == "ActiveDirectoryTab")
        {
            ADServerSelect.Items[0].Selected = true;
            MessageBox.Show(ADServerSelect.Items[0].Text);
        }
    }

但如果消息框是事件的结果,它就可以正常工作:

    private void MainTabs_Selected(object sender, TabControlEventArgs e)
    {
        if (MainTabs.SelectedTab.Name == "ActiveDirectoryTab")
        {
            ADServerSelect.Items[0].Selected = true;
        }
    }

    private void testButton_Click(object sender, EventArgs e)
    {
        MessageBox.Show(ADServerSelect.Items[0].Text); 
    }

任何帮助将不胜感激..

【问题讨论】:

  • 我们能不能多看一点代码,你是怎么绑定数据的等等?
  • 所有这些都在同一个线程中运行吗?
  • 1.数据绑定没有什么花哨的..这些项目是硬编码的 2.所有都在同一个线程(UI)上运行
  • @Shlomi,我们可以看看你是如何绑定数据的,也许还有 XAML?在没有看到更多代码的情况下,不清楚为什么会出现此错误。
  • @Matt,绑定数据是什么意思?这只是一个简单的 WinForms 列表视图,我已经通过 VS 设计器手动添加了项目(listViewItem)。你能指点我想看的 relevenat 代码部分吗?谢谢

标签: c# listview


【解决方案1】:

看起来像是在 MainTabs_Selected 事件之后添加到列表视图的项目。可以查一下吗?

【讨论】:

  • 这行代码工作正常:ADServerSelect.Items[0].Selected = true; //选择listview第一项所以我必须假设listview项是在这个事件之前添加的,不是吗?
猜你喜欢
  • 2011-11-04
  • 2019-01-10
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多