Why lose webbrowser control when adding click event handler? - c#

I'm using .NET visual C# 2008 and in my webbrowser, I added click event handler to capture what html tag name I've clicked.
But then I lost control to the webbrowser.
When I start the application, in the webbrowser, I cannot enter texts into text field in the browser. It still accepts mouse clicks to the links though.
I've found few people having this issue in forums but no solution is found.
What am I doing wrong?
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace IERecorder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private mshtml.HTMLDocument doc = null;
private void Form1_Load_1(object sender, EventArgs e)
{
txtRecord.Items.Add("start...");
txtRecord.Items.Add("start2...");
webBrowser1.Navigate("http://www.google.com");
}
private void webBrowser1_DocumentComplete(object sender, WebBrowserDocumentCompletedEventArgs e)
{
txtRecord.Items.Add(e.Url.ToString() + " loaded...");
if (doc == null)
{
doc = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
mshtml.HTMLDocumentEvents2_Event iEvent;
iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);
}
}
private bool ClickEventHandler(mshtml.IHTMLEventObj e)
{
txtRecord.Items.Add("clicked ==>" + e.srcElement.tagName);
txtRecord.Items.Add("clicked2 ==>" + e.srcElement.getAttribute("name", 0));
txtRecord.Items.Add("clicked3 ==>" + e.srcElement.innerHTML);
return true;
}
}
}

Its a framework related issue, It will not work on framework 3.5. Change the project framework to 4.5.2, It will work.

I don't see a text field in your code? Did you mean the google search box?
This is a screenshot of what I was able to reproduce with the code that you supplied. I was able to search for stackoverflow and browse to it. I would doublecheck that your event handlers are still wired up correctly.

Related

data not saving in textbox properties.settings

I am working on a project where I want to save the text that is in the textbox when im closing the application, and when I open the application again I want it to be there.
I am trying to save the text data in the app.config as you can see in the code below but when I close the application and run it, the text I put in is not there.
Im really confused because when the form loads it runs this
textbox1.Text = Properties.Settings.Default.SavedText;
and it should pull up the "saved text" which is being saved when the form is calling the Closing event
Properties.Settings.Default.SavedText = textbox1.Text;
Properties.Settings.Default.Save();
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework;
using MetroFramework.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : MetroForm
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textbox1.Text = Properties.Settings.Default.SavedText;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.SavedText = textbox1.Text;
Properties.Settings.Default.Save();
}
}
}
I was also getting these errors before and I tried showing the potential fixes and use them but they didnt solve the issue with the text not saving.
This were the potential fixes.
I solved the issue by going into my project properties and adding this to the project settings. SavedText > String > User.

button in c# not firing

I am writing a simple code it has 3 buttons 2 that will need to open up other forms and one to close the program. When i start the program the exit button will not work even though i have it coded the same as any other time i have wrote a program.
When i press any of the buttons nothing happens. Im not 100% sure how to use the buttons to open another form but i know the exit button should work as is.
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _3343_ParksJ_Lab02
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void workerButton_Click(object sender, EventArgs e)
{
WorkerForm workersForum = new WorkerForm();
}
private void suppervisorButton_Click(object sender, EventArgs e)
{
SupervisorForm workersForum = new SupervisorForm();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
You have to subscribe your buttons' click events to the event methods before they'll fire correctly.
You can check the Designer.cs file to see if this has already been done, though I'm guessing it hasn't. You'll be looking for something like this:
this.workerButton.Click += new System.EventHandler(this.workerButton_Click);
One way to do so is directly in the constructor:
public MainForm()
{
InitializeComponent();
workerButton.Click += workerButton_Click;
suppervisorButton.Click += suppervisorButton_Click;
exitButton.Click += exitButton_Click;
}
Normally, I'd do this through the designer. Select each Button in turn, then open the properties panel and double-click on the event you wish to subscribe to, which will create the corresponding event method in the code-behind for you.
Look at .Designer.cs file and make sure your button is adding the correct delegate method. In your case it should exitButton_Click.
Sometimes when you change names of a button VS designer does not make the name change correctly in the .Designer file. Very rare but it happens.

Drag and drop from windows explorer to custom C# app that actually works with Windows 7

I've attempted (using dozens of examples) to create a simple WinForms app that will allow me to drag-and-drop a simple text file into an input box on a custom DotNet program.
Unfortunately, there appears to be no examples that actually work in Windows 7.
Here's a simple example (from another post that's been referenced all over the place) but it does not work at all.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace DragAndDropTestCSharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.AllowDrop = true;
this.DragEnter += Form1_DragEnter;
this.DragDrop += Form1_DragDrop;
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
foreach (string fileLoc in filePaths)
{
// Code to read the contents of the text file
if (File.Exists(fileLoc))
{
using (TextReader tr = new StreamReader(fileLoc))
{
MessageBox.Show(tr.ReadToEnd());
}
}
}
}
}
}
}
Could this be a UAC issue? If so, why does every other app in the world seem to do this simple, yet elusive feat of drag and drop with UAC on?
Does someone have a real, working example of getting this to work in Windows 7?
I've tried your sample and it works fine.
Check if you have hook the Load event to the form object to the Form1_Load handler and your namespace is the same.
this.Load += new System.EventHandler(this.Form1_Load);
or via properties editor:
Ok, I discovered that running VS under Administrator (which I had to do for another project) was the culprit. Loading VS in normal user mode, it works fine with UAC on.
Thanks all for your input!

