Solved

reselect ListBox1

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

790 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