ListViewItem Error

vellaima

Well-known member
Joined
Jan 29, 2003
Messages
109
Code:
Dim myReader As System.Data.OleDb.OleDbDataReader
myReader = SelectData.ExecuteReader()
While myReader.Read()
str(0) = myReader.GetString(0)
str(1) = myReader.GetString(1)
itm = New ListViewItem(str)
itm.Checked = True   ---> error??
ListView1.Items.Add(itm)

In the above code if i write itm.checked=true during runtime i get error. If i ignore the error the item is indeed getting checked.

If the itm.checked=false, i am not getting run time error.

The runtime error i am getting is "Object reference not set to the instance of the object". Can you please tell me why such error is occuring.
 
ListView is making my life hell

Code:
Dim str(2) as String
Private Sub test_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
	ListView_AddCol()
	ListView_AddData()
End Sub

Private Sub ListView_AddCol()
        ListView1.Columns.Add("Item ID", 110, HorizontalAlignment.Left)
        ListView1.Columns.Add("Item Desc", 170, HorizontalAlignment.Left)
End Sub

Private Sub ListView_AddData()
         str(0) = "Item 1"
         str(1) = "ABC"
         Dim itm as New ListViewItem
         itm.Checked = True
         ListView1.Items.Add(itm)  
End Sub


Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
        Dim ij1 as Integer   
        ij1 = e.Index
        If ListView1.Items(ij1).Checked = True Then
            ListView1.Items(ij1).Checked = False
        End If
End Sub

I am getting run time error when there is ItemCheck Sub and when i set a particular item is set to true during loading.

If there is no itemCheck sub it is working fine

Can anyone please help me as to why such error is occuring since i am new to programming.
 
Code:
itm.Checked = True

That line itself should not cause an error. I am skeptical that it does. If indeed it does, make a sample application that reproduces the error and send it to Microsoft.

Or, just post your whole project here (without binaries) and Ill see what I can do.
 
I tested your code and didnt get any run time error. It seemed to work fine. Can you give more details of the error you are getting. Also, can you briefly describe what you are trying to do.
 
I want to do some validation depending upon whether the item is checked or unchecked. I think the ListView1_ItemCheck is getting triggered during runtime. Can it be avoided. Due to that i am getting errors.
 
Its very possible that setting the checked property raises the ItemCheck event. If this is the case, youll have to use a form-scoped boolean variable to tell the code in that event not to run if youre setting it manually.
 
I am new to vb.net divil can you please provide me with a example. It will be very helpful.
 
Well, you should have something like this to declare your variable and in the ItemCheck event:

Code:
    Private ignoreListviewChecks As Boolean

    Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
        If ignoreListviewChecks Then Return

        Perform validation here
    End Sub

And whenever you change a listitems Checked property manually in code, surround the line by setting the variable to true:

Code:
ignoreListviewChecks = True
myListViewItem.Checked = True
ignoreListviewChecks = False
 
Divil, can you please help me with this code. It will be very helpful.

I have written the following sample code. Can you please modify it for me. It is getting triggered during run time.

Code:
Dim ij as integer
ij = e.Index()

if ListView1.Items(ij).checked=True
         Do some validation
else 
         Do some validation
end if.
 
Back
Top