Solved

Deleting Info From Write File

Posted on 2004-10-01
6
187 Views
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
[CODE]
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)
Close
TotalWords = TotalWords + 1

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

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?
0
Comment
Question by:deathman5
6 Comments
 
LVL 3

Accepted Solution

by:
sgayatri earned 40 total points
Comment Utility
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

0
 

Author Comment

by:deathman5
Comment Utility
Well I thought of that, but it doesn’t work in my case
This is the .dat file

“server1”,”port1”
“server2”,”port2”
“server3”,”port3”

In the list box I have:

Server1
Server2
Server3

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…
0
 
LVL 3

Assisted Solution

by:sgayatri
sgayatri earned 40 total points
Comment Utility
Well,
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.



0
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 30 total points
Comment Utility
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.
0
 
LVL 1

Assisted Solution

by:VBtorment
VBtorment earned 30 total points
Comment Utility
Hai,

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
    Loop
Close

'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%
Close


hope this helps
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now