473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to Load File into WebBrowser

Hi there,

I currently am trying to get HTML files to load into my WebBrowser control. These HTML files are saved on disk and I have a working OpenFileDialog control, but when I select the file it won't load. When I navigate to the HTML file I want to load and click OK, all it does it is pop up a new dialog box that shows the file address.

I am new to C# and haven't learned everything yet so what would be the easiest way to get the files to load?

Thanks for your time and help.

Michael
Aug 17 '08 #1
7 8556
Curtis Rutland
3,256 Expert 2GB
I'm not sure what the problem is...
Here's something I just tried, and it works:
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.     DialogResult res = openFileDialog1.ShowDialog();
  4.     if (res == DialogResult.OK)
  5.         textBox1.Text = openFileDialog1.FileName;
  6. }
  7.  
  8. private void button2_Click(object sender, EventArgs e)
  9. {
  10.     FileInfo fi = new FileInfo(textBox1.Text);
  11.     if (fi.Exists)
  12.     {
  13.         webBrowser1.Navigate(fi.FullName);
  14.     }
  15. }
  16.  
button1 opens the OpenFileDialog, and sets its value to a textbox. But you can store that value anywhere. button2 checks to see if the file selected still exists, and then navigates to that filename. And it works. (FileInfo is in the System.IO namespace)

You could implement a few more checks, to see if the file is an HTML file, but this basic code here works.
Aug 17 '08 #2
Thanks for the response but what I am trying to do is to have a menu system where the File Open option isn't a button but its located in a menu which for my browser is the usual File->Open File...

Is there a way to set up the code to work this way?

Here is what I have currently which lets me navigate to the file, select it but won't load it.

Expand|Select|Wrap|Line Numbers
  1. OpenFileDialog fDialog = new OpenFileDialog();
  2. fDialog.Title = "Open HTML File";
  3. fDialog.Filter = "HTML files (*.html)|HTM files (*.htm)|All files (*.*)|*.*";
  4. fDialog.InitialDirectory = @"C:\Documents and Settings\";
  5. if (fDialog.ShowDialog() == DialogResult.OK)
  6. {
  7.  
  8.     MessageBox.Show(fDialog.FileName.ToString());
  9.  
  10. }
  11. fDialog.CheckFileExists = true;
  12.  
  13. fDialog.CheckPathExists = true;
I also tried implementing the above suggestion but I kept receiving errors relating to the System.IO namespace saying that 'Windows' is not part of my webBrowser.
Aug 17 '08 #3
Curtis Rutland
3,256 Expert 2GB
Well, in the code snippet you provided, it seems like you never actually try to navigate the web browser control. Once you've verified that the path exists, you need to do:
nameOfWebBrowserControl.Navigate("urlToFile");
Aug 17 '08 #4
Yeah I never had it in there because I didn't know how to do it. I am also getting an assembly reference saying that the type or namespace Navigate does not exist. What namespace is Navigate under or how do I fix this?

Thank you so much for all the help and sorry for so many questions.
Aug 18 '08 #5
Curtis Rutland
3,256 Expert 2GB
Do you already have a WebBrowser control added to your project?

Just take the name of that webBrowser, and add .Navigate(urlToNavigateTo);
on to the end of it.
Aug 18 '08 #6
Do you already have a WebBrowser control added to your project?

Just take the name of that webBrowser, and add .Navigate(urlToNavigateTo);
on to the end of it.

Yes I have a webBrowser control but it keeps saying that .Navigate does not exist within namespace webBrowser.
Aug 19 '08 #7
Curtis Rutland
3,256 Expert 2GB
You're going to need to post more of your code...you're obviously using this incorrectly. WebBrowser isn't a namespace, it's a class. And navigate isn't a static method, so you need to be using it from an instance.

Post all the relevant code so we can see where you've gone wrong.
Aug 19 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Charles A. Lackman | last post by:
Hello, I have Visual Studio .NET and was wondering if you can load a PDF File into the report Viewer. If this is possible a quick line of code (VB) would be greatly appreciated. Thanks, ...
2
by: Dave | last post by:
Hi, I'm creating a windows forms utility to create dhtml flyout menus. I want to load the html and javascript generated into a webbrowser control, thats on the form, so I can view the menus and...
0
by: PL | last post by:
Hi, I would like to know how to load a HTML file contained in a Stream within an embedded IE webbrowser. I'm not using WinFX or .Net Framework 2.0 but Framework 1.1 (so I'm using the OCX). ...
1
by: Risen | last post by:
Hi, all, How can I to load Html file that in memory stream to webBrowser instead to load Html file from disk. can anybody help me? Thx. B.R. Risen
5
by: Jesper | last post by:
Hi, Is there an alternative to an URI location when loading html into a webBrowser control. I have some html generated in a string. I would like to skip saving the string to a file on disk...
4
by: adwooley2 | last post by:
Can't seem to find anything that works for me. I have a small bit of HTML that is stored in a string variable. I want to assign this string to an mshtml object (IHTMLDocument2 I believe), so that...
1
by: MrNobody | last post by:
I want to load a document which would be residing in the same directory as the application so I pass in a string to WebBrowser control which has no path information- normally doing this with...
1
by: Manfred | last post by:
Hello I have a Webbrowser Control on my mainform. When the Webbrowser is loading a webpage it may take some time and the application waits upon completion. How can I load this page in the...
1
by: muralidhargvn | last post by:
Hi, I am writing an application in C# (.NET 1.1). I have AxWebBrowser control in my form. It will be loaded with a html page from the following location: /GA/Receive/Main.htm The loaded...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.