I have
1 listbox, 1 textbox, and 2 buttons (for start, and stop)
if I press the the buton for start the textbox will display (one by one) the items in listbox.. and if i press stop the textbox will stop displaying items..
is it possible? do i have to use a timer for this?
i think i have to use Do While (or Do until) Statement...but..hmmm i don't know.. :D
Guys Help Me...Thanks..

(sorry i am very poor in english)
^_^

Dani AI

Generated

A concise, practical pattern that builds on ’s suggestion and fits ’s scenario (800+ items, listbox hidden, a Start and Stop control): keep a form-level index, let a UI timer advance that index, and update the textbox (and optionally the list selection) on each tick. The example below is a simple, safe WinForms pattern that also disables/enables buttons to prevent duplicate starts and preserves the current position when stopped.

' form-level
Private indexPos As Integer = -1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If lstItems.Items.Count > 0 Then
        indexPos = If(lstItems.SelectedIndex >= 0, lstItems.SelectedIndex, 0)
        txtDisplay.Text = lstItems.Items(indexPos).ToString()
    End If
    btnStop.Enabled = False
End Sub

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    If lstItems.Items.Count = 0 Then Return
    displayTimer.Interval = 100   ' 100 ms -> adjust for readability/performance
    displayTimer.Start()
    btnStart.Enabled = False
    btnStop.Enabled = True
End Sub

Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
    displayTimer.Stop()
    btnStart.Enabled = True
    btnStop.Enabled = False
End Sub

Private Sub displayTimer_Tick(sender As Object, e As EventArgs) Handles displayTimer.Tick
    indexPos = (indexPos + 1) Mod lstItems.Items.Count
    txtDisplay.Text = lstItems.Items(indexPos).ToString()
    lstItems.SelectedIndex = indexPos   ' optional: keep the list selection in sync
End Sub

Notes and troubleshooting: System.Windows.Forms.Timer runs on the UI thread so the Tick handler may update controls directly; if a background timer or Task is used instead then marshal updates to the UI with Invoke/BeginInvoke. Hiding the listbox (Visible = False) does not prevent reading its Items. For very short intervals, Windows timer granularity and UI work inside Tick can cause jitter—keep expensive work out of the Tick handler and increase Interval if updates skip. To preserve position across sessions, persist indexPos (or store it in the control Tag) before closing.

Recommended Answers

All 4 Replies

You need a timer. You will also need a class variable to keep track of which item in the listbox is the next one to copy. When it gets to the last item the index will be reset to the first item. You don't need a loop. The iteration will be handled on each tick of the timer. If for any reason you do not want to create a class variable for the index you could alwyas store it in the Tag property of the listbox or textbox control.

.hmm.. .i am confused.. :(

here is what I'm trying to do:
i have 800+ items that are already stored on the listbox or on the listview..
the 1st item is already selected once i start the program..
then when i press the start button the timer will start and in every tick of the timer
you can see that the program will automatically selecting the items down to the last item..
and when i press stop the program will stop to selecting items.

i already know how to display selected item to a textbox
my problem now is how to implement my idea that is stated above..
could that be possible?

i am new in vb.net :(

but anyway thanks for the reply sir..
God Bless..

^_^

Surely possible. Let's say you have exactly 832 items in the listbox (or listview). That means the first item is accessed by

ListBox1.Items(0)

and the last item by

ListBox1.Items(831)

Where 831 is ListBox1.Items.Count - 1. If you have a class level index (let's call it currindex) then it is initialized to 0. Once you start your timer then the timer handler will

increment currindex

'if past the end of the list then wrap back to the first item

if currindex = ListBox1.Items.Count then
    currindex = 0
end if

copy text from ListBox1.Items(currindex) to the textbox

I find it almost always helps if you write out the problem steps in English (or whatever language suits you) and only then translate that into actual VB code. If you leave your pseudo-code in place you can just prefix all lines with ' and they become your comments in the code.

thanks a lot sir..
:)
i set the listbox visible = false
and now i have a loading text(changing every .100 secs)..
that's what i want to do. lol. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.