Solved

reselect ListBox1

Posted on 2006-07-15
4
307 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
A short film showing how OnPage and Connectwise integration works.

914 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now