WebBrowser.DocumentCompleted and Navigate - C# - 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 ?

Related

Show password in textbox while holding a button

I have a textbox in my WinForm and when I type the password in, its hidden because:
private void textBoxPWMain2_TextChanged(object sender, EventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = true;
}
is it possible to add here a button, and while the button is pressed, the password show normal and when I stop pressing the button, the password will hide again?
Maybe this? (Don't forget to subscribe to these events)
private void button2_MouseDown(object sender, EventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = false;
}
private void button2_MouseUp(object sender, EventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = true;
}
I have a solution now, I wanted something like a eye button, when you press it down the password shows, when you stop pressing, the password hides again.
Solution
First I added a pictureBox with Eye Icon and added this pictureBox to my password textbox and set Passwort textbox to .UseSystemPasswordChar
public Form1
{
textBoxPW.Controls.Add(pictureBoxEye);
pictureBoxEye.Location = new Point(95,0);
pictureBoxEye.BackColor = Color.Transparent;
textBoxPW.UseSystemPasswordChar = true;
//Subscribe to Event
pictureBoxPW.MouseDown += new MouseEventHandler(pictureBoxPW_MouseDown);
pictureBoxPW.MouseUp += new MouseEventHandler(pictureBoxPW_MouseUp);
}
Added the Mouse_Down/Up Event
private void pictureBoxEye_MouseDown(object sender, MouseEventArgs e)
{
textBoxPW.UseSystemPasswordChar = false;
}
private void pictureBoxEye_MouseUp(object sender, MouseEventArgs e)
{
textBoxPW.UseSystemPasswordChar = true;
}
This works fine for me! Thank you guys !!
Adding a bit change details to ispiro's answer
public void button1_MouseDown(object sender, EventArgs e)
{
textBox1.PasswordChar = '\0';
textBox1.UseSystemPasswordChar = false;
}
public void button1_MouseUp(object sender, EventArgs e)
{
textBox1.PasswordChar = '*';
textBox1.UseSystemPasswordChar = true;
}
Before:-
After :-
Is there a reason you set the UseSystemPasswordChar in the TextChanged event?
If you can set the property in the Initialize() method or in the constructor you can implement the following events for your button:
private void button1_MouseDown(object sender, MouseEventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = false;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = true;
}

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

Retrieving the URL of a clicked link in a C# WebBrowser control

Okay. So, I'm making my own internet browser and I have tabs. But, I'm trying to make it a real time url updater while if you click on any link it will show up in the textbox of the url.
It will not work.
Here is the tab button.
private void button8_Click(object sender, EventArgs e)
{
WebBrowser Browser = new WebBrowser();
tabControl1.TabPages.Add("New Page");
tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
Browser.Name = "Web Browser";
Browser.Dock = DockStyle.Fill;
tabControl1.SelectedTab.Controls.Add(Browser);
((WebBrowser)(tabControl1.SelectedTab.Controls[0])).GoHome();
And here is where my textbox isn't getting the right url.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
button1.Enabled = true;
textBox1.Enabled = true;
//textBox1.Text = Browser.Url.ToString();
((WebBrowser) (this.tabControl1.SelectedTab.Controls[0])).Url.ToString();
}
In order for this to work, you need to get all link elements of the web page you've just loaded and assign a custom function to the HtmlElement.Click event of that element.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
button1.Enabled = true;
textBox1.Enabled = true;
var linkElements = Browser.Document.GetElementsByTagName("a");
foreach(HtmlElement link in linkElements)
{
link.Click += (s, args) =>
{
// a link is being clicked
// get the url the link is pointing to using the href attribute of the element
textBox1.Text = link.GetAttribute("href");
}
}
}

Error while using webbrowser

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();
}

Categories