How can I read user input to an array in VB.NET?
OK, so I am trying to make a program using Visual Basic that will allow the user to add links to web pages (such as www.google.ca
) to a list in the program and make it so that they do not dissapear once the program is closed.
So to get into more detail, I have a text box, a listview, and a button. When the user types a link into the text box it needs to be put into an array (called "addlink") and 开发者_Go百科then when the user presses the button, the link is typed into the listview as an object.
Then if the user clicks on that object in the listview, it will open up the browser using the WebBrowser
command. How do I set the text in the textbox to an array once the button is clicked?
This program greatly resembles most Internet browser's bookmarking feature. :D
Specifications:
- OS: Windows 7 Ultimate Edition
- Software: Microsoft Visual Basic 2010 Express
- Experience with Visual Basic: Average
To answer your question about adding to an array. You could do something like this.
Dim addlink() As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If IsNothing(addlink) Then
ReDim addlink(0)
Else
ReDim Preserve addlink(addlink.Count)
End If
addlink(UBound(addlink)) = TextBox1.Text
TextBox1.Text = Nothing
End Sub
Or to use a collection like @Cody Gray suggested
Dim addlink As New List(Of String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
addlink.Add(TextBox1.Text)
TextBox1.Text = Nothing
End Sub
edit: I have removed my answer, which was specific to VB6, but left the portion of my answer that made suggestions:
You will need an edit bookmark and remove bookmark feature.
You could make the textbox multi-line and allow the user to input multiple links at once... or you can do one at a time. The only difference is that you would have to cut the text up every time there was a chr(13) & chr(10), using Mid (to do the actually cutting) and inStr (to figure out where to cut.)
Logically separating the "titles" of the bookmarks from the URLs would be ideal. For example, you might have 3 links to different cook.com recipes (we'll say chicken, lamb, and stir fry), but the URLs might resemble something like http://www.cook.com/recipes/2389047291
, which doesn't exactly inform the user which link they want if they want a specific recipe. Using two text boxes, you could allow the user to logical separate the titles and the URL, e.g. List1: Cook.com Stir Fry... List2: http://www.cook.com/recipes/2389047291 (Alternatively, you could just hide the URL altogether, displaying only the page titles to the user.)
精彩评论