|
-
Nov 27th, 2002, 05:15 PM
#1
Thread Starter
Junior Member
splitting a string into 2?
hi...i've managed to upload a .txt file into a comboBox and now i'm trying split each line into 2 ,1st part of line goes to comboBox where the 2nd part goes to lbl.text....so far , i can get 1st part shown in comboBox but the 2nd part nowhere to be found..here the code~:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ProductSplit(2) As String
Dim sw As StreamWriter
Dim sr As StreamReader
Dim fileName As String = "E:\...ProductList.txt"
Dim i As Integer
If (File.Exists(fileName)) Then
sr = File.OpenText(fileName)
Do While (sr.Peek <> -1)
LineOfProduct = sr.ReadLine
ProductSplit = Split(LineOfProduct, ",")
cobProduct.Items.Add(ProductSplit(0))
lblUnitPrice.Text = ProductSplit(1)
Loop
Else
MsgBox("file does not exist")
End If
sw.writerLine(fileName)
sw.Close()
also there are 2 other things i'd like to help me out with plezase
1- an error on sw.writerLine(fileName) it says writerLine is not part of system.io.steamwriter.??
2- how do u get indexes or items selected when u select one of them in 2 or 3 listboxes, for instance u want to remove an item u select it, and automaticly its quantity and price get selected as well??
Thank u people in advance..u've been great
-
Nov 27th, 2002, 06:30 PM
#2
Is your code giving you an error? If so where at?
Also you shouldn't specific the number of elements of the array if you are going to use it with split, change:
Dim ProductSplit(2) As String
TO:
Dim ProductSplit() As String
As for Q#2 there is no WriterLine there is a WRITELINE (no R). Although you haven't declared an instance of the streamwriter or given it a stream to work with. What is this part suppose to do?
As for syncing up multiple listboxs you should put the data in to a class and then all of it will be in one place and easier to sync.
Try something like this:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ProductSplit() As String
'Dim sw As StreamWriter
Dim fileName As String = "E:\...ProductList.txt"
If (File.Exists(fileName)) Then
Dim sr As StreamReader= File.OpenText(fileName)
Do While (sr.Peek <> -1)
LineOfProduct = sr.ReadLine
ProductSplit = String.Split(LineOfProduct, ",")
cobProduct.Items.Add(ProductSplit(0))
lblUnitPrice.Text = ProductSplit(1)
Loop
sr.Close
Else
MsgBox("file does not exist")
End If
'sw.writerLine(fileName)
'sw.Close()
-
Nov 27th, 2002, 06:40 PM
#3
As for a class this should get you started:
VB Code:
Public Class Product
Private _ProductName as string
Private _UnitPrice as Single
Private _Quanity as Short
Public Property ProductName as string
Get
return _ProductName
End Get
Set(Value as String)
_ProductName=Value
End Set
End Property
Public Property UnitPrice as Single
Get
return _UnitPrice
End Get
Set(Value as Single)
_UnitPrice =Value
End Set
End Property
Public Property Quanity as Short
Get
return _Quanity
End Get
Set(Value as Short)
_Quanity =Value
End Set
End Property
Public Sub New()
MyBase.New
End Sub
Public Sub New(Name as string,UnitPrice as single)
_ProductName=Name
_UnitPrice=UnitPrice
_Quanity=0
End Sub
Public Sub New(Name as string,UnitPrice as single,Quanity as short)
_ProductName=Name
_UnitPrice=UnitPrice
_Quanity=Quanity
End Sub
End Class
'then instead of loading the individual data you'd load it into a class and bind the class to the different UI parts.
Private Products as ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ProductSplit() As String
'Dim sw As StreamWriter
Dim fileName As String = "E:\...ProductList.txt"
If (File.Exists(fileName)) Then
Dim sr As StreamReader= File.OpenText(fileName)
Do While (sr.Peek <> -1)
LineOfProduct = sr.ReadLine
ProductSplit = String.Split(LineOfProduct, ",")
dim itm as New Product(ProductSplit(0),CType(ProductSplit(1),Single))
Products.Add(itm)
Loop
sr.Close
cobProduct.DataSource=Products
cobProduct.DisplayMember="ProductName"
lblUnitPrice.Text=Products(0).UnitPrice
Else
MsgBox("file does not exist")
End If
'sw.writerLine(fileName)
'sw.Close()
[color=red]THEN IN THE COMBO CHANGE EVENT YOU CAN SET THE OTHER ITEMS EASILY[/color]
Private Sub cobProduct_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cobProduct.SelectionChangeCommitted
If Not cobProduct.SelectedItem Is Nothing Then
dim cur as Product=CType(cobProduct.SelectedItem,Product)
lblUnitPrice.Text=cur.UnitPrice
End If
End Sub
Last edited by Edneeis; Nov 27th, 2002 at 06:47 PM.
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
|