[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

select different records

Posted on 2006-05-09
13
Medium Priority
?
283 Views
Last Modified: 2010-04-23
hello,
I have a listbox with
name1
name2
name3
and when I select a name I see it in a label,
I would like to make a button so that when you
click it, it selects name1 then after 2 secs selects
name2 etc.. next record until the end! how can I do that?
thanks
0
Comment
Question by:aplelois
  • 5
  • 4
  • 3
12 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 16645920
Hello aplelois,

add this line in the top of your form

Imports System.Threading


then the button click will be like

----------
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim myThread As Thread

        i = 0
        While Not i = ListBox1.Items.Count
            ListBox1.SelectedIndex = i
            myThread = Thread.CurrentThread()
            myThread.Sleep(2000)
            i = i + 1
        End While

    End Sub
----------

it will loop through the listbox names with a pause of 2 seconds (2000 milliseconds) you can change it yourself if needed

hope this helps a bit
bruintje
0
 

Author Comment

by:aplelois
ID: 16646150
its working but I have this
Label1.Text=ListBox1.SelectedItem
is not working why?
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16646165
because its not in the loop, change the code to


 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim myThread As Thread

        i = 0
        While Not i = ListBox1.Items.Count
            ListBox1.SelectedIndex = i
            Label1.Text=ListBox1.SelectedItem
            myThread = Thread.CurrentThread()
            myThread.Sleep(2000)
            i = i + 1
        End While

    End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:aplelois
ID: 16646399
the label is not showing!
0
 

Author Comment

by:aplelois
ID: 16646407
and if I click on the application after the 4th loop it crashes
0
 
LVL 44

Assisted Solution

by:bruintje
bruintje earned 1000 total points
ID: 16646548
this updates the label and deletes the thread object, i do not know why it crashed on your side

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim myThread As Thread

        i = 0
        myThread = Thread.CurrentThread()
        While Not i = ListBox1.Items.Count
            ListBox1.SelectedIndex = i
            Label1.Text = ListBox1.SelectedItem
            Label1.Refresh()
            myThread.Sleep(2000)
            i = i + 1
        End While
        myThread = Nothing
    End Sub
0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 1000 total points
ID: 16646669
Put a Timer Control on the Form
Set its Interval to 2 secs (2000 as it is milliseconds)
And set its Enabled Property to False

Then on ListBox1's  SelectedIndexChanged event write this
Label1.Text=ListBox1.SelectedItem

Then on Timer1's Tick event write this code
If ListBox1.SelectedIndex >= ListBox1.Items.Count -1 Then
       Timer1.Enabled=False
Else
       ListBox1.SelectedIndex=ListBox1.SelectedIndex + 1
End if

Finally, on the Button Click Say
Timer1.Enabled=True

#I have typed the code directly here so take care of typo errors (if any)
Thanks
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 16646728
Sorry didn't refresh, you'd already accepted the answer.
The code by bruintje is fine except for it would result in freezing of the UI for 2 secs because you are forcing the current Thread to sleep. If that's ok for you no probs...But I think for such type of work Timer Control is best suited so you can try my solution as well.
0
 

Author Comment

by:aplelois
ID: 16646736
arif_eqbal, you code is the one I was looking for im asking in CS to give you some points. thanks
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16646745
a moderator can undo the accept here, so you can accept the other comment as answer
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 16646830
Don't mind the points, They are not so important after all
0
 

Author Comment

by:aplelois
ID: 16646928
do you guys have some idea in here to help me http:Q_21844809.html
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question