By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Third time...Listbox, Array and Selected Index

P: 10
Hello..I'm almost done with this however and I posted it on Visual 3/4/6 and VBA and they directed me to this forum.. The selectedindex is not recognized in VB 2010 and ("co") is apparently should not be infinity?

Please help I been working on this for a while


Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.  
  5. Dim strcountries() As String = {"United States", "France"} For intsubsript As Integer = 0 To 1 LstCountries.Items.Add(strcountries(intsubsript)) Next intsubsript
  6.  
  7. End Sub
  8.  
  9. Private Sub LstCountries_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LstCountries.SelectedIndexChanged
  10.  
  11. Dim strcity() As String = {"Washington", "Paris"}
  12.  
  13. Dim strIndex As String = LstCountries.SelectedIndex txtcity.Text = strcity(strIndex).ToString("co")
  14.  
  15. End Sub End Class
Apr 9 '12 #1

✓ answered by Monomachus

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.  
  5.         Dim strcountries() As String = {"United States", "France"}
  6.         For intsubsript As Integer = 0 To 1
  7.  
  8.             LstCountries.Items.Add(strcountries(intsubsript))
  9.         Next intsubsript
  10.  
  11.     End Sub
  12.  
  13.     Private Sub LstCountries_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LstCountries.SelectedIndexChanged
  14.  
  15.         Dim strcity() As String = {"Washington", "Paris"}
  16.  
  17.         Dim strIndex As Integer = LstCountries.SelectedIndex
  18.         txtcity.Text = strcity(strIndex).ToString()
  19.  
  20.     End Sub
  21. End Class
The problem was that SelectedIndex is String in your code, but actually is an Integer

Share this Question
Share on Google+
4 Replies


Monomachus
Expert 100+
P: 127
Hi,
Please post exactly the behavior you want to achieve, and also the error you are getting also I don't really understand what do you want to achieve with .ToString("co") ?
Expand|Select|Wrap|Line Numbers
  1. strcity(strIndex).ToString("co")
  2.  
Apr 9 '12 #2

P: 10
I trying to load Form1 with the name of the two countries in the LstCountries (a List Box) and the name of the capital of the country should show up in the txtcity.Text when I select the country in the list box..(match up to it)

So when I click on US or France only Washington DC show up for both of them..

Verse select US ..Washington Dc shows up
Select France .....France should show up

But selectedIndex is not recognizable by the Windows applications form for Visual Basic 2010 ..is there another way?
I think this should be ok...if I get rid of the "co" I felt like something should go there...

strcity(strIndex).ToString()
Apr 9 '12 #3

P: 10
annie8508
I trying to load Form1 with the name of the two countries in the LstCountries (a List Box) and the name of the capital of the country should show up in the txtcity.Text when I select the country in the list box..(match up to it)

So when I click on US or France only Washington DC show up for both of them..

Verse select US ..Washington Dc shows up
Select France .....France should show up

But selectedIndex is not recognizable by the Windows applications form for Visual Basic 2010 ..is there another way?
I think this should be ok...if I get rid of the "co" I felt like something should go there...

strcity(strIndex).ToString()
Apr 9 '12 #4

Monomachus
Expert 100+
P: 127
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.  
  5.         Dim strcountries() As String = {"United States", "France"}
  6.         For intsubsript As Integer = 0 To 1
  7.  
  8.             LstCountries.Items.Add(strcountries(intsubsript))
  9.         Next intsubsript
  10.  
  11.     End Sub
  12.  
  13.     Private Sub LstCountries_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LstCountries.SelectedIndexChanged
  14.  
  15.         Dim strcity() As String = {"Washington", "Paris"}
  16.  
  17.         Dim strIndex As Integer = LstCountries.SelectedIndex
  18.         txtcity.Text = strcity(strIndex).ToString()
  19.  
  20.     End Sub
  21. End Class
The problem was that SelectedIndex is String in your code, but actually is an Integer
Apr 9 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.