Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

web browser ctrl - retrieve address for display in textbox?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rich - 26 Sep 2007 17:16 GMT
Greetings,

I am using a web browser control to browse windows explorer.  I give it a
default address as below and then click on some folder.  The question is:  
how do I retrieve the address of the folder I clicked on so that I can
display it in a textbox similar to windows explorer?  

object loc = "W:\\EP  2005";
object null_obj_str = "";
System.Object null_obj = 0;
this.axWebBrowser1.Navigate2(ref loc , ref null_obj, ref null_obj, ref
null_obj_str, ref null_obj_str);

While I am at it, is there a back button mechanism that is already built
into .Net 2.0?  Or do I have to go with my plan of noting all the addresses
that have been visited and then just navigating backwards from the list of
visited address that I store?

Thanks,
Rich
Nicholas Paldino [.NET/C# MVP] - 26 Sep 2007 17:30 GMT
Rich,

   You should be able to look at the Document property of the web browser
control and then look at the Url property on the HtmlDocument instance
returned from that to get the current page location.

   The WebBrowser control has a GoBack method on it that you can use to
navigate to the previous page.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Greetings,
>
[quoted text clipped - 17 lines]
> Thanks,
> Rich
Rich - 26 Sep 2007 18:24 GMT
Thank you for your reply.   I am not familiar with the web browser control.  
I amjust getting errors right now.  May I ask for a simple example how I
would use the document protperty of the web browser? and the url property?

Thanks

> Rich,
>
[quoted text clipped - 26 lines]
> > Thanks,
> > Rich
Nicholas Paldino [.NET/C# MVP] - 26 Sep 2007 18:47 GMT
Rich,

   It's just a property access, like so:

Uri url = webBrowser.Document.Url;

   If you want the string representation of the Uri, then just call the
ToString method on the uri.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Thank you for your reply.   I am not familiar with the web browser
> control.
[quoted text clipped - 38 lines]
>> > Thanks,
>> > Rich
Rich - 26 Sep 2007 18:58 GMT
Thank you again for getting back to me.  Here is what I tried:

Uri url = web1.Document.Url;

but it is error ing out.  I guess I will have to wrestle with this for a
while.

I also tried

Console.WriteLine(web1.Document.Url.ToString)

but error said Object reference not set to instance of an object.  If I
comment out the Console... then web1 displays the contents of the specified
folder.

> Rich,
>
[quoted text clipped - 47 lines]
> >> > Thanks,
> >> > Rich
Nicholas Paldino [.NET/C# MVP] - 26 Sep 2007 19:12 GMT
Rich,

   What is the exception you are getting?  If you don't indicate what
exception is occuring, it's very difficult to figure out what the error is.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Thank you again for getting back to me.  Here is what I tried:
>
[quoted text clipped - 71 lines]
>> >> > Thanks,
>> >> > Rich
Rich - 26 Sep 2007 19:26 GMT
Well, here is what I am tyring now with no luck:

object loc = "W:\\EP  2005";
object null_obj_str = "";
System.Object null_obj = 0;
this.axWebBrowser1.Navigate2(ref loc , ref null_obj, ref null_obj, ref
null_obj_str, ref null_obj_str);

I added the lines below to the above.  won't compile.  (C# 2005)

HtmlDocument doc = this.axWebBrowser1.Document;
Uri url = doc.Url;
Console.WriteLine(url.ToString);

> Rich,
>
[quoted text clipped - 76 lines]
> >> >> > Thanks,
> >> >> > Rich
Rich - 26 Sep 2007 19:46 GMT
made some changes

HTMLDocument doc = (HTMLDocument)this.axWebBrowser1.Document;

Console.WriteLine(doc.url.ToString);  //errors here

error says

Error 1 The best overloaded method match for
'System.Console.WriteLine(bool)' has some invalid arguments   

Error 2 Argument '1': cannot convert from 'method group' to 'bool'   

> Well, here is what I am tyring now with no luck:
>
[quoted text clipped - 90 lines]
> > >> >> > Thanks,
> > >> >> > Rich
mpetrotta@gmail.com - 26 Sep 2007 19:56 GMT
> made some changes
>
[quoted text clipped - 8 lines]
>
> Error 2 Argument '1': cannot convert from 'method group' to 'bool'

ToString is a method, so you need to call it as you would other
methods (with parentheses around the parameters).

Back to your original issue: since you say you're using .NET 2.0, I
strongly suggest you use the native WebBrowser control
(System.Windows.Forms.WebBrowser) rather than the ActiveX-wrapped
kludge.  It's more robust and easier to use.

Michael
Rich - 26 Sep 2007 20:10 GMT
Thank you for the tip.  Actually, I figured out a couple of things (btw, I
confess that I am really a vB guy and was studying a sample I found in C# -
forgot about the parens - lazy vb guy - the IDE puts em in for me).  But I am
able to get the current url from the documentcompleted event of the web
browser.

So you say that the web browser control from the toolbox and
System.Windows.Forms.WebBrowser
are not the same?  

In the components initialize page of my app it says

Me.web1 = New System.Windows.Forms.WebBrowser

Well, anyway, I solved my problem.  I thank you all for your replies and tips.

> > made some changes
> >
[quoted text clipped - 18 lines]
>
> Michael
mpetrotta@gmail.com - 26 Sep 2007 20:32 GMT
> Thank you for the tip.  Actually, I figured out a couple of things (btw, I
> confess that I am really a vB guy and was studying a sample I found in C# -
[quoted text clipped - 5 lines]
> System.Windows.Forms.WebBrowser
> are not the same?

I don't know what's in your toolbox, but if you're constructing it as
you described in your original post, then no, they're not the same.
System.Windows.Forms.WebBrowser does not have a Navigate2 method (it
has a Navigate method that takes far fewer arguments).

> In the components initialize page of my app it says
>
> Me.web1 = New System.Windows.Forms.WebBrowser

Then you're not using the same code or control as you posted
originally.  You're now using the newer, more sensible control:

WebBrowser webBrowser = new WebBrowser();
webBrowser.Navigated += webBrowser_Navigated;
...
private void webBrowser_Navigated(object sender,
WebBrowserNavigatedEventArgs e)
{
    Console.WriteLine(e.Url.ToString());
}
...
webBrowser.Navigate("http://www.google.com");
...

Note that you need to wait for the control to finish navigating before
you can retrieve the URL, as shown above.

Michael

Michael
Rich - 26 Sep 2007 21:37 GMT
I see your point.  the webbrowser control from the toolbox (VS2005) does not
have a Navigate2 property.  I will use the SystemWindows.Forms.WebBrowsert.

Thanks for the tip

> > Thank you for the tip.  Actually, I figured out a couple of things (btw, I
> > confess that I am really a vB guy and was studying a sample I found in C# -
[quoted text clipped - 36 lines]
>
> Michael
mpetrotta@gmail.com - 26 Sep 2007 20:52 GMT
On Sep 26, 10:56 am, "mpetro...@gmail.com" <mpetro...@gmail.com>
wrote:

> Back to your original issue: since you say you're using .NET 2.0, I
> strongly suggest you use the native WebBrowser control
> (System.Windows.Forms.WebBrowser) rather than the ActiveX-wrapped
> kludge.  It's more robust and easier to use.

("Native" is the wrong word - there's still an ActiveX control being
wrapped, but it's the Framework WebBrowser class doing the wrapping
for you.)

Michael

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.