tags:

views:

221

answers:

3

Hi guys,

I have a label who's text is determined by looping through a listbox upon a click event. I would like to have a timer loop through the listbox (... to set the label's text) if the button is not pressed in time ('x' seconds).

Please help, so lost

A: 

Try using the timer control available in .net .!

The timer control allows you to set specific time intervals until some code has to be executed.

an illustrative step by step how to add text to a listbox using a timer... http://www.ehow.com/how_4590003_program-timer-control-vbnet.html

another timer control tutorial for u to read..

http://www.vbdotnetheaven.com/UploadFile/mahesh/TimerControl04262005033148AM/TimerControl.aspx

Madi D.
A: 

I'm assuming this is WinForms. I think what you should be doing is handling the SelectedIndexChanged event of the ListBox to set your label's text, this would be easier to implement than a timer.

In your form's constructor you could have the following:

ListBox1.Items.Clear()
ListBox1.Items.Add(New KeyValuePair(Of Integer, String)(0, "Value-1"))
ListBox1.Items.Add(New KeyValuePair(Of Integer, String)(1, "Value-2"))
ListBox1.DisplayMember = "Value"
ListBox1.ValueMember = "Key"

and then you could have a method to handle the SelectedIndexChanged event as follows:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    If ListBox1.SelectedItem IsNot Nothing Then
        Label1.Text = ListBox1.SelectedItem.Value
    End If
End Sub
mdresser
+1  A: 

Use the timer control and set the selectedindex. Then you can use the SelectedIndexChanged event to handle the new selection.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

ListBox1.Items.Add("First Item")
ListBox1.Items.Add("Second Item")
ListBox1.Items.Add("Third Item")
ListBox1.Items.Add("Fourth Item")
ListBox1.SelectedIndex = 0

Timer1.Interval = 500
Timer1.Start()

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Dim i As Integer
i = ListBox1.SelectedIndex
i = i + 1
If i > ListBox1.Items.Count - 1 Then i = 0
ListBox1.SelectedIndex = i

End Sub
xpda