ListView.TopItem and Minimized Window

l1on3l

Member
Joined
Apr 21, 2011
Messages
6
Programming Experience
5-10
Hi Everyone,


I have an issue in a project trying to auto-scroll down a ListView (used as Details with columns) when adding a new ListViewItem when the window is minimized. I guess we have to use TopItem property to set force the listview to scroll down.
I get two exceptions when the form is minimized:
- "Object reference not set to an instance of an object."
- "Index (zero based) must be greater than or equal to zero and less than the size of the argument list."

I'm just using standard .Net components (Events/Delegate) with standard way to use them.


I made a small test code to isolate the issue:


VB.NET:
' Code based on a simple form (name=Form1) and just adding a Listview (name=lvLogs, View=Details, Columns=3 cols) 
Imports System
Imports System.Windows.Forms
Imports System.Threading




Public Class Form1


    Public Delegate Sub DisplayMsgDelegate(ByVal str As String, ByVal level As TraceEventType)
    Dim WithEvents _o As MyObject




    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _o = New MyObject()
    End Sub






    Private Sub DisplayMsg(ByVal msg As String, ByVal level As TraceEventType)
        Dim item As New ListViewItem("")
        item.SubItems.Add(DateTime.Now.ToString("HH:mm:ss.fff"))
        item.SubItems.Add(msg)
        item.BackColor = Color.LightBlue
        Me.lvLogs.Items.Add(item)
        Try
            Me.lvLogs.TopItem = item ' Auto-scroll down
        Catch ex As Exception
            Console.WriteLine(String.Format("[DisplayMsg] Unable to display message: '{0}', (level={1})", ex.ToString()))
        End Try
    End Sub


    Private Sub OnNotification(ByVal sender As System.Object, ByVal e As NotificationEventArgs) Handles _o.Notification
        Dim lvl As TraceEventType = DirectCast(e.Level, TraceEventType)
        If Not Me.InvokeRequired Then
            Me.DisplayMsg(e.Message, lvl)
        Else
            Dim dlg As New DisplayMsgDelegate(AddressOf DisplayMsg)
            Try
                Invoke(dlg, New Object() {e.Message, lvl})
            Catch ex As Exception
                Console.WriteLine(String.Format("[OnNotification] Unable to display message: '{0}', (level={1})", e.Message, e.Level.ToString()))
            End Try
        End If
    End Sub


End Class




' ---------------------------------------------------------------------------------------------------------




Public Class MyObject


    Public Event Notification As EventHandler(Of NotificationEventArgs)




    Public Sub New()
        Dim t As New Thread(AddressOf MyThreadSub)
        t.Start()
    End Sub




    Private Sub MyThreadSub()
        While True
            RaiseEvent Notification(Me, New NotificationEventArgs("coucou", TraceEventType.Error))
            Thread.Sleep(1000)
        End While
    End Sub


End Class


' ---------------------------------------------------------------------------------------------------------


Public Class NotificationEventArgs
    Inherits EventArgs


#Region "Members"


    Private _message As String
    Private _level As TraceEventType


#End Region


#Region "Constructor"


    Public Sub New(ByVal message As String, ByVal level As TraceEventType)
        _message = message
        _level = level
    End Sub


    Public Sub New(ByVal message As String)
        _message = message
        _level = TraceEventType.Information
    End Sub


#End Region


#Region "Properties"


    Public ReadOnly Property Message() As String
        Get
            Return _message
        End Get
    End Property


    Public ReadOnly Property Level() As TraceEventType
        Get
            Return _level
        End Get
    End Property


#End Region


End Class


The exceptions occur this way (both are caught in this code example):
- 1st one: in DisplayMsg()
- 2nd one: in OnNotification()


Thanks for your precious help as I'm really stuck now: I mean I can use workaround as: "If Me.State <> State.Minimized Then" or prevent the user to reduce the window, but I would prefer to understand from where it comes from.


Thanks
/Lionel
 
I tried the code and did not get any exception when form was minimized, including if form was minimized on startup, but when closing the form the foreground thread is alive and keeps invoking and you get a first chance ObjectDisposedException on accessing lvLogs that has been disposed, and FormatException from String.Format in exception handler since it is missing a parameter (in DisplayMsg).
 
Hi John,
Did you add breakpoints in the Catch parts (Try/Catch blocks)? This is in the very parts that the Exceptions I told about are raised. This happens on VS2005/2008.
PS: This is just a sample of my code in which application closing is not handled as not part of my wonders.
Thanks
/Lionel
 
Do you have reproducable code for the "minimize problem"?
 
That is basically the same code. I get no exceptions on minimize. Handling closing you added code for I see. You still have the code error in DisplayMsg that I mentioned.
 
Yes it's the same code. I'm running on Win7 and this issue happens on Win Server 2003 too.
I don't know if this is due to the system.
Thanks for your help anyway.
/Lio
 
Back
Top