Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,943 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,413 people online right now. Registration is fast and FREE... Join Now!




Listbox help

 
Reply to this topicStart new topic

Listbox help

quakeworld2007
12 Dec, 2007 - 06:00 PM
Post #1

New D.I.C Head
*

Joined: 28 Sep, 2007
Posts: 44


My Contributions
I have a listbox in one windows form and when the item is selected and a update button is clicked I am trying to make it open a new form where the user can update the item.

My code in the class where the listbox is

CODE


Public Function SelcIndex()

        Static i As Integer
        i = ListBox1.SelectedIndex
        Return i

    End Function



and my code in the new form i want to open is

CODE


locs = locat.GetAll()

          
            Dim i As Integer

            For i = 0 To locs.Count - 1

                i = thelocation.SelcIndex

                alocation = locs(i)

                TextBox2.Text = alocation.GetLoc



The problem is no matter what item in the listbox i select i is always equal to -1. How do I fix it so it matches with whatever item was selected from the listbox?



User is offlineProfile CardPM
+Quote Post

girasquid
RE: Listbox Help
12 Dec, 2007 - 07:53 PM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,295



Thanked: 18 times
Dream Kudos: 725
My Contributions
I might be missing something, but...why are you writing a function to duplicate the functionality of SelectedIndex? Why not just use this:
CODE

i = thelocation.SelectedIndex;

??
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Listbox Help
12 Dec, 2007 - 09:40 PM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 4,029



Thanked: 38 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE
The problem is no matter what item in the listbox i select i is always equal to -1. How do I fix it so it matches with whatever item was selected from the listbox?

That's because you've declared i as a static variable and not a global.
Static variables are accessible only in the function they were declared in.

Declare your variable as a global and it should work.

Why do you need 'i', if the SelectedIndex property is already available?
User is offlineProfile CardPM
+Quote Post

quakeworld2007
RE: Listbox Help
13 Dec, 2007 - 08:55 AM
Post #4

New D.I.C Head
*

Joined: 28 Sep, 2007
Posts: 44


My Contributions
Ok, I changed it to this

CODE


locs = locat.GetAll()

            alocation = locs(thelocation.ListBox1.SelectedIndex)



But the selectedIndex is still always -1. Any suggestions?


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Listbox Help
13 Dec, 2007 - 10:30 AM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Do you have anything selected in the listbox?

You need to click on and highlight an item in the list. The SelectedIndex will return -1 when there is nothing selected in the listbox.
User is offlineProfile CardPM
+Quote Post

quakeworld2007
RE: Listbox Help
13 Dec, 2007 - 12:44 PM
Post #6

New D.I.C Head
*

Joined: 28 Sep, 2007
Posts: 44


My Contributions
Yeh, that is the problem then I am highlighting an item, but then clicking on a button that i have defined like this

CODE


Dim form As New modifyLocation()
        form.ShowDialog()

and that form is trying to get the selected item with this code

CODE


locs = locat.GetAll()

            alocation = locs(thelocation.ListBox1.SelectedIndex)



but no item is beging passed. Is the only way to fix this to add a constructor to modifyLocation that takes the ListBox1.SelectedIndex?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Listbox Help
13 Dec, 2007 - 01:46 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
That would be the easiest solution, just pass the SelectedIndex to a constructor.

Is that all you need on the other form, the index of the item selected?


User is offlineProfile CardPM
+Quote Post

quakeworld2007
RE: Listbox Help
13 Dec, 2007 - 08:39 PM
Post #8

New D.I.C Head
*

Joined: 28 Sep, 2007
Posts: 44


My Contributions
Yes, that is all the form is goign to need.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Listbox Help
13 Dec, 2007 - 09:00 PM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Then create another constructor and pass the index to it as we discussed. That will work dandy.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month