blah blah blah is here! blah blah » Close

up1down
link

Hi,

I am working on a small application which has a listview and webbrowser control. I have a list of URLs in the first column of the list view and I want the webbrowser to navigate to that URL once the item in the listview is clicked, is this possible?

I am trying something like

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser1.Navigate(new Uri(listView1.SelectedItem.ToString()));
}

last answered one year ago

1 answers

link

Try the following instead:

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
webBrowser1.Navigate(listView1.SelectedItems[0].Text);
}
}

ademmeda
108

Thanks vulpes, I tried it but didn't work.

vulpes
17279

It worked fine when I tried it myself. Are you getting an error or does nothing show up in the web browser control ? Are you sure the items in the ListView are valid URLs?

ademmeda
108

The URLs are ok, it does not give error, just nothing shows in the webbrowser when I run the application and click the listview items.

vulpes
17279

I wonder if the eventhandler isn't wired up. Did you add it from the Visual Studio Forms designer (in which case it'll be wired up automatically) or have you added it manually? If you can't remember, you could put a MessageBox.Show(listView1.SelectedItems[0].Text) in the handler and see whether it displays anything when you select something in the listview.

ademmeda
108

I guess you are right. I added the messagebox code but nothing displays when I select listview items. I am working on Microsoft visual C# 2008 express edition by the way. What I did is just created a new "windows form application", added a listview and webbrowser control on it using the designer. Added a few items-URLs- to the listbox, and added your code. My Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Listview_Deneme { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void listView1_SelectedIndexChanged(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { webBrowser1.Navigate(listView1.SelectedItems[0].Text); MessageBox.Show(listView1.SelectedItems[0].Text); } } } }

vulpes
17279

OK, what I'd do is to delete the listView1_SelectedIndexChanged method completely. Then go to the designer and double click on the listview which will add the method again (as a 'skeleton') but this time it will be wired up. Now insert my code into the skeleton method, build and run.

ademmeda
108

Great! It worked. Thank you very much for your time.

Feedback