ListBox.Items.Count>0 但執行ListBox.SelectedIndex=0 時確出錯.Index was outside the bounds of the array
我是在一個TabControl中放的ListBox   ,每個TabPage中都有一個ListBox. 
 在雙擊了當前TabPage中的ListBox後.給所有TabPage中的ListBox設置數據源屬性.   
 是這樣寫的.但在執行到給不是當前TabPage中的ListBox設置數據源屬性時,執行到 
 ListBox   .SelectedIndex   =   0(Items.Count> 0)   就會出現Index   was   outside   the   bounds   of   the   array錯誤. 
 若我打開程式後.每個TabPage都點一遍後.就不會出錯. 
 不知道是不是.Net的Bug?大家有沒碰到過.   
                         ListBox   .DataSource   =   ds.Tables(0) 
                         ListBox   .DisplayMember   =   ds.Tables(0).Columns(1).ColumnName 
                         ListBox   .ValueMember   =   ds.Tables(0).Columns(0).ColumnName 
                      ListBox   .Items.Count   >    0   Then   ListBox   .SelectedIndex   =   0 
------解决方案--------------------你这是VB的语法啊,怎么提问到C#了, 
 If ListBox .Items.Count >  0 Then ListBox .SelectedIndex = 0这里是不是少了If? 
 C#: 
 if (listBox.Items.Count >  0) 
 { 
     listBox.SelectedIndex = 0; 
 }
------解决方案--------------------能把异常的具体信息贴出来看下吗?
------解决方案--------------------是不是因为你没有点选界面上面的ListBox造成的?
------解决方案--------------------你可以用DEBUG看一下,你每点一个TAB PAGE会发生什么,你只写这些代码肯定是没有错的。(我想你这些代码都不是在一起的)因为有些EVENT是会在你给PROPERTY时就会RISE的,比如你这里给DisplayMember 时就会出生好像是DataMember_Changed事件的,你看看你是否在这个事件里做了点什么?
------解决方案--------------------我觉得楼主给的信息可能不够分析问题所在.