Vb.net - 指数超出范围。必须是非负数且小于集合列表视图的大小 [英] Vb.net - index was out of range. Must be non-negative and less than the size of the collection listview

查看:104
本文介绍了Vb.net - 指数超出范围。必须是非负数且小于集合列表视图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码在运行时更改listview图像,但我收到错误:



索引超出范围。必须是非负面且小于集合的大小



我尝试过的事情:



 尝试 
lvroom.BeginUpdate()
对于 每个 L 作为 ListViewItem lvroom.Items
Dim mycommand As MySqlCommand = conn.open.CreateCommand
Dim query1 = 选择房间。* ,roomtype。* FROM房间,房型WHERE room.RoomType = roomtype.RoomType AND RoomNo ='& L.ImageKey& '
mycommand.CommandText = query1
Dim baca1 作为 MySqlDataReader = mycommand.ExecuteReader
如果 baca1 .Read()然后
Dim NoKamar As 字符串 = baca1( RoomNo
Dim str As String
如果 Len(NoKamar)= 1 然后
str = 00& NoKamar
ElseIf Len(NoKamar)= 2 然后
str = 0& NoKamar
Else
str = NoKamar
结束 如果
如果 baca1( 可用)= True baca1( 状态)= 错误 然后

L.ImageList.Images.RemoveByKey(L.ImageKey)
Dim newbitmap As 位图= My.Resources.bulat_hijau
Dim g 作为图形
g = Graphics.FromImage(newbitmap)
g.DrawString(str,字体( Arial 65 ,FontStyle.Bold), SolidBrush(Color.Yellow),点( 85 50 ))
g.DrawString( ______________________ New 字体( Arial 65 ,FontStyle.Bold), SolidBrush(Color.White),点( 0 80 ))
g.DrawString(baca1( Singkatan),字体( Arial 65 ,FontStyle.Bold), SolidBrush(Color.White),点( 82 170 ))
L.Text = EMPTY
L.Font = 字体( Arial 12 ,FontStyle.Bold)
L.ImageList.Images.Add(NoKamar,newbitmap)

ElseIf baca1( 可用)= 错误 baca1( 状态)= 错误 然后

L .ImageList.Images.RemoveByKey(L.ImageKey)
Dim newbitmap As Bitmap = My。 Resources.bulat_purple
Dim g As Graphics
g = Graphics.FromImage(newbitmap)
g.DrawString(str,字体( Arial 65 ,FontStyle.Bold), SolidBrush(Color.Yellow) ),点( 85 50 ))
g.DrawString( _______________ _______字体( Arial 65 ,FontStyle.Bold), SolidBrush(Color.White),< span class =code-keyword>新点( 0 80 ))
g.DrawString(baca1( Singkatan),字体( Arial 65 ,FontStyle.Bold), SolidBrush(Color.White),点( 82 170 ))
L.Text = N / A
L.Font = 字体( Arial 12 ,FontStyle.Bold)
L.ImageList.Images.Add(NoKamar,newbitmap)

End 如果
结束 如果
baca1.Close()
lvroom.Refresh()
下一步
lvroom.EndUpdate()
Catch ex As 异常
MsgBox(ex.Message)
最后
conn.close()
结束 尝试

解决方案

从哪里开始...



首先,你是代码泄漏r资源,非常糟糕。您正在创建的那些SqlCommand,Font,Bitmap和Graphics对象绝对必须在您使用它们时进行处置。您将从资源中运行Windows并且它将开始崩溃。



接下来,我们无法告诉您代码失败的原因。我们无权访问数据。您将不得不在此处发布的代码的顶部放置一个断点并逐行逐步执行,检查变量和控件的内容,方法是将鼠标悬停在它们上面,以便计算出来。

I'm trying to change listview image at runtime with this code, but I am getting error:

Index was out of range. Must be non-negative and less than the size of the collection

What I have tried:

Try
        lvroom.BeginUpdate()
        For Each L As ListViewItem In lvroom.Items
            Dim mycommand As MySqlCommand = conn.open.CreateCommand
            Dim query1 = "SELECT room.*,roomtype.* FROM room, roomtype WHERE room.RoomType=roomtype.RoomType AND RoomNo='" & L.ImageKey & "'"
            mycommand.CommandText = query1
            Dim baca1 As MySqlDataReader = mycommand.ExecuteReader
            If baca1.Read() Then
                Dim NoKamar As String = baca1("RoomNo")
                Dim str As String
                If Len(NoKamar) = 1 Then
                    str = "00" & NoKamar
                ElseIf Len(NoKamar) = 2 Then
                    str = "0" & NoKamar
                Else
                    str = NoKamar
                End If
                If baca1("Available") = True And baca1("Status") = False Then

                    L.ImageList.Images.RemoveByKey(L.ImageKey)
                    Dim newbitmap As Bitmap = My.Resources.bulat_hijau
                    Dim g As Graphics
                    g = Graphics.FromImage(newbitmap)
                    g.DrawString(str, New Font("Arial", 65, FontStyle.Bold), New SolidBrush(Color.Yellow), New Point(85, 50))
                    g.DrawString("______________________", New Font("Arial", 65, FontStyle.Bold), New SolidBrush(Color.White), New Point(0, 80))
                    g.DrawString(baca1("Singkatan"), New Font("Arial", 65, FontStyle.Bold), New SolidBrush(Color.White), New Point(82, 170))
                    L.Text = "EMPTY"
                    L.Font = New Font("Arial", 12, FontStyle.Bold)
                    L.ImageList.Images.Add(NoKamar, newbitmap)

                ElseIf baca1("Available") = False And baca1("Status") = False Then

                    L.ImageList.Images.RemoveByKey(L.ImageKey)
                    Dim newbitmap As Bitmap = My.Resources.bulat_purple
                    Dim g As Graphics
                    g = Graphics.FromImage(newbitmap)
                    g.DrawString(str, New Font("Arial", 65, FontStyle.Bold), New SolidBrush(Color.Yellow), New Point(85, 50))
                    g.DrawString("______________________", New Font("Arial", 65, FontStyle.Bold), New SolidBrush(Color.White), New Point(0, 80))
                    g.DrawString(baca1("Singkatan"), New Font("Arial", 65, FontStyle.Bold), New SolidBrush(Color.White), New Point(82, 170))
                    L.Text = "N/A"
                    L.Font = New Font("Arial", 12, FontStyle.Bold)
                    L.ImageList.Images.Add(NoKamar, newbitmap)

                End If
            End If
                baca1.Close()
            lvroom.Refresh()
        Next
        lvroom.EndUpdate()
        Catch ex As Exception
        MsgBox(ex.Message)
        Finally
        conn.close()
        End Try

解决方案

Where to begin...

First, you're code leaks resources, badly. Those SqlCommand, Font, Bitmap, and Graphics objects you're creating absolutely MUST be Disposed when you're done using them. You WILL run Windows out of resources and it will start crashing.

Next, we can't tell you why your code is failing. We don't have access to the data. You are going to have to put a breakpoint at the top of the code you posted here and step through it line-by-line, examining the contents of variables and controls, by hovering the mouse over them, to figure this out.


这篇关于Vb.net - 指数超出范围。必须是非负数且小于集合列表视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