Program in C# to read a file:Button not working

Here is my code, for some reason button two doesn't fire, button one does and when I place the code from button 2 into one it works there. What am i missing about the syntax to get buttons one and two to both work on click? I am about 2 weeks into learning c# so this is all new to me, I do not see why this code should not work.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
string filePath = null;
public Form1()
{
InitializeComponent();
}
//Method to check database connection
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("button1.Click was raised.");
}
//Method to select a file
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK)
{
filePath = file.FileName;
}
}
}
}
I assume that the event handler isn't subscribed (anymore). So have a look at the partial class of Form1 in the auto generated file Form1.Designer.cs. There must be somewhere this:
this.button1.Click += new System.EventHandler(this.button1_Click);
// is this missing?
this.button2.Click += new System.EventHandler(this.button2_Click);
How to: Subscribe to and Unsubscribe from Events (C# Programming Guide)
Make sure button2 is bound.
From the designer, select the button then go to the properties window. Click the lightning bolt, and make sure the click event is bound to button2_Click.
Alternative method is right-clicking on InitializeComponent() and select "Go to definition" (brings you to Form1.designer.cs) and look for the following:
button2.OnClick += new EventHandler(button2_Click);
If you've confirmed it's bound, we'll need to see more than what you've shown to determine the problem.

Problem logging in programatically to a website using WebBrowser Control

I'm trying to login programatically to https://www.salesgenie.com/Account/LogOn using WebBrwoser control.
The problem is when I click on "Log On", the browser doesn't navigate to the next page in the LogonCompleted event.
HtmlElement userName = wBrowser.Document.GetElementById("username");
userName.SetAttribute("value", SomeUserName);
HtmlElement password = wBrowser.Document.GetElementById("password");
password.SetAttribute("value", SomePassword);
wBrowser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(LogonPageLoaded);
wBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(LogonCompleted);
HtmlElement logonForm = wBrowser.Document.GetElementById("logon-submit");
logonForm.InvokeMember("click");
I think this is because the element "logon-submit" calls a JavaScript function or so.
Please, any help would be appreciated.
Check out your 4th line, you're setting userName again when you should be setting password
EDIT
Besides the problem above I'm guessing that you're trying to invoke the click too soon. The button is created using JavaScript so you have to wait a bit before clicking it. Unfortunately there's no event that you can listen for to determine when the JavaScript is done although you could test various properties probably. The safest thing is to probably just wait a couple seconds after load before invoking click.
The code below works for me (although I don't have a valid username and password). I've got one button called button1 and one webbrowser called webBrowser1. Once the page is visually loaded in the browser clicking the button on the form correctly invokes the click event in the browser.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate("https://www.salesgenie.com/Account/LogOn");
}
private void button1_Click(object sender, EventArgs e)
{
string SomeUserName = "Test";
string SomePassword = "Test";
HtmlElement userName = webBrowser1.Document.GetElementById("username");
Console.WriteLine(userName.GetAttribute("value"));
userName.SetAttribute("value", SomeUserName);
userName.RemoveFocus();
HtmlElement password = webBrowser1.Document.GetElementById("password");
Console.WriteLine(userName.GetAttribute("value"));
password.SetAttribute("value", SomePassword);
HtmlElement logonForm = webBrowser1.Document.GetElementById("logon-submit");
logonForm.InvokeMember("click");
}
}
}
Did you try using logonForm.click(); ? Without invoking any member ?

Categories