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
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"
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"
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.
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"
How do you declare BMF?
The Listbox gets filled from your bookmarks textfile, right?
vb Code:
Dim R As New IO.StreamReader(your_bookmarks_textfile)
Dim BMF As String() = R.ReadToEnd().Split(New String(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries))
ListBox1.Items.AddRange(BMF)
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.
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"
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:
Option Strict On
Option Explicit On
Public Class Bookmark
Public URL As String
Public Title As String
Public Overrides Function ToString() As String
Return Me.Title
End Function
End Class
Public Class Form1
Dim MyBookmarks As String = My.Computer.FileSystem.CurrentDirectory & "/bookmarks.txt"
Dim BMF As String()
Dim newBMF As String()
Private Sub FindBookmarks(Optional ByVal GoToTarget As Boolean = False)
If Not GoToTarget Then
Me.ListBox1.Items.Clear() 'for testing
End If
Dim bm_index As Integer = 0
Dim NewBookmark As New Bookmark
For Each bookmark In BMF
newBMF = Split(BMF(bm_index), ",")
NewBookmark.Title = newBMF(0)
NewBookmark.URL = newBMF(1)
If GoToTarget Then
If ListBox1.SelectedIndex = bm_index Then
Process.Start(NewBookmark.URL.Trim)
Exit Sub
End If
Else
Me.ListBox1.Items.Add(NewBookmark)
End If
bm_index += 1
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
FindBookmarks(True)
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim R As New IO.StreamReader(MyBookmarks)
BMF = R.ReadToEnd().Split(CChar(vbNewLine))
R.Close()
FindBookmarks()
End Sub
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
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.
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"
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.
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:
Option Strict On
Option Explicit On
Public Class Form1
Dim MyBookmarks As String = My.Computer.FileSystem.CurrentDirectory & "/bookmarks.txt"
Dim BMF As String()
Dim newBMF As String()
Dim GoogleBookmark As String = "Google," & "www.google.com"
Private Sub FindBookmarks(Optional ByVal GoToTarget As Boolean = False)
If Not GoToTarget Then
Me.ListBox1.Items.Clear() 'for testing
End If
Dim bm_index As Integer = 0
Dim NewBookmark As New Bookmark
For Each bookmark In BMF
newBMF = Split(BMF(bm_index), ",")
NewBookmark.Title = newBMF(0)
NewBookmark.URL = newBMF(1)
If GoToTarget Then
If ListBox1.SelectedIndex = bm_index Then
Process.Start(NewBookmark.URL.Trim)
Exit Sub
End If
Else
Me.ListBox1.Items.Add(NewBookmark)
End If
bm_index += 1
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
FindBookmarks(True)
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
' check to see if the bookmarks file exists
' If not, then create it and add your website URL or default it to Google which is what I did for this example program
If Not My.Computer.FileSystem.FileExists(MyBookmarks) Then
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!
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
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.
One more to help SPARK some ideas.
On the Form_Load event of FormAddBookmark:
vb Code:
Private Sub FormAddBookmark_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
If My.Computer.Clipboard.ContainsText(TextDataFormat.Text) Then
Dim ClipContents As String = My.Computer.Clipboard.GetText
If ClipContents.StartsWith("http://", StringComparison.OrdinalIgnoreCase) Then
TextboxURL.Text = ClipContents
End If
End If
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.