Click here to Skip to main content
11,488,877 members (78,694 online)
Rate this: bad
good
Please Sign up or sign in to 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!

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

thanks
Posted 4-May-13 11:09am
Nnorss521
Edited 5-May-13 15:20pm
v2
Comments
Sandeep Mewara at 5-May-13 7:16am
   
string url = listBox1.Text;
.Text? Why? Should that not be SelectedValue or so?
Naourass Derouichi at 5-May-13 20:06pm
   
right!
TnTinMn at 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());
Naourass Derouichi at 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 at 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

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

Solution 1

change this line
string url = listBox1.Text;

to

string url = listBox1.SelectedText;

try it.
  Permalink  
Comments
Naourass Derouichi at 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 at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 489
1 OriginalGriff 328
2 Sascha Lefèvre 135
3 _Asif_ 125
4 _duDE_ 121
0 Sergey Alexandrovich Kryukov 9,308
1 OriginalGriff 8,375
2 Sascha Lefèvre 3,334
3 Maciej Los 2,996
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 6 May 2013
Copyright © CodeProject, 1999-2015
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