I am a starter with c# programming language. I placed a simple web browser into a window form. I assign a url address to the browser and I want to see if the browser successfully opened the link I provided.
I know that there is a eventhandler called
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
however, after assigning the url for the browser, I want to write something like
if (webBrowser1_DocumentCompleted)
{
//my code here
}
is this possible? I know you can use "WebBrowserReadyState" but I would prefer to try and use Document ready.
I'm not sure if this is what you are looking for but this is what I would try:
first create an Event Handler in the Constructor of your form class:
public void Form1()
{
webBrowser1.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(WebDocumentCompleted);
}
After this you need to create a method that will be called when that event is fired:
void WebDocumentcompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//Your code here
}
Hope this helps!
Because loading and rendering of the webpage is running asynchronously you have to do you logic (which should run after the document is loaded) in the event method. You can subscribe the event in this way:
webBrowser.DocumentCompleted += webBrowser_DocumentCompleted;
You have to have a method in your class with this signature in which you can make the coding you want:
void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Do something after the document is loaded.
}
You can inspect the result from DownloadDataCompletedEventArgs (e)
class Program
{
static void Main(string[] args)
{
WebClient wb = new WebClient();
wb.DownloadDataAsync("www.hotmail.com");
wb.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wb_DownloadDataCompleted);
}
static void wb_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
if (e.Cancelled)//cancelled download by someone/may be you
{
//add necessary logic here
}
else if (e.Error)// all exception can be collected here including invalid download uri
{
//add necessary logic here
}
else if (e.UserState)// get user state for asyn
{
//add necessary logic here
}
else
{
//you can assume here that you have result from the download.
}
}
}
Related
I'm in College and this is my first (major) project.
I'm trying to perform an action when a form is closed. I don't seem to be getting the terminology right when searching online, or the answer given doesn't match what I want to do.
At the moment i'm declaring a Class and displaying the from -
private void createuser_Click(object sender, EventArgs e)
{
User_Modification mod = new User_Modification("Create", "Create");
mod.ShowDialog();
}
What I want to do is this -
WHEN mod IS CLOSED {
// Do stuff
}
You're using ShowDialog, so the code following it is not executed until after the dialog box is closed. mod.ShowDialog(); doStuff(); will work pretty well.
You need to create a handler to capture the FormClosed event:
In your constructor do:
this.FormClosed += Form_Closed;
Then in the body of your form, add this method.
private void Form_Closed(object sender, FormClosedEventArgs e)
{
// Do stuff
}
You should attach handler to FormClosed event:
private void createuser_Click(object sender, EventArgs e)
{
User_Modification mod = new User_Modification("Create", "Create");
mod.FormClosed += new FormClosedEventHandler(FormClosed);
mod.ShowDialog();
}
void FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("Closed");
}
if you're using WinForms you can override OnFormClosing event:
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
// your code...
}
You'll want to take a look at two events:
Form.FormClosing : https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing(v=vs.110).aspx
Form.FormClosed : https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed%28v=vs.110%29.aspx
First one will allow you to perform actions prior to the form being closed completely, such as canceling the closing procedure. The second one is what you would use if you want to perform actions after the form is closed (perhaps to clean up resources, as an example).
So, as an example, let's say that you want to perform an action when the form is in fact closed:
// Somewhere in your code where you create the form object.
form.FormClosed += Form_FormClosed;
// Somewhere else in your code.
private void Form_FormClosed(Object sender, FormClosedEventArgs e)
{
MessageBox.Show("Form closed");
}
Am trying to submit a webform pragmatically on button1 click. In first attemp ie after launching my webbrowser from visual studio it automatically submit the form but again clicking on button1, webpage loads but submit button is not invoked. While debugging its shows that the line of code executed but no action takes place.
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("xxxx");
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.Url.OriginalString.ToString() == "xxxx")
{
if(webBrowser1.ReadyState==WebBrowserReadyState.Complete)
{
HtmlElementCollection doc = webBrowser1.Document.All;
foreach (HtmlElement ele in doc)
{
if (ele.GetAttribute("name").ToString()== "username")
{
ele.SetAttribute("value","xxxx");
}
if (ele.GetAttribute("name").ToString() == "password")
{
ele.SetAttribute("value", "xxxx");
}
if (ele.GetAttribute("classname") == "btn")
{
ele.InvokeMember("click");
}
}
}
}
textBox1.Text = webBrowser1.DocumentText;
}
You only want to setup the DocumentCompleted event for the web browser once. In your code you keep adding to the event chain each time the button is pressed which is not the behaviour you want.
You want something like (pseudo code as haven't got VS with me):
// When the form/parent loads bind the event ONCE here.
public void FormLoads()
{
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
// Just navigate here and the event will still be raised
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("xxxx");
}
Its Due to your webBrowser1_DocumentCompleted Event is not working when your click on Button.
Just Create one method
Private Void Submit(); and here put your code which you want to perform on click event and call this method from two location one is at button1_click and another webBrowser1_DocumentCompleted so that your code is run at both events.
I am building my first app for windows application. My requirement is that- On clicking a button i want to navigate to another page and in that page i want the data to be displayed directly from a soap web service also by performing xml parsing.
Button event code:
private void button1_Click(object sender, RoutedEventArgs e)
{
KejriwalService.arvindSoapClient client = new KejriwalService.arvindSoapClient();
client.getarvindNewsCompleted += new EventHandler<KejriwalService.getarvindNewsCompletedEventArgs>(client_getarvindNewsCompleted);
}
void client_getarvindNewsCompleted(object sender, KejriwalService.getarvindNewsCompletedEventArgs e)
{
textBlock1.Text = e.Result.ToString();
}
I am not getting any result from here. Can anyone please help. I want to extract 3 text fields and 1 image from this web method
Just check your web service. Your code is perfect but you need to change your web service. Donot return string from your web method but return the xml
You had associate the delegate but you have not called the method. You probably have a method like
KejriwalService.arvindSoapClient.DoSomethingAsync()
This will fire the event and after that it will trigger the client_getarvindNewsCompleted method when the responde from the WebService comes.
Edit
Just remember to use the [WebMethod] attribute in you WebService method.
public class Service1 : System.Web.Services.WebService
{
[System.Web.Services.WebMethod]
public string getarvindNews()
{
return "I am a string";
}
}
In you code you call this async like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
KejriwalService.arvindSoapClient client = new arvindSoapClient();
client.getarvindNewsCompleted += new
EventHandler<getarvindNewsCompletedEventArgs>(client_getarvindNewsCompleted);
//Call the method async and get its result in client_getarvindNewsCompleted
client.getarvindNewsAsync();
}
void client_getarvindNewsCompleted(object sender, getarvindNewsCompletedEventArgs e)
{
textBlock1.Text = e.Result.ToString();
}
I have a simple C # application that contains buttons and a web browser, each button execute a request and displays the result of the request on the web browser. And use the results of a request in the next button.
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.test.com");
}
private void button2_Click(object sender, EventArgs e)
{
if (webBrowser1.Document.GetElementById("tesxtbox1") != null)
{
HtmlElement txt1 = webBrowser1.Document.GetElementById("tesxtbox1");
txt1.SetAttribute("value", "test");
webBrowser1.Document.Forms[0].InvokeMember("submit");
}
}
Is there any method or way to perform the two buttons with a single click, but the second button, do not execute until the web browser is loaded.
in the first button, I added
webBrowser1.DocumentCompleted + = new WebBrowserDocumentCompletedEventHandler (Button2_Click);
but the second button excuted several times, so I added in the last line of the button 2:
webBrowser1.DocumentCompleted - = new WebBrowserDocumentCompletedEventHandler (Button2_Click);
it works, but in the console I find that Button 2 is execute twice
Thanks in advance!
You shouldn't think like executing second button click. There is DocumentCompleted event which you must listen to and execute neccesary code afterwards.
private void button3_Click(object sender, EventArgs e)
{
webBrowser1.DocumentCompleted += (sender, e) => {button2_Click(sender, e);};
button1_Click(sender, e);
}
will register the button2_Click method to be executed after the document is loaded completely as triggered by button1_Click().
You should add an event that will call the code once the document has completed loading.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
button2_Click(sender, e);
}
You could make the following call from your button1_click method.
button2_Click(sender, e);
You could give an argument in the url:
webBrowser1.Navigate("http://www.test.com?excuteSecondButton=Yes");
Then on the second page you check if that argument was given:
PageLoad()
{
if(Request.QueryString["executeSecondButton"].Equals("Yes"))
{
button2_Click(button2, new RoutedEventArgs());
}
}
I'm using the C# WebBrowswer control and I have a problem that when a button like "Next" is pressed when the page is not loaded yet the program tries to proceed but instead gives me a null error.
Is there is a function to make the program wait until the page has completed loading?
I tried to put a while loop in the program that checks the title of the html page but then the program freezes. Something like will freeze the program:
while(!webbrowser1.Document.Title.ToString().Equals("NextPageTitle"))
{
}
::NextCommands::
It Doesnt work, i tried that and the button "fblqf" is not clicked. but its not returns null error..
public void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("q").SetAttribute("value", "חחח");
webBrowser1.Document.GetElementById("btnK").InvokeMember("Click");
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// do the work you need to do now that that page has completed loading
webBrowser1.Document.GetElementById("fblqf").InvokeMember("Click");
}
Solution:
public void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("q").SetAttribute("value", "חחח");
webBrowser1.Document.GetElementById("btnK").InvokeMember("Click");
int x=0;
while (x==0)
{
System.Windows.Forms.Application.DoEvents();
if(webBrowser1.Document.GetElementById("pnnext") != null)
break;
}
webBrowser1.Document.GetElementById("pnnext").InvokeMember("Click");
webBrowser1.Document.GetElementById("q").Focus();
}
You need to hook the WebBrowswer.DocumentCompleted event:
Perhaps in your constructor or your OnLoad:
webBrowser1.Document.GetElementById("q").SetAttribute("value", "חחח");
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
webBrowser1.Document.GetElementById("btnK").InvokeMember("Click");
Then you event looks like this:
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// do the work you need to do now that that page has completed loading
}
I Found an easy Solution!!!
public void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("q").SetAttribute("value", "חחח");
webBrowser1.Document.GetElementById("btnK").InvokeMember("Click");
while (true)
{
System.Windows.Forms.Application.DoEvents();
if(webBrowser1.Document.GetElementById("pnnext") != null)
break;
}
webBrowser1.Document.GetElementById("pnnext").InvokeMember("Click");
webBrowser1.Document.GetElementById("q").Focus();
}