Question Subtracting a value from a text file va a button press

Jonnystudent

New member
Joined
Aug 23, 2010
Messages
1
Programming Experience
Beginner
Hi Guys and Gyals. I'm a newbie here so please be gentle. I'm in the process of completing a project but I'm finding it difficult to accomplish one thing. I have built a program in where I have to add and remove items from two listboxes. Listbox1 consists of a file which I have picked up on my C:/. Within this file, are different lines of items. At the end of the item line is a figure. I have a textbox in where when someone adds the an item from listbox1 to listbox2. On clicking the add button, it will add the total which was in the file. If I add more items, it will give me a running total of the items I have added to listbox2.

Now, I'm bamboozled in how to subtract the items from listbox2 with the remove button. So that when a user wants to remove an item that they currently placed in Listbox2. On pressing remove it will subtract the running total from the item which has been removed..

Here is my code in where I add products to the textbox. I have two other text boxes which I use to have these running totals. FYI, Textbox where value is outputted is Textbox5. Textbox6 is used to refresh the data and the other textbox is named 'Total'.

VB.NET:
    Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If ListBox1.SelectedIndex < 0 Then
            MsgBox("Please select a Batch")
        Else : TextBox5.Text = ListBox1.SelectedItem
            Dim obWriter As New System.IO.StreamWriter("C:\Scheduler\Batchsort\Len1.txt")
            obWriter.Write(TextBox5.Text)
            obWriter.Close()
            TextBox5.Clear()
            Dim FILE_NAME2 As String = "C:\Scheduler\Batchsort\Len1.txt"
            Dim so As New System.IO.StreamReader(FILE_NAME2)
            Dim Len As String
            Len = so.ReadLine

            Dim length = Len.Substring(29, 6)
            ListBox2.Items.Add(ListBox1.SelectedItem)

            Dim Total = Len.Substring(29, 6)
            running_tot_length = running_tot_length + Val(Total)
            TextBox5.Text = running_tot_length
            TextBox6.Refresh()

            so.Close()

        End If

        ListBox1.Items.Remove(ListBox1.SelectedItem)

    End Sub

Any help would be greatly appreciated.

Cheers,

Jonah
 
my approach would be to not write anything into the text file until everything was done - do you need to write to the text file everytime or can you do this at the end somehow?

i would create a class that was whatever fields are in your text file and add / remove those entries from the listboxes as the user decide.

example:

VB.NET:
public MyFileObjectList1 as system.collections.generic List(of MyFileObject) = nothing
public MyFileObjectList2 as system.collections.generic List(of MyFileObject) = nothing

Public Class MyFileObject

private _Field1 as string=string.empty
public property Field1() as string
get
    return me._Field1
end get
set (value as string)
    me._Field1 = value
end set

private _Field2 as string=string.empty
public property Field2() as string
get
    return me._Field2
end get
set (value as string)
    me._Field2 = value
end set

''  add more fields depending on what your text file data is hold
private _Total as Integer=0
public property Total() as string
get
    return me._Total
end get
set (value as Integer)
    me._Total = value
end set


Public Sub New(byval strField1 as string,byval strField1 as string,byval iTotal as Integer)

    ''  i like to build constructors that take arguments to fill values
    me._Field1=strField1
    me._Field2=strField2
    me._Total=iTotal

end Sub

end Class

public sub ReadTextFile()

''  create a streamreader or whatever file reader you prefer
''  and then for each line parse it out into a new instance of that class

while not(myStreamReader.EndofStream)
    dim s as string=myStreamReader.readline()

''  this assumes the file is fixed length and field1 is the 1st 10 characters, field2 is the next 10 characters, and total is the next 10 characters (converted to an interger)
    dim item as new MyFileObject(s.substring(0,10),s.substring(10,10),cint(s.substring(20,10)

MyFileObjectList1.add(item) 
end while


me.listbox1.datasource=MyFileObjectList1
me.listbox1.displaymember="Field1"  '' can display whatever you want from the class
end sub

when you want to move am item from list 1 to list 2, simply find that item in the collection and add it to the other collection, then remove it form the source one

VB.NET:
public sub AddItemToListbox2FromListbox1()

dim obj as MyFileObject=ctype(listbox1.selecteditem,MyFileObject)
MyFileObjectList2.add(obj)
MyFileObjectList1.remove(obj)

end sub

public sub AddItemToListbox1FromListbox2()

dim obj as MyFileObject=ctype(listbox2.selecteditem,MyFileObject)
MyFileObjectList2.add(obj)
MyFileObjectList1.remove(obj)

end sub

public sub ShowListbox2Total()

dim iTotal as integer=0
for i as integer=0 to listbox2.items.count-1
    iTotal +=ctype(listbox2.selecteditem,MyFileObject).Total
next i

TextBox5.Text = iTotal

end sub

this way you can read the textfile when the app starts, create a bunch of your class objects with data, and move them around, add/subtract all you want, and then when all that is done you can write them out to your text file.


this would give you complete control over your data, and stop you from having to do alot of read/write from your data files.


sorry this code was not written in VB.NET's IDE, i just wrote it by hand so by no means is it perfect - i hope though it gives you a good idea of what i am saying.
 
Back
Top