Results 1 to 3 of 3

Thread: splitting a string into 2?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    18

    Unhappy 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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Talking

    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:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.    Dim ProductSplit() As String
    3.    'Dim sw As StreamWriter
    4.    Dim fileName As String = "E:\...ProductList.txt"
    5.  
    6.    If (File.Exists(fileName)) Then
    7.       Dim sr As StreamReader= File.OpenText(fileName)
    8.       Do While (sr.Peek <> -1)
    9.          LineOfProduct = sr.ReadLine
    10.          ProductSplit = String.Split(LineOfProduct, ",")
    11.    
    12.          cobProduct.Items.Add(ProductSplit(0))
    13.          lblUnitPrice.Text = ProductSplit(1)
    14.       Loop
    15.       sr.Close
    16.    Else
    17.       MsgBox("file does not exist")
    18.    End If
    19. 'sw.writerLine(fileName)
    20. 'sw.Close()

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    As for a class this should get you started:
    VB Code:
    1. Public Class Product
    2.   Private _ProductName as string
    3.   Private _UnitPrice as Single
    4.   Private _Quanity as Short
    5.  
    6.   Public Property ProductName as string
    7.     Get
    8.         return _ProductName
    9.     End Get
    10.     Set(Value as String)
    11.         _ProductName=Value
    12.     End Set
    13.   End Property
    14.  
    15.   Public Property UnitPrice as Single
    16.     Get
    17.         return _UnitPrice
    18.     End Get
    19.     Set(Value as Single)
    20.         _UnitPrice =Value
    21.     End Set
    22.   End Property
    23.  
    24.   Public Property Quanity as Short
    25.     Get
    26.         return _Quanity
    27.     End Get
    28.     Set(Value as Short)
    29.         _Quanity =Value
    30.     End Set
    31.   End Property
    32.  
    33.    Public Sub New()
    34.       MyBase.New
    35.    End Sub
    36.  
    37.    Public Sub New(Name as string,UnitPrice as single)
    38.      _ProductName=Name
    39.      _UnitPrice=UnitPrice
    40.      _Quanity=0
    41.   End Sub
    42.  
    43.    Public Sub New(Name as string,UnitPrice as single,Quanity as short)
    44.      _ProductName=Name
    45.      _UnitPrice=UnitPrice
    46.      _Quanity=Quanity
    47.   End Sub
    48.  
    49. End Class
    50.  
    51. 'then instead of loading the individual data you'd load it into a class and bind the class to the different UI parts.
    52.  
    53. Private Products as ArrayList
    54.  
    55. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    56.    Dim ProductSplit() As String
    57.    'Dim sw As StreamWriter
    58.    Dim fileName As String = "E:\...ProductList.txt"
    59.  
    60.    If (File.Exists(fileName)) Then
    61.       Dim sr As StreamReader= File.OpenText(fileName)
    62.       Do While (sr.Peek <> -1)
    63.          LineOfProduct = sr.ReadLine
    64.          ProductSplit = String.Split(LineOfProduct, ",")
    65.    
    66.          dim itm as New Product(ProductSplit(0),CType(ProductSplit(1),Single))
    67.          Products.Add(itm)
    68.       Loop
    69.       sr.Close
    70.       cobProduct.DataSource=Products
    71.       cobProduct.DisplayMember="ProductName"
    72.       lblUnitPrice.Text=Products(0).UnitPrice
    73.    Else
    74.       MsgBox("file does not exist")
    75.    End If
    76. 'sw.writerLine(fileName)
    77. 'sw.Close()
    78.  
    79.  
    80. [color=red]THEN IN THE COMBO CHANGE EVENT YOU CAN SET THE OTHER ITEMS EASILY[/color]
    81.     Private Sub cobProduct_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cobProduct.SelectionChangeCommitted
    82.         If Not cobProduct.SelectedItem Is Nothing Then
    83.            dim cur as Product=CType(cobProduct.SelectedItem,Product)
    84.            lblUnitPrice.Text=cur.UnitPrice
    85.         End If
    86.     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
  •  



Click Here to Expand Forum to Full Width