For and next loop... problem
-
Hi, I am trying to insert selected items from a listbox into a label, the listbox is fillby a database query, the problem is all I get is this
System.Data.DataRowView
it works fine when I dont use a database query and just text in the listbox, but need to use a database query. Can anyone help on a solution for this??Dim i As Integer For i = 0 To ListBoxUpgrade.Items.Count - 1 If ListBoxUpgrade.Items(i) Then LabelSum.Text = LabelSum.Text & Space(6) & ListBoxUpgrade.SelectedItem(i) & vbCrLf End If Next i
Thanks in advance! Stefan."All answers have a question? All code has an end."
-
Hi, I am trying to insert selected items from a listbox into a label, the listbox is fillby a database query, the problem is all I get is this
System.Data.DataRowView
it works fine when I dont use a database query and just text in the listbox, but need to use a database query. Can anyone help on a solution for this??Dim i As Integer For i = 0 To ListBoxUpgrade.Items.Count - 1 If ListBoxUpgrade.Items(i) Then LabelSum.Text = LabelSum.Text & Space(6) & ListBoxUpgrade.SelectedItem(i) & vbCrLf End If Next i
Thanks in advance! Stefan."All answers have a question? All code has an end."
smguc wrote:
ListBoxUpgrade.SelectedItem(i)
try ListBoxUpgrade.SelectedItem(i).Text instead without .Text the ToString() method gets called, which by default returns a string naming the type. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
smguc wrote:
ListBoxUpgrade.SelectedItem(i)
try ListBoxUpgrade.SelectedItem(i).Text instead without .Text the ToString() method gets called, which by default returns a string naming the type. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Hi Luc, Thanks for that, but doesn't accept the .text after the
ListBoxUpgrade.SelectedItem(i)
so frustrading it works in 'VB6' which is where I have upgraded from to 'VB2005' all because of 'Vista', many different rules and changes. What I am trying to achieve, is there is a list of items in a listbox which you can multi select and from there show only those selected items in a label as a summary 'like a report'. Any more suggestions would be much appreciated! Thanks, Stefan."All answers have a question? All code has an end."
-
Hi Luc, Thanks for that, but doesn't accept the .text after the
ListBoxUpgrade.SelectedItem(i)
so frustrading it works in 'VB6' which is where I have upgraded from to 'VB2005' all because of 'Vista', many different rules and changes. What I am trying to achieve, is there is a list of items in a listbox which you can multi select and from there show only those selected items in a label as a summary 'like a report'. Any more suggestions would be much appreciated! Thanks, Stefan."All answers have a question? All code has an end."
I missed that you missed an 's' try ListBoxUpgrade.SelectedItems(i).Text instead :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
I missed that you missed an 's' try ListBoxUpgrade.SelectedItems(i).Text instead :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Hmmm... No still no luck! Doesn't like the .text after it. Sometimes makes me think I should go back to VB6, haha. Was like my second lanugage! This is the code for VB6, which works fine the only difference is that the 'i' was declare as a variant, but VB2005 wants it as a object, which didn't work so changed it to a Integer.
Dim i As Variant For i = 0 To ListBoxUpgrade.ListCount - 1 If ListBoxUpgrade.Selected(i) Then LabelSum = LabelSum & Space(6) & ListBoxUpgrade.List(i, 3) & vbCrLf End If Next i
Do you have any more ideas or a better way to write the code?? Or go back to VB6... lol Thanks, Stefan."All answers have a question? All code has an end."
-
Hmmm... No still no luck! Doesn't like the .text after it. Sometimes makes me think I should go back to VB6, haha. Was like my second lanugage! This is the code for VB6, which works fine the only difference is that the 'i' was declare as a variant, but VB2005 wants it as a object, which didn't work so changed it to a Integer.
Dim i As Variant For i = 0 To ListBoxUpgrade.ListCount - 1 If ListBoxUpgrade.Selected(i) Then LabelSum = LabelSum & Space(6) & ListBoxUpgrade.List(i, 3) & vbCrLf End If Next i
Do you have any more ideas or a better way to write the code?? Or go back to VB6... lol Thanks, Stefan."All answers have a question? All code has an end."
Dont go back to VB6, if anything show your current VB.NET code here. And tell exactly what the current result is and what it should be. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi, I am trying to insert selected items from a listbox into a label, the listbox is fillby a database query, the problem is all I get is this
System.Data.DataRowView
it works fine when I dont use a database query and just text in the listbox, but need to use a database query. Can anyone help on a solution for this??Dim i As Integer For i = 0 To ListBoxUpgrade.Items.Count - 1 If ListBoxUpgrade.Items(i) Then LabelSum.Text = LabelSum.Text & Space(6) & ListBoxUpgrade.SelectedItem(i) & vbCrLf End If Next i
Thanks in advance! Stefan."All answers have a question? All code has an end."
The listbox contains objects which could be anything. ListBoxUpgrade.SelectedItem(i) will return whatever text the ToString method returns. For most objects this is the type of object it is. Because you've bound your listview you actually have a listview full of DataRowView objects. This is why the ToString method is returning System.Data.DataRowView. If you want to get the corresponding text of the item (the text being displayed in the listbox) you can use the GetItemText method.
For Each obj As Object In ListBox1.SelectedItems Console.WriteLine(ListBox1.GetItemText(obj)) Next
-
The listbox contains objects which could be anything. ListBoxUpgrade.SelectedItem(i) will return whatever text the ToString method returns. For most objects this is the type of object it is. Because you've bound your listview you actually have a listview full of DataRowView objects. This is why the ToString method is returning System.Data.DataRowView. If you want to get the corresponding text of the item (the text being displayed in the listbox) you can use the GetItemText method.
For Each obj As Object In ListBox1.SelectedItems Console.WriteLine(ListBox1.GetItemText(obj)) Next
Hi, that looks great. Never knew VB code could look so clean. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
The listbox contains objects which could be anything. ListBoxUpgrade.SelectedItem(i) will return whatever text the ToString method returns. For most objects this is the type of object it is. Because you've bound your listview you actually have a listview full of DataRowView objects. This is why the ToString method is returning System.Data.DataRowView. If you want to get the corresponding text of the item (the text being displayed in the listbox) you can use the GetItemText method.
For Each obj As Object In ListBox1.SelectedItems Console.WriteLine(ListBox1.GetItemText(obj)) Next
Excellent! Thank you very much! You would not believe the amount of books I have, and read that doesn't even state the 'GetItemText' method, Have only just made the transistion in VB2005 a few weeks now. Great help! Thanks again.:) Stefan.
"All answers have a question? All code has an end."
-
Excellent! Thank you very much! You would not believe the amount of books I have, and read that doesn't even state the 'GetItemText' method, Have only just made the transistion in VB2005 a few weeks now. Great help! Thanks again.:) Stefan.
"All answers have a question? All code has an end."
That's what IntelliSense is for :) Also the MSDN library is a big help.