Deleting Info From Write File

Posted on 2004-10-01
Medium Priority
Last Modified: 2010-05-02
Im working on an IRC program, Im trying now to save the servers, so the user wont have to remember them all
I have a list box, I put inside it the server names that are saved using this code
Text1 = Server
Text2 = Port
Dim Ser(2500) As String
Dim Por(2500) As String
Dim TotalWords As Integer

Private Sub Command6_Click()
'How To Save
Ser(TotalWords) = Text1.Text
Por(TotalWords) = Text2.Text
Open "Servers.dat" For Append As #1
    Write #1, Ser(TotalWords), Por(TotalWords)
TotalWords = TotalWords + 1

'How To Load
TotalWords = 0
Open "Servers.dat" For Input As #1
    Do Until EOF(1) = True
        Input #1, Ser(TotalWords), Por(TotalWords)
        TotalWords = TotalWords + 1
Dim x As Integer
For x = 0 To TotalWords - 1
    List1.AddItem Ser(x)
Next x
End Sub

Ok the problem is, I want to make the user able to remove some servers by highlighting them on the list and clicking command button, but then I have to remove them from the file it self and I must not keep any blank lines or spaces in the file (need that for another thing)
So how to do that?
Question by:deathman5

Accepted Solution

sgayatri earned 120 total points
ID: 12198313
The logic yu have to use is :
1. Whenever an item in the list box is clicked,
and delete button is pressed,
save the listindex of the item clicked.
2. delete that item from the list
3. create a temporary file servers_temp.dat
4. Read servers.dat line by line and copy each line to servers_temp.dat till you reach the line to be deleted
(keep incrementing a counter for each line)
5. When you reach the line to be deleted, skip that line without writing into servers_temp.dat
6. Copy all lines from servers.dat to servers_temp.dat
7. delete servers.dat
8. rename servers_temp.dat to servers.dat

Hope I am clear.
Shall try to post thecode , but need some time


Author Comment

ID: 12198613
Well I thought of that, but it doesn’t work in my case
This is the .dat file


In the list box I have:


The port is not shown, so I can’t copy from list box and paste in another file, this would only copy the server; I need to copy the port too…

Assisted Solution

sgayatri earned 120 total points
ID: 12198683
I never told you to copy from list box.
All you have to do is , note the "LISTINDEX"
whenever a delete is done

Add a command button for delete,

In command1_click
msgbox list1.listindex+1
list_index_number = list1.listindex+1

will give you the number of the line clicked.

use list_index_number  to delete that number line in your file.

LVL 76

Assisted Solution

by:David Lee
David Lee earned 90 total points
ID: 12198762
Why store the server names twice, once in the Ser array and again in the ListBox?  Instead, I'd create a second ListBox for the ports and set its Visible property to false.  Eliminate the Ser and Por arrays and read the data directly into the two ListBoxes.  When the user deletes one of the servers, you delete the values from both ListBox controls at the same time.  Something like:

    Private Sub DeleteServer()
            PorList.RemoveItem (SerList.ListIndex)
            SerList.RemoveItem (SerList.ListIndex)
    End Sub

We remove the entry from the Ports ListBox first because when we remove the entry from the Servers ListBox its ListIndex will change.

This will keep the two lists in sync.  Now when you exit the program all you need do is loop through the Servers ListBox and use its ListIndex value to get the corresponding Ports value and write them to the file.  Not only does this make your coding simpler, it also eliminates storing the same data twice, once in the array and again in the ListBox which will make your program smaller and faster.

Assisted Solution

VBtorment earned 90 total points
ID: 12199912

try this

on delete just do: Ser(index)=""
then save complete list, not just delete one from list...

'Load is good
Open "Servers.dat" For Input As #1
    Do Until EOF(1) = True
        Input #1, Ser(TotalWords), Por(TotalWords)
        TotalWords = TotalWords + 1

'for Saving try this
kill "Servers.dat"
Open "Servers.dat" For Append As #1
    for i%=1 to TotalWords
        if Ser(i%)<>"" then
            Write #1, Ser(i%), Por(i%)
        end if
    next i%

hope this helps

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question