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:
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 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