Solved

reselect ListBox1

Posted on 2006-07-15
4
308 Views
Last Modified: 2010-04-23
hello
how can I make a button
that clicks the selected
ListBox1.SelectedIndex

this one will go to the next one
ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1

this one will go to the one before
ListBox1.SelectedIndex = ListBox1.SelectedIndex - 1

but how do I make it to reselect the actula one?
0
Comment
Question by:aplelois
4 Comments
 
LVL 14

Expert Comment

by:Ramuncikas
ID: 17115146
Could you please clear out: you want to have three buttons where:
 - first would select a particular item in a listbox
 - second would select next item in a listbox
 - third would select previuos item in a listbox

Is that correct?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17115161
Hi aplelois;

May be I am missing something but the ListBox SelectedIndex always points to the currently selected list box item. So when you do ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1 it will selected the next item in the list box, al long as there is one otherwise you will get an out of range error. and the same holeds true for ListBox1.SelectedIndex = ListBox1.SelectedIndex - 1. The only time that SelectedIndex is not pointing to one of the Items is when SelectedIndex is -1.

So can you explaine your question please?

Fernando
0
 
LVL 13

Accepted Solution

by:
iHadi earned 500 total points
ID: 17115844
Hi aplelois
What I understood from your question is that you want to reselect a selected item in a listbox via a button to reraise the ListBox_SelectedIndexChanged event. To acheive this the easiest way put the code you want to execute in the event in a seprate sub and call it from the event handler and from the button. The following code demonstrates the previous: Add a listbox and a button to a form and past the folowing code:


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        Button1.Text = "Current"
        Button2.Text = "Previous"
        Button3.Text = "Next"

        For i = 1 To 10
            ListBox1.Items.Add(i)
        Next
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Listbox1Event()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Listbox1Event()
    End Sub

    Private Sub Listbox1Event()
        Try
            MessageBox.Show(ListBox1.SelectedItem.ToString)
        Catch

        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            ListBox1.SelectedIndex = ListBox1.SelectedIndex - 1
        Catch

        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
        Catch

        End Try
    End Sub

0
 

Author Comment

by:aplelois
ID: 17115893
I have that if you click on the selected index you go to a page
I want so that if something happens just click that selected index
how can I do that?
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Make all values in arrayist to titlecase+vb.net 23 32
Variable Event ? 3 27
VB.Net - KeyPress Event 4 36
Store results in vb.net 3 20
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 …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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