Results 1 to 21 of 21

Thread: [RESOLVED] Deleting & Altering File Data

  1. #1

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Resolved [RESOLVED] Deleting & Altering File Data

    I got a empty file following the ANSI encoding and I want to make a bookmark library(kinda useless, but it tests my knowledge of programming). At first there will be no links at all and there's a button that will lead to a form that prompts you for the name of the site and URL. Then it'll add it to the file. Then on the main form, it'll will pop up (without needing to restart program.)
    Ex-Youtube
    www.youtube.com

    File- Youtube www.youtube.com


    But then you also need the function of deleting them.
    File- Google www.google.com
    Youtube www.youtube.com (Delete this one)
    Yahoo www.yahoo.com

    File- Google www.google.com
    Yahoo www.yahoo.com (fill in the blank line)

    Now for the question, how would you do it? (lol, sorry for asking such a question, but I kinda depressed since I spent around 2 hours trying to figure it out.)
    "When ideas and actions fuse, they form creations"

  2. #2
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Deleting & Altering File Data

    I would use a ListView for the Bookmark names and second column contains the URL. The name is the Title of page.

    A comma delimited file/db.
    Reading with StreamReader, writing with StreamWriter.

    This should spark something

  3. #3

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Re: Deleting & Altering File Data

    If its going to be two columns then how do you tell it to break each line into two with the comma?
    Also could you give of a example of how to use stream writer? ( is it the same way as using stream reader but defining what is being entering?)
    "When ideas and actions fuse, they form creations"

  4. #4
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Deleting & Altering File Data

    Break into two because each line has a name/Title separated by a SPACE " " and then the URL.
    So, just as you Split at the comma, you now SPLIT at the Space.
    So create Listview ITEM and a SUbItem [title/URL] and add them while working on the same line you are reading.

    Yes, StreamWriter is the Object used to write to the file.
    There are oodles of examples here and elsewhere.

    Good Luck!

  5. #5

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Re: Deleting & Altering File Data

    So the file will be like this?

    Youtube,www.youtube.com
    Google,www.google.com
    Yahoo,www.yahoo.com

    when I try to get the listbox to = BMF(variable for my file), it gives me this error

    Error 1 Value of type '1-dimensional array of String' cannot be converted to 'String'. C:\Users\Blank\Desktop\Bookmark Library\Bookmark Library\Form4.vb 14 25 Bookmark Library
    Last edited by Brinith; Apr 1st, 2012 at 08:31 AM.
    "When ideas and actions fuse, they form creations"

  6. #6
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Deleting & Altering File Data

    How do you declare BMF?
    The Listbox gets filled from your bookmarks textfile, right?

    vb Code:
    1. Dim R As New IO.StreamReader(your_bookmarks_textfile)
    2.             Dim BMF As String() = R.ReadToEnd().Split(New String(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries))
    3.      
    4.             ListBox1.Items.AddRange(BMF)
    5.             R.Close()

    edit: I'm only guessing.
    I will make a bookmark text file in that format you posted.
    I will make a mock program.
    Give me a few hours because I am not on the machine with the IDE.
    Last edited by proneal; Apr 1st, 2012 at 10:00 AM.

  7. #7

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Re: Deleting & Altering File Data

    If you are really that busy then you don't have to, but if you do thanks

    EDIT: I got the streamreader part to work, but it won't split the line. Is there a specific character you must use to break it?
    Also I am now trying to get the streamwriter to work, but I cant seem to get it to work
    Public Class Form5
    Dim BMC As New IO.StreamWriter("C:\Users\Allen Liu\Desktop\BM.varia")
    Dim BM As String = TextName.Text & TextURL.Text
    Private Sub ButtonAdd_Click(sender As System.Object, e As System.EventArgs) Handles ButtonAdd.Click
    BMC = BM
    End Sub
    End Class

    The error- Value of type 'String' cannot be converted to 'System.IO.StreamWriter'.
    Last edited by Brinith; Apr 1st, 2012 at 12:00 PM.
    "When ideas and actions fuse, they form creations"

  8. #8
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Deleting & Altering File Data

    I wrote a custom class called Bookmark.
    We can fiddle around with an Array that matches up nicely with the Listbox Selected Index..
    A Listview was what I suggested, but Listbox can be used.

    vb Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Public Class Bookmark
    5.  
    6.     Public URL As String
    7.     Public Title As String
    8.  
    9.     Public Overrides Function ToString() As String
    10.         Return Me.Title
    11.     End Function
    12. End Class
    13.  
    14.  
    15. Public Class Form1
    16.     Dim MyBookmarks As String = My.Computer.FileSystem.CurrentDirectory & "/bookmarks.txt"
    17.     Dim BMF As String()
    18.     Dim newBMF As String()
    19.  
    20.     Private Sub FindBookmarks(Optional ByVal GoToTarget As Boolean = False)
    21.         If Not GoToTarget Then
    22.             Me.ListBox1.Items.Clear() 'for testing
    23.         End If
    24.  
    25.         Dim bm_index As Integer = 0
    26.         Dim NewBookmark As New Bookmark
    27.  
    28.         For Each bookmark In BMF
    29.             newBMF = Split(BMF(bm_index), ",")
    30.             NewBookmark.Title = newBMF(0)
    31.             NewBookmark.URL = newBMF(1)
    32.  
    33.             If GoToTarget Then
    34.                 If ListBox1.SelectedIndex = bm_index Then
    35.                     Process.Start(NewBookmark.URL.Trim)
    36.                     Exit Sub
    37.                 End If
    38.             Else
    39.                 Me.ListBox1.Items.Add(NewBookmark)
    40.             End If
    41.  
    42.             bm_index += 1
    43.         Next
    44.  
    45.     End Sub
    46.  
    47.     Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    48.         FindBookmarks(True)
    49.     End Sub
    50.  
    51.     Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    52.         Dim R As New IO.StreamReader(MyBookmarks)
    53.         BMF = R.ReadToEnd().Split(CChar(vbNewLine))
    54.         R.Close()
    55.         FindBookmarks()
    56.     End Sub
    57. End Class

    This example Program will do what you ask.
    remember, it is just a working example -to help spawn ideas -or better code- LOL

    Good Luck!

  9. #9
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Deleting & Altering File Data

    Put the bookmarks text file in your Debug folder.
    It is attached
    Attached Files Attached Files

  10. #10

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Re: Deleting & Altering File Data

    K thx I'll go try it out and see if it works

    EDIT: Is this to write to file or read the file or both?
    "When ideas and actions fuse, they form creations"

  11. #11
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Deleting & Altering File Data

    Quote Originally Posted by Brinith View Post
    K thx I'll go try it out and see if it works

    LOL< I wouldn't post it if it didn't work
    I used a StreamWriter object to create the file, but deleted the code and write button- so I could just select all code and paste in here.

    You have to have that Bookmark.txt file in your debug folder though. That is why I attached it here.
    In real world application, one would not put this in the Form Load event.

  12. #12

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Re: Deleting & Altering File Data

    Okay I placed the code into it and started it up. Everything works but the listbox does not show the link. You can still press it to go to it, just not see it.
    Where do you even learn these things. Is there like a tutorial webstie for something like that because all the ones I used barely covered anything
    Last edited by Brinith; Apr 1st, 2012 at 05:35 PM.
    "When ideas and actions fuse, they form creations"

  13. #13
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Deleting & Altering File Data

    I figured you didn't want to see the links right next to the Title.
    I just play with code until it does what I want.
    Listboxes do not have a TAG property, so I sort of made my own, though it is not really a Tag for the Listbox control.
    It is merely a way to store data while program is running- without persisting.
    I'm working on Add Bookmark Form because I figure that would be the next thing to work on.

    The main objective of the AddBookmark Form is to add the title and URL to the text file.
    Normally we would check to see if that bookmark text file exists or not.
    I will include that portion in the next post that demonstrates adding a bookmark.

  14. #14
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Deleting & Altering File Data

    Ok, for adding bookmark and writing a default file if Bookmarks.txt is not written yet (first time use)
    Add a Windows Form to project. Name it FormAddBookmark
    If you rather do it easy, then copy the code at bottom of this post.
    It is thee designer code. It will create the buttons and labels and correct size.

    This code now will Handle the Load event and show Add Bookmarks Form.
    vb Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Public Class Form1
    5.     Dim MyBookmarks As String = My.Computer.FileSystem.CurrentDirectory & "/bookmarks.txt"
    6.     Dim BMF As String()
    7.     Dim newBMF As String()
    8.     Dim GoogleBookmark As String = "Google," & "www.google.com"
    9.     Private Sub FindBookmarks(Optional ByVal GoToTarget As Boolean = False)
    10.         If Not GoToTarget Then
    11.             Me.ListBox1.Items.Clear() 'for testing
    12.         End If
    13.  
    14.         Dim bm_index As Integer = 0
    15.         Dim NewBookmark As New Bookmark
    16.  
    17.         For Each bookmark In BMF
    18.             newBMF = Split(BMF(bm_index), ",")
    19.             NewBookmark.Title = newBMF(0)
    20.             NewBookmark.URL = newBMF(1)
    21.             If GoToTarget Then
    22.                 If ListBox1.SelectedIndex = bm_index Then
    23.                     Process.Start(NewBookmark.URL.Trim)
    24.                     Exit Sub
    25.                 End If
    26.             Else
    27.                 Me.ListBox1.Items.Add(NewBookmark)
    28.             End If
    29.             bm_index += 1
    30.         Next
    31.  
    32.     End Sub
    33.  
    34.     Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    35.         FindBookmarks(True)
    36.     End Sub
    37.  
    38.     Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    39.         ' check to see if the bookmarks file exists
    40.         ' If not, then create it and add your website URL or default it to Google which is what  I did for this example program
    41.         If Not My.Computer.FileSystem.FileExists(MyBookmarks) Then
    42.             My.Computer.FileSystem.WriteAllText(MyBookmarks, GoogleBookmark, False)
    43.         End If
    44.  
    45.         Dim R As New IO.StreamReader(MyBookmarks)
    46.         BMF = R.ReadToEnd().Split(CChar(vbNewLine))
    47.         R.Close()
    48.         FindBookmarks()
    49.     End Sub
    50.  
    51.     Private Sub CloseButton_Click(sender As System.Object, e As System.EventArgs) Handles CloseButton.Click
    52.         Application.Exit()
    53.     End Sub
    54.  
    55.     Private Sub ButtonAddBookmark_Click(sender As System.Object, e As System.EventArgs) Handles ButtonAddBookmark.Click
    56.         Dim AddBookmark As New FormAddBookmark
    57.         Dim DialogResult As Windows.Forms.DialogResult = AddBookmark.ShowDialog
    58.         If DialogResult = Windows.Forms.DialogResult.OK Then
    59.             MsgBox("BookMarks may have been added!")
    60.         ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
    61.             MsgBox("We have Cancelled Adding Bookmark!")
    62.         Else
    63.             MsgBox("We are not in Kansas anymore TOTO! Space Aliens have come. We should NEVER see this TEXT. NEVER!!!")
    64.         End If
    65.         DialogResult = Nothing
    66.     End Sub
    67. End Class
    68. Public Class Bookmark
    69.     Public URL As String
    70.     Public Title As String
    71.     Public Overrides Function ToString() As String
    72.         Return Me.Title
    73.     End Function
    74. End Class


    Here is what I have for the FormAddBookmark form:
    Just copy and paste into the form module window

    vb Code:
    1. <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    2. Partial Class FormAddBookmark
    3.     Inherits System.Windows.Forms.Form
    4.  
    5.     'Form overrides dispose to clean up the component list.
    6.     <System.Diagnostics.DebuggerNonUserCode()> _
    7.     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    8.         Try
    9.             If disposing AndAlso components IsNot Nothing Then
    10.                 components.Dispose()
    11.             End If
    12.         Finally
    13.             MyBase.Dispose(disposing)
    14.         End Try
    15.     End Sub
    16.  
    17.     'Required by the Windows Form Designer
    18.     Private components As System.ComponentModel.IContainer
    19.  
    20.     'NOTE: The following procedure is required by the Windows Form Designer
    21.     'It can be modified using the Windows Form Designer.  
    22.     'Do not modify it using the code editor.
    23.     <System.Diagnostics.DebuggerStepThrough()> _
    24.     Private Sub InitializeComponent()
    25.         Me.ButtonOK = New System.Windows.Forms.Button()
    26.         Me.ButtonCancel = New System.Windows.Forms.Button()
    27.         Me.TextboxTitle = New System.Windows.Forms.TextBox()
    28.         Me.LabelTitle = New System.Windows.Forms.Label()
    29.         Me.TextboxURL = New System.Windows.Forms.TextBox()
    30.         Me.LabelURL = New System.Windows.Forms.Label()
    31.         Me.SuspendLayout()
    32.         '
    33.         'ButtonOK
    34.         '
    35.         Me.ButtonOK.Location = New System.Drawing.Point(197, 98)
    36.         Me.ButtonOK.Name = "ButtonOK"
    37.         Me.ButtonOK.Size = New System.Drawing.Size(75, 23)
    38.         Me.ButtonOK.TabIndex = 0
    39.         Me.ButtonOK.Text = "&OK"
    40.         Me.ButtonOK.UseVisualStyleBackColor = True
    41.         '
    42.         'ButtonCancel
    43.         '
    44.         Me.ButtonCancel.Location = New System.Drawing.Point(110, 98)
    45.         Me.ButtonCancel.Name = "ButtonCancel"
    46.         Me.ButtonCancel.Size = New System.Drawing.Size(75, 23)
    47.         Me.ButtonCancel.TabIndex = 1
    48.         Me.ButtonCancel.Text = "&Cancel"
    49.         Me.ButtonCancel.UseVisualStyleBackColor = True
    50.         '
    51.         'TextboxTitle
    52.         '
    53.         Me.TextboxTitle.Location = New System.Drawing.Point(99, 17)
    54.         Me.TextboxTitle.Name = "TextboxTitle"
    55.         Me.TextboxTitle.Size = New System.Drawing.Size(173, 20)
    56.         Me.TextboxTitle.TabIndex = 2
    57.         '
    58.         'LabelTitle
    59.         '
    60.         Me.LabelTitle.AutoSize = True
    61.         Me.LabelTitle.Location = New System.Drawing.Point(12, 20)
    62.         Me.LabelTitle.Name = "LabelTitle"
    63.         Me.LabelTitle.Size = New System.Drawing.Size(81, 13)
    64.         Me.LabelTitle.TabIndex = 3
    65.         Me.LabelTitle.Text = "Bookmark Title:"
    66.         '
    67.         'TextboxURL
    68.         '
    69.         Me.TextboxURL.Location = New System.Drawing.Point(12, 72)
    70.         Me.TextboxURL.Name = "TextboxURL"
    71.         Me.TextboxURL.Size = New System.Drawing.Size(260, 20)
    72.         Me.TextboxURL.TabIndex = 4
    73.         '
    74.         'LabelURL
    75.         '
    76.         Me.LabelURL.AutoSize = True
    77.         Me.LabelURL.Location = New System.Drawing.Point(9, 56)
    78.         Me.LabelURL.Name = "LabelURL"
    79.         Me.LabelURL.Size = New System.Drawing.Size(83, 13)
    80.         Me.LabelURL.TabIndex = 5
    81.         Me.LabelURL.Text = "Bookmark URL:"
    82.         '
    83.         'FormAddBookmark
    84.         '
    85.         Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    86.         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    87.         Me.ClientSize = New System.Drawing.Size(284, 131)
    88.         Me.Controls.Add(Me.LabelURL)
    89.         Me.Controls.Add(Me.TextboxURL)
    90.         Me.Controls.Add(Me.LabelTitle)
    91.         Me.Controls.Add(Me.TextboxTitle)
    92.         Me.Controls.Add(Me.ButtonCancel)
    93.         Me.Controls.Add(Me.ButtonOK)
    94.         Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
    95.         Me.MaximizeBox = False
    96.         Me.MinimizeBox = False
    97.         Me.Name = "FormAddBookmark"
    98.         Me.Text = "My Bookmarks- Add Bookmark"
    99.         Me.ResumeLayout(False)
    100.         Me.PerformLayout()
    101.  
    102.     End Sub
    103.     Friend WithEvents ButtonOK As System.Windows.Forms.Button
    104.     Friend WithEvents ButtonCancel As System.Windows.Forms.Button
    105.     Friend WithEvents TextboxTitle As System.Windows.Forms.TextBox
    106.     Friend WithEvents LabelTitle As System.Windows.Forms.Label
    107.     Friend WithEvents TextboxURL As System.Windows.Forms.TextBox
    108.     Friend WithEvents LabelURL As System.Windows.Forms.Label
    109. End Class
    Last edited by proneal; Apr 1st, 2012 at 06:36 PM. Reason: added FormAddBookmark code

  15. #15

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Re: Deleting & Altering File Data

    Thanks for the help and I do feel a little guilty making you do all that work. How did you learn all of this?
    "When ideas and actions fuse, they form creations"

  16. #16
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Deleting & Altering File Data

    Practicing is all.
    Writing code and living off Intellisense, LOL
    Documentations too, as well as ebooks...
    World Wide Web too.
    So many resources available -sigh
    vbForums too!

  17. #17
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Deleting & Altering File Data

    Quote Originally Posted by Brinith View Post
    Thanks for the help and I do feel a little guilty making you do all that work. How did you learn all of this?
    I've appreciated intelligent dialog with you so I do this because I have nothing better to do with myself for rest of the night.
    I do it with pleasure.
    Please -no- guilt- allowed

  18. #18

    Thread Starter
    Lively Member Brinith's Avatar
    Join Date
    Feb 2012
    Location
    Near you
    Posts
    116

    Re: Deleting & Altering File Data

    After a little altering, I finally got the program to work. Thanks proneal, I couldn't have done it without you.
    "When ideas and actions fuse, they form creations"

  19. #19
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: [RESOLVED] Deleting & Altering File Data

    Well, my pleasure, but I am sad now. I really wanted to finish with adding and removing bookmarks
    BUT we are RESOLVED NOW, so -I will finish on my end.

    Pleasure working with you.


    patrick

  20. #20
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: [RESOLVED] Deleting & Altering File Data

    One more to help SPARK some ideas.
    On the Form_Load event of FormAddBookmark:

    vb Code:
    1. Private Sub FormAddBookmark_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    2.  
    3.  
    4.         If My.Computer.Clipboard.ContainsText(TextDataFormat.Text) Then
    5.             Dim ClipContents As String = My.Computer.Clipboard.GetText
    6.             If ClipContents.StartsWith("http://", StringComparison.OrdinalIgnoreCase) Then
    7.                 TextboxURL.Text = ClipContents
    8.             End If
    9.         End If
    10.     End Sub

    This will nicely place the copied URL of the Webpage you want to add as a Bookmark for your application (pretend program ) into the Textbox that holds the URL.

    It tests before applying- to see if it is TEXT data. If so, it will be placed. If not -nothing is entered in the text field.

    I could place a timer that checks for Text Data that ends with .php, shtml, html, htm, etc..or simply that starts with http://
    When it detects such text in Clipboard, the Add Bookmark form will automatically pop up and allow for quick naming of the Title for the Bookmark.
    Last edited by proneal; Apr 1st, 2012 at 08:38 PM.

  21. #21
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: [RESOLVED] Deleting & Altering File Data

    Well I modified the Form Load one last time, LOL
    Sorry. Last post, I promise!
    vb Code:
    1. Private Sub FormAddBookmark_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    2.  
    3.  
    4.         If My.Computer.Clipboard.ContainsText(TextDataFormat.Text) Then
    5.             Dim ClipContents As String = My.Computer.Clipboard.GetText
    6.             Dim myRootDirectory As String = My.Computer.FileSystem.CurrentDirectory.Substring(0, 1) & ":\"
    7.             If ClipContents.StartsWith("http://", StringComparison.OrdinalIgnoreCase) _
    8.                 Or ClipContents.StartsWith("https://", StringComparison.OrdinalIgnoreCase) _
    9.                 Or ClipContents.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase) _
    10.                 Or ClipContents.StartsWith(myRootDirectory, StringComparison.OrdinalIgnoreCase) Then
    11.                 TextboxURL.Text = ClipContents
    12.             End If
    13.         End If
    14.     End Sub

Tags for this Thread

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