Error while using webbrowser - c#

So I want to use a webbrowser to show an html file, and print it. I can load the file, but when I try to use webBrowser1.ShowPrintDialog(); I have a script error. I tried to disable this with webBrowser1.ScriptErrorsSuppressed = true; but it doesn't work.
The weird part is that when I run my code in debug(F5), it works, but not in "normal" mode (Ctrl + F5).
My code so far is very simple:
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://google.com");
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.ShowPrintDialog();
}

Related

WebBrowser.DocumentCompleted and Navigate - C#

I'm trying to modify a simple application which use the WebBrowser item:
private void button1_Click(object sender, EventArgs e)
{
var menu = webBrowser1.Document.GetElementById("SomeItem").InvokeMember("click");
webBrowser1.Visible = true;
button1.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Visible = false;
webBrowser1.AllowNavigation = true;
webBrowser1.Navigate("domain");
}
This works fine, the page load and then after clicking the button the user is redirected to the desired location.
Now I'm trying to make this without the help of the button.
Simply put the button code inside the load function didn't help, so I've introduced in my code the DocumentCompleted event.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.AllowNavigation = true;
webBrowser1.Navigate("SomeDomain");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentComplete);
}
private void wb_DocumentComplete(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
wb.Document.GetElementById("SomeItem").InvokeMember("click");
}
By debugging it, I saw that the wb object is correctly instantiated with the "domain" but I got System.NullReferenceException when I try to retrieve SomeItem.
The document is not fully loaded because I'm still in the load function ?

C# Help Using Multiple Forms

I am trying to create a application with multiple forms,
I have placed the forms on top of each other in the section needed.
However, the code as provided does not work.
private void home_butt_Click(object sender, EventArgs e)
{
Home_Panel.Visible = true;
Home_Panel.BringToFront();
}
private void Intro_butt_Click(object sender, EventArgs e)
{
Introduction.Visible = true;
Introduction.BringToFront();
Home_Panel.Visible = false;
Crime.Visible = false;
}
private void Crime_butt_Click(object sender, EventArgs e)
{
Crime.Visible = true;
Crime.BringToFront();
Home_Panel.Visible = false;
Introduction.Visible = false;
}
It will the home_Panel on start up but when you click on a button it will only show the Crimes Panel.
Any help will be appreciated.
Why not use the show and hide methods for forms instead of bringing them to the front and setting the visibility? E.G.
private void home_butt_Click(object sender, EventArgs e)
{
Home_Panel.Show();
Crime.Hide();
Introduction.Hide();
}
And vice versa for the other button events?

GlobalHotKey not firing any events

I googled around to find a way to have a GlobalHotKey on my C# Winform.
I saw this tutorial and followed it.
The issue I'm having now is that nothing is occurring once I press the designated key.(In this case, the ` key)
Here is the class I'm using
Here is the relevant code on my Form1.cs
Hotkey hk = new Hotkey();
private void Form1_Load(object sender, EventArgs e)
{
hk.KeyCode = Keys.Oemtilde;
hk.Windows = true;
hk.Pressed += hk_Pressed;
hk.Register(this);
}
private void hk_Pressed(object sender, EventArgs e)
{
MessageBox.Show("pressed");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (hk.Registered)
{ hk.Unregister(); }
}
Because you set hk.Windows = true; which required you to press Windows+ ``, remove that line and press `, it will work

C# write text to textbox which adds written text to linklabel and when linklabel clicked goes to url?

I'm trying to do the following: I write link to textbox and it displays in a linklabel and after I click linklabel it goes to that url written in it? Everything goes well but pressing linklabel doesn't go to url.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
label2.Text = textBox1.Text;
linkLabel1.Text = textBox2.Text;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.checkbox = checkBox1.Checked;
Properties.Settings.Default.textbox = textBox1.Text;
Properties.Settings.Default.label = label2.Text;
Properties.Settings.Default.linkLabel = linkLabel1.Text;
Properties.Settings.Default.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkbox;
textBox1.Text = Properties.Settings.Default.textbox;
label2.Text = Properties.Settings.Default.label;
linkLabel1.Text = Properties.Settings.Default.linkLabel;
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
}
You have to start a process with the text of the LinkLabel, which should be a valid URL. This code will open the URL in the default browser:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(linkLabel1.Text);
}
More info on the Process Class.
If the default browser is IE and you want to open it in Chrome for example you'll have to provide the necessary info:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = #"C:\Users\<UserName>\AppData\Local\Google\Chrome\chrome.exe";
p.StartInfo.Arguments = linkLabel1.Text;
p.Start();
}
Of course you'll have to do validation to check if the text of the LinkLabel is a valid URL.
You should use following code to add a link to LinkLabel as:
// Add a link to the LinkLabel.
LinkLabel.Link link = new LinkLabel.Link();
link.LinkData = textbox2.Text;
linkLabel1.Links.Add(link);

Source code to text file not working using C#.net and Web browser?

This source code scraping is not working; it's giving a blank text file as output:
private void button2_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.jigsaw.com/SearchCompany.xhtml?opCode=refresh&rpage=20&order=0&orderby=0&industry=1160000&subindustry=1160300&country=9000&country=2000&cmDead=false&count=0&screenNameType=0&screenName=&omitScreenNameType=0&omitScreenName=&rowsPerPage=200&uid=13473859&tok=1354716874406-8761960955252771794");
string MainsourceCode = webBrowser1.DocumentText;
StreamWriter sw = new StreamWriter("G:/jigsaw_info.txt", true);
sw.Write(MainsourceCode + "\n");
sw.Close();
}
You are starting to write too fast. You should wait DocumentCompleted event.
private void button2_Click(object sender, EventArgs e)
{
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
webBrowser1.Navigate("https://www.jigsaw.com/SearchCompany.xhtml?opCode=refresh&rpage=20&order=0&orderby=0&industry=1160000&subindustry=1160300&country=9000&country=2000&cmDead=false&count=0&screenNameType=0&screenName=&omitScreenNameType=0&omitScreenName=&rowsPerPage=200&uid=13473859&tok=1354716874406-8761960955252771794");
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
File.WriteAllText("G:/jigsaw_info.txt",webBrowser1.DocumentText);
}
You have to wait until navigation is complete. Navigation is asynchronous.
Detect WebBrowser complete page loading

Categories