|
-
Feb 23rd, 2008, 12:12 PM
#1
Thread Starter
New Member
How to get text into list box?
Hi i have some text stored in varible which i want to put in a listbox but im having problems getting it to work.
here is my code so far
Code:
Public Class Form5
Dim fname1 As String
Dim sname1 As String
Dim money1 As Decimal
Dim fname2 As String
Dim sname2 As String
Dim money2 As Decimal
Dim fname3 As String
Dim sname3 As String
Dim money3 As Decimal
Dim fname4 As String
Dim sname4 As String
Dim money4 As Decimal
Dim fname5 As String
Dim sname5 As String
Dim money5 As Decimal
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim RT As New System.IO.StreamReader("c:\Test.txt")
fname1 = RT.ReadLine()
sname1 = RT.ReadLine()
money1 = RT.ReadLine()
fname2 = RT.ReadLine()
sname2 = RT.ReadLine()
money2 = RT.ReadLine()
fname3 = RT.ReadLine()
sname3 = RT.ReadLine()
money3 = RT.ReadLine()
fname4 = RT.ReadLine()
sname4 = RT.ReadLine()
money4 = RT.ReadLine()
fname5 = RT.ReadLine()
sname5 = RT.ReadLine()
money5 = RT.ReadLine()
ListBox1.Text = fname1 + sname1
End Sub
End Class
i am just trying to get fname1 and sname1 to show up first then i will put fname and sname2 on the next line in the listbox hopefully.
thanks
-
Feb 23rd, 2008, 12:15 PM
#2
Re: How to get text into list box?
ListBox1.items.add(fname1 & sname1)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 23rd, 2008, 12:21 PM
#3
Re: How to get text into list box?
You'd add them to the listbox like this:
VB.Net Code:
ListBox1.Items.Add(fname1 & fname2)
However may I suggest using a structure like this:
VB.Net Code:
Private Structure YourStructure
Dim fname As String
Dim sname As String
Dim money As Decimal
End Structure
Private instances(4) As YourStructure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim RT As New System.IO.StreamReader("c:\Test.txt")
For i As Integer = 0 to 4
instances(i).fname = RT.ReadLine()
instances(i).sname = RT.ReadLine()
instances(i).money = Convert.ToDecimal(RT.ReadLine())
Next i
'Add to listbox here
End Sub
But of course, change 'YourStructure' and 'instances' to have better names.
Also, turn Option Strict ON.
-
Feb 23rd, 2008, 12:56 PM
#4
Thread Starter
New Member
Re: How to get text into list box?
ok thanks,
One other problem i have just come across is that im using VB 2008 express and at uni we have VB 2005 express i have just been informed and having just downloaded express 2005 my 2008 project file won't open in 2005 so can i save my project as an older version some how? or is there way to convert it? if not looks like i will have to re-do it in 2005.
thanks
-
Feb 23rd, 2008, 12:58 PM
#5
Re: How to get text into list box?
I dont know if you'll be able to open it in 2005, but try going into the project properties and setting the target framework to 2.0, then save and try to open it in 2005. Though I think you'll have to re-do it in VB2005.
-
Feb 23rd, 2008, 01:04 PM
#6
Thread Starter
New Member
Re: How to get text into list box?
 Originally Posted by Atheist
I dont know if you'll be able to open it in 2005, but try going into the project properties and setting the target framework to 2.0, then save and try to open it in 2005. Though I think you'll have to re-do it in VB2005.
would i be able to just import all my forms from my 2008 project into a new project in 2005?
-
Feb 23rd, 2008, 01:13 PM
#7
Re: How to get text into list box?
You could try it. 
If you cant, it wont be too much work just copying over the code and re-constructing the forms from scratch.
-
Feb 24th, 2008, 12:39 PM
#8
Thread Starter
New Member
Re: How to get text into list box?
 Originally Posted by .paul.
ListBox1.items.add(fname1 & sname1)
Ok thanks for all the help i've got most of it working now. I did have to re do it in 2005 but i could copy the code and copy the objects from 2008 into 2005. Now i another simple question about the listbox how do i put a space between fname and sname when it appears in the listbox?
-
Feb 24th, 2008, 12:44 PM
#9
Re: How to get text into list box?
 Originally Posted by kuliand
Ok thanks for all the help i've got most of it working now. I did have to re do it in 2005 but i could copy the code and copy the objects from 2008 into 2005. Now i another simple question about the listbox how do i put a space between fname and sname when it appears in the listbox?
VB.Net Code:
ListBox1.Items.Add(fname1 & " " & sname1)
-
Feb 24th, 2008, 12:46 PM
#10
Re: How to get text into list box?
ListBox1.Items.Add(fname1 & " " & fname2)
Looks kind of silly, doesn't it? Yet it's the easiest way to do it. Alternatively, you could append in the space character, which is a good idea if you are using more than one space, since you will have a hard time telling the difference between " " and " ".
My usual boring signature: Nothing
 
-
Feb 24th, 2008, 12:46 PM
#11
Re: How to get text into list box?
My usual boring signature: Nothing
 
-
Feb 24th, 2008, 12:52 PM
#12
Thread Starter
New Member
Re: How to get text into list box?
thanks both of you i was nearly this time but i was putting the quotes after the & symbol without the second & symbol.
-
Feb 24th, 2008, 01:04 PM
#13
Re: How to get text into list box?
That's one of my pet peeves about the language. If you write:
var=varA+varB
in both cases, VS will add correct spaces to result in
var = varA + varB
But not if you are using the & symbol, then you just get errors.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|