Delete Button
Delete Button
Delete Button
Delete Button
Delete Button
Delete Button Delete Button Delete Button Delete Button Delete Button Delete Button Delete Button Delete Button
Delete Button Delete Button
Delete Button
Go Back  Xtreme Visual Basic Talk > > > Delete Button


Reply
 
Thread Tools Display Modes
  #1  
Old 12-03-2010, 08:48 PM
DarylN DarylN is offline
Newcomer
 
Join Date: Dec 2010
Posts: 1
Default Delete Button


Hey guys:

I am trying to code a delete button for a browsing program based on data from a txt file. I have coded the buttons to sort(ascending and descending), append to the text file, search the text file, but I can not seem to get the delete function working. I am using VB 2010, this is what I have so far:
Code:
Dim deleline As String
        Dim line As String
        Dim isFound As Boolean = False
        sr4 = IO.File.OpenText("Students.txt")
        sw = IO.File.CreateText("Testing.txt")
        sw = IO.File.CreateText("Temp.txt")
        deleline = sr4.ReadLine()
        Dim fn, ln, major, phone, mail, gpa As String
        fn = txtFirstName.Text.Substring(0, 1).ToUpper & txtFirstName.Text.Substring(1)
        ln = txtLastName.Text.Substring(0, 1).ToUpper & txtLastName.Text.Substring(1)
        major = txtMajor.Text.Substring(0, 1).ToUpper & txtMajor.Text.Substring(1)
        phone = txtPhone.Text
        mail = txtMail.Text
        gpa = txtGpa.Text
        line = fn & " " & ln & " " & major & " " & phone & " " & mail & " " & gpa
        For i = 0 To sr4.Peek - 1
            If isFound = False Then
                If deleline = line Then
                    sw.WriteLine(n)
                ElseIf isFound = True Then
                    n += 1
                End If
            End If
            n += 1
        Next
        sr4.Close()

        sw.Close()

        IO.File.Delete("Students.txt")
        IO.File.Move("Testing.txt", "Students.txt")
    End Sub
any help or tips will be appreciated thanks

Last edited by passel; 12-05-2010 at 04:22 PM. Reason: Added code tags
Reply With Quote
  #2  
Old 12-04-2010, 12:15 PM
snarfblam's Avatar
snarfblamDelete Button snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

Quote:
Originally Posted by DarylN View Post
I can not seem to get the delete function working.
You aren't explaining the problem. Is there an error? Also, it would help if you used [Code] tags for readability and maybe comment the code or remove bits irrelevant to the question.
__________________
C# _VB.NET _
Reply With Quote
  #3  
Old 12-05-2010, 05:08 PM
passel's Avatar
passelDelete Button passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

I'm not sure what you're doing.
To delete a line from a file I would expect somthing along the lines of
Code:
Open the file for input
Open a temp file for output
Do until no lines left in the file
  Read a line from the file
  If the line is not the one we want deleted then
    write the line to the temp file
  End if
Loop
Close the files
Copy the temp file to the file.
Now, the code you have currently, looks like it is trying to debug your logic, not do the task.
The code looks like it would wipe out your input file, since you open a second file using sw "Temp.txt" and write to that, and then copy "Testing.txt" to your input file which wasn't written to.

In any case, there seems to be a number of obvious flaws in the code logic, regardless of what you intended.

You have a condition, If isFound = False, then inside that condition you test for isFound = True. isFound can never be True since it had to be False to get to that code.

Your loop, For i = 0 to sr4.Peek-1, might work, I haven't tried it, but I wouldn't do that.
Peek is a function which should return the byte offset of the next character to be read from the file. You're not reading new lines from the file in your loop, so the Peek function should never return a different number, so will loop some arbitrary number of times (the length of the first line read from the file + 1), but even if you were reading lines in the loop, I would be surprised if it didn't end up not reading the whole file, or reading past the end of the file.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Delete Button
Delete Button
Delete Button Delete Button
Delete Button
Delete Button
Delete Button Delete Button Delete Button Delete Button Delete Button Delete Button Delete Button
Delete Button
Delete Button
 
Delete Button
Delete Button
 
-->