Solved

reselect ListBox1

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

10 Experts available now in Live!

Get 1:1 Help Now