ListBox & Several Timers

omidlolo

New member
Joined
Apr 24, 2011
Messages
1
Programming Experience
3-5
How do i make a ListBox with the addition of each item, the item be removed after 4 seconds?
Added time and number of items is unknown.
Other parts of the job application should not be hampered.
I know that thread should I use, but I do not know.
Thanks for your help
 
It seems as if the ListBox control does not have an event to capture when an item is added. Therefore there are two options that you have and I really like the second:

1. Use Another Control
2. Create a User Control

In option 2 you can create a user control that has a timer, a listbox, and a custom event that is fired when an item is added. When the event is fired you can start your timer.

VB.NET:
'object in your user control
Public Event ItemAdded(ByVal AddTime as Date)

VB.NET:
'to fire your event
RaiseEvent ItemAdded(Now)
 
Ask if you don't find this example self-explanatory. :)
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim s As String = Me.TextBox1.Text
    Me.ListBox1.Items.Add(s)

    'schedule for removal
    Dim task As New TaskInfo()
    task.OtherInfo = s
    task.Handle = New Threading.ManualResetEvent(False)
    Dim callback As New Threading.WaitOrTimerCallback(AddressOf WaitProc)
    Threading.ThreadPool.RegisterWaitForSingleObject(task.Handle, callback, task, 4000, False)
End Sub

Private Class TaskInfo
    Public Handle As Threading.ManualResetEvent
    Public OtherInfo As Object
End Class


Private Sub WaitProc(ByVal state As Object, ByVal timedOut As Boolean)
    Dim task As TaskInfo = CType(state, TaskInfo)
    task.Handle.Close()
    Me.ListBox1.BeginInvoke(New Threading.WaitCallback(AddressOf Me.ListBox1.Items.Remove), task.OtherInfo)
End Sub
 

Latest posts

Back
Top