首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

ListBox的溢出有关问题

2012-04-19 
ListBox的溢出问题在窗口程序里有4个Listbox:lstcpj,lstcmg,lstscd,lstloglstcpj,lstcmg里的项都对应一个t

ListBox的溢出问题
在窗口程序里有4个Listbox:lstcpj,lstcmg,lstscd,lstlog
lstcpj,lstcmg里的项都对应一个txt文件名,
lstscd用来显示lstcmg里对应txt文件的内容(文件内容为一些十六进制的指令,比如: 关闭,0xFFFF)
lstlog用来显示所有已经发送出去的指令,即为lstscd里已经显示过的内容

lstcpj,lstCmg选择项的改变是由程序控制的,被选项改变时,从txt文件里读命令到lstscd里:(以lstcmg为例)

Private Sub lstCmg_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCmg.SelectedIndexChanged

Dim strFoldPath_lstC As String
strFoldPath_lstC = fncGetFoldPath(strFilePath_cmg)
strListboxItem_lstC = lstCmg.SelectedItem.ToString

If strListboxItem_lstC <> "" Then

lstScd.Items.Clear()
  '下面函数为从txt里读入的函数
fncSetListboxFromFile(strFoldPath_lstC & "\" & strListboxItem_lstC, lstScd)

End If

If lstScd.Items.Count > 0 Then

lstScd.SelectedIndex = 0

End If
End Sub

Public Sub fncSetListboxFromFile(ByVal strFileName As String, ByRef lstbName As System.Windows.Forms.ListBox)

Dim strItemText As String'a line of the file
Dim blEmptyFile As Boolean
blEmptyFile = True

Dim sr As New StreamReader(strFileName, Encoding.Default)
While (sr.Peek >= 0)
'Do While Not sr.EndOfStream

strItemText = sr.ReadLine

If Trim(strItemText) <> "" Then

lstbName.Items.Add(strItemText)

blEmptyFile = False

End If

End While
'Loop

sr.Close()
  end sub


现在的问题是在运行一段时间后(lstcpj和lstcmg都从txt文件里读入了一些项),在lstbName.Items.Add(strItemText)这行会报下面的错误:
An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll

网上查了下,好像说是Listbox的选择项改变时的问题,但具体没看太懂,大家有碰到过类似问题吗?先谢谢了!

附网上查到的说明
http://www.debugging.com/bug/19023

listbox's take an object for each row. if you have inserted a string, then you can set the selected item to a string. if you've assigned the item to an object it uses the object's tostring to render the text in the box. if you then try to set the selected item equal to the string text, it will attempt to do a compare (object.equals) for each item in the list, and since the string you're setting doesn't equal the object it has stored as the item, then it sets the index to -1 (ie, it doesn't contain the item you set selected item to).
make sure whatever you're setting the selected item to is the same thing thats stored in the items collection else you'll get the described behavior.
if you want to change the text of each item make sure you override its tostring, and change what that produces as this will be what's used in your list.

[解决办法]
有没有出现递归调用(一个函数内部调用自身)。堆栈溢出多半是这个原因。
[解决办法]

探讨

有没有出现递归调用(一个函数内部调用自身)。堆栈溢出多半是这个原因。

热点排行