Click here to Skip to main content
13,055,884 members (49,332 online)
Rate this:
Please Sign up or sign in to vote.
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!

private void listBox1_SelectedValueChanged(object sender, EventArgs e)
            string url = listBox1.SelectedValue.ToString();

Posted 4-May-13 10:09am
Updated 5-May-13 14:20pm
Sandeep Mewara 5-May-13 7:16am
string url = listBox1.Text;
.Text? Why? Should that not be SelectedValue or so?
Naourass Derouichi 5-May-13 20:06pm
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:

Naourass Derouichi 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)

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

change this line
string url = listBox1.Text;


string url = listBox1.SelectedText;

try it.
Naourass Derouichi 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 :


please check the class name for listBox1

or are you doing 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;

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 6 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100