Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
<hello
i have a web browser and a listBox showing urls from an sql server database, how can i tell to the webBrowser to navigate to listBox selected url? i tried this code but it stay on the first url! help me please!

C#
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            string url = listBox1.SelectedValue.ToString();
            webBrowser1.Navigate(url);
 
        }


thanks
Posted
Updated 5-May-13 14:20pm
v2
Comments
Sandeep Mewara 5-May-13 7:16am    
string url = listBox1.Text;
.Text? Why? Should that not be SelectedValue or so?
Nnorss 5-May-13 20:06pm    
right!
TnTinMn 5-May-13 19:37pm    
Please show your code that populates the listbox. If you just added string items to it, your code should be working.

If you set the listbox datasource and valuemember properties, you would use:

WebBrowser1.Navigate(listBox1.SelectedValue.ToString());
Nnorss 5-May-13 20:18pm    
the listBox shows item from a datasource, i set valuemember = url (url is a colomn of the db table and its nvarchar).
i tried the code that you suggested but it retun an error:
the exeption NullReferenceExeption is not managed
the object reference is not defined to an object instance...

the webBrowser show the first url, when i click on this first url of the listBox, the webBrowser refresh the page, but when i click on another url, nothing happens !
TnTinMn 5-May-13 21:33pm    
Without actually seeing your data and code it is not possible to to understand the reason for the NullReference exception.

It is possible to filter out null values though.

Give this a try. It also verifies that the value is a string.

private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (listBox1.SelectedValue != null && listBox1.SelectedValue is string)
{
webBrowser1.Navigate(listBox1.SelectedValue.ToString());
}
}

1 solution

change this line
string url = listBox1.Text;

to

string url = listBox1.SelectedText;

try it.
 
Share this answer
 
Comments
Nnorss 5-May-13 17:22pm    
when i changed it, "SelectedText" is underlined by the red line, and when i place the cursor on it it says that "System.Windows.Form.Lisbox doesnt contain a definition for 'SelectedText' and no extention method that accept first argument.. using assembly missing" (sorry i'm using french visual studio so i translated the error message)
i think i must add an assembly with 'using ...;' but wich one?
Member 10012743 6-May-13 6:38am    
System.Windows.Form.Lisbox ?

or is it :

System.Windows.Form.Listbox

please check the class name for listBox1


or are you doing asp.net or windows forms
Member 10012743 6-May-13 6:51am    
or do one thing :

change the event;

private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{

}

try this one:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string url = listBox1.SelectedText;
webBrowser1.Navigate(url);
}

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