Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello everyone,

i have an app with one list and one webbrowser and one external file on the web

lets say this:
my app "C:\App.exe"
my file "Http:\\something.com\file.txt"

the content of the file:
[Title]
Google
Facebook
[URL]
http://www.google.com
http://facebook.com



now how can i make the app read the text file
then include the "Title" objects in the list
then make "OnClick" event for every one as this one
VB
WebBrowser1.Navigate("http://xxxx.xxx")


and this have to be like this:
on the list the first object will be "Google"
and "OnClick" event will be "
VB
WebBrowser1.Navigate("http://www.google.com")


so, is this possible or not
if it possible please help me to do it :(
Posted
Comments
LanFanNinja 5-Feb-12 14:34pm    
Check solution below.
If you need any explanation of what the code does just ask.

1 solution

Sure it is possible!

Personally I would want to store the sites title and url on the same line though.
EXAMPLE:
Google|http://www.google.com/
Facebook|http://www.facebook.com/

Then it would be even easier to parse the file.

For an example I will parse this file into a dictionary with the title being the key and the url being the value.

EDIT: The code was in C# then I realized you wanted VB so I convert the code using the converter at http://converter.telerik.com/[^]

/// C# Code ///
C#
using (WebClient client = new WebClient())
{
    client.DownloadFile("http://something.com/test.txt", "test.txt");
}

string[] lines = File.ReadAllLines(@"test.txt");
Dictionary<string, string> fileData = new Dictionary<string, string>();

for (int i = 0; i < lines.Length; i++)
{
    string[] split = lines[i].Split(new char[] { '|' },
        StringSplitOptions.RemoveEmptyEntries);

    fileData.Add(split[0], split[1]);
}

/// VB.Net Code ///
VB
Using client As New WebClient()
    client.DownloadFile("http://something.com/test.txt", "test.txt")
End Using

Dim lines As String() = File.ReadAllLines("test.txt")
Dim fileData As New Dictionary(Of String, String)()

For i As Integer = 0 To lines.Length - 1
    Dim split As String() = lines(i).Split(New Char() {"|"C}, StringSplitOptions.RemoveEmptyEntries)

    fileData.Add(split(0), split(1))
Next



Then just to show it works.
/// C# Code ///
C#
string title = fileData.Keys.ElementAt(0);
string url = fileData[title];

MessageBox.Show(title + " - " + url);

/// VB.Net Code ///
VB
Dim title As String = fileData.Keys.ElementAt(0)
Dim url As String = fileData(title)

MessageBox.Show(title + " - " + url)

the message box would show " Google - http://www.google.com/ "

You could add all these keys to a listBox for example:
/// C# Code ///
C#
for (int i = 0; i < fileData.Count; i++)
{
    sitesListBox.Items.Add(fileData.Keys.ElementAt(i));
}

/// VB.Net Code ///
VB
For i As Integer = 0 To fileData.Count - 1
    sitesListBox.Items.Add(fileData.Keys.ElementAt(i))
Next


then use the SelectedIndexChanged event of the list box to navigate the web browser to the url
/// C# Code ///
C#
private void sitesListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    webBrowser.Navigate(fileData[(string)sitesListBox.SelectedItem]);
}

/// VB.Net Code ///
VB
Private Sub sitesListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sitesListBox.SelectedIndexChanged
    webBrowser.Navigate(fileData(DirectCast(sitesListBox.SelectedItem, String)))
End Sub


NOTE:
There are many ways to do this but this is a quick way that will work just fine.
 
Share this answer
 
v13
Comments
ahmad.afef 5-Feb-12 15:00pm    
ok this will only list the text in the list
i'm asking about listing the name only
and create an action for the name
this action will open the link of the name in the browser in the form
so the import will make the name clickable and make this action for
each name in the list
WebBrowser1.Navigate("http://www.google.com")
the url will be hidden for the user and only the name will be viable

thank you and i hope u have the answer :)
LanFanNinja 5-Feb-12 15:06pm    
This code will only list the Name i.e. " Google " in the list and then when the name is clicked on the url will be retrieved from the dictionary and passed to the webBrowser.Navigate() method. The user will never see the URL.

So if I am understanding you correctly this is exactly the functionality you are looking for.
LanFanNinja 5-Feb-12 15:07pm    
Also if the convert at developerFusion happens to be down there is another converter here for you to use.
telerik code converter
LanFanNinja 5-Feb-12 15:25pm    
Recheck the solution if you need the code in VB I decided to convert it for you anyway.
LanFanNinja 5-Feb-12 15:30pm    
Again if you need any help with the code just ask as I realize my code is not just a copy and past solution it will need to be modified a little for your needs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900