I have written some code to post messages to a site. It works just fine (for the first instance), the problem is that it keeps looping (running the SendData method over and over) once it is in the webBrowser1_DocumentCompleted method. So I must not be handling the event correctly. After it has run the SendData call one time I want it return to the button1_Start_Click event from which it originally started.
private void button1_Start_Click(object sender, EventArgs e)
{
GetData();
}
private void GetData()
{
webBrowser1.Navigate(inputURLID);
}
private void SendData()
{
webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2_Subject.Text);//To (username)
webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject
webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
SendData();
}
I'll have to post, the answer you selected is going to get you in deep trouble. The workaround is simple, you just need a variable that tells you that the next DocumentCompleted event is the one you are interested in. Like this:
private bool WaitingForData;
private void GetData()
{
webBrowser1.Navigate(inputURLID);
WaitingForData = true;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (WaitingForData) SendData();
WaitingForData = false;
}
The problem is that when you do the click on Submit, a new page is loaded and DocumentCompleted is called again for this new page.
You can try something like this:
bool documentCompleted = false;
private void button1_Start_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(inputURLID);
WaitForDocumentCompleted();
SendData();
WaitForDocumentCompleted();
}
private void WaitForDocumentCompleted()
{
while (!documentCompleted)
{
Thread.Sleep(100);
Application.DoEvents();
}
documentCompleted = false;
}
private void SendData()
{
webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2_Subject.Text);//To (username)
webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject
webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
documentCompleted = true;
}
Related
I have an entry_TextChanged event handler and a button.
How can I say if this button clicked go to that entry_TextChanged handler again?
private bool save = false;
private void entry_TextChanged(object sender, TextChangedEventArgs e)
{
if (save == true)
{
// Do Something
}
}
private void goto_Clicked(object sender, EventArgs e)
{
save = true;
// GO and do the entry texchanged??
}
you can just call it the same as any other function entry_TextChanged(null, null) or any other values that you want to send to the function
private void goto_Clicked(object sender, EventArgs e)
{
save = true;
entry_TextChanged(null, null)
}
private void Form1_Load(object sender, EventArgs e)
{
webbrowser.Navigate(url);
}
private async void buttonDownload_Click(object sender, EventArgs e)
{
await Task.Run(()=> {
MessageBox.Show(webBrowser.Document.GetElementsByTagName("body")[0].InnerHtml);
});
}
The error displayed "The specified conversion is not valid" i read about it and it something because is another thread not ui thread, do you know a simple and short way to make this work ?
Edit
You must subscribe to webBrowser.DocumentCompleted event
webBrowser.DocumentCompleted += completed;
private void completed(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (sender is WebBrowser w)
{
var a = w.Document.GetElementsByTagName("body")[0].InnerHtml;
}
}
private async void buttonDownload_Click(object sender, EventArgs e){
webBrowser.Navigate(url);
}
I would rewrite your download method like this (I assume the webBrowser object is doing some 'long' computation and you don't want to block the UI thread)
private async void buttonDownload_Click(object sender, EventArgs e)
{
var htmlString = await Task.Run(()=> {
return webBrowser.Document.GetElementsByTagName("body")[0].InnerHtml;
});
MessageBox.Show(htmlString);
}
Also, are you sure that is the correct error? perhaps you overlooked some inner exception, which could explain more
I want to know the the task status runnning in A button event from the other button click.
Like this.
private void button1_Click(object sender, EventArgs e)
{
Task.Run(()=>{
//The method to take long time
//For example
Thread.Sleep(5000)
;});
}
private coid button2_Click(object sender, EventArgs e)
{
//until 5000ms
//the method to know the above task status (Runnning....)
//after 5000ms
//the method to know the above task status (Conpleted....)
}
It's not entirely clear what you're trying to achieve, but if you want to check the status of the task, you can get its reference when calling Task.Run()
private Task _task;
private void button1_Click(object sender, EventArgs e)
{
_task = Task.Run(() => Thread.Sleep(5000));
}
private void button2_Click(object sender, EventArgs e)
{
if (_task?.Status == TaskStatus.RanToCompletion)
//do something
}
The answer is as vague as the question, so if you could give me a bit more details, I could come up with a better more tailored answer.
Again, it's very unclear what you're looking for here...but this is an expanded version of Faylit's example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Task T = null;
private void Form1_Load(object sender, EventArgs e)
{
updateStatus();
}
private async void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
await (T = Task.Run(() =>
{
updateStatus();
System.Threading.Thread.Sleep(5000);
}));
button1.Enabled = true;
updateStatus();
}
private void button2_Click(object sender, EventArgs e)
{
updateStatus();
}
private void updateStatus()
{
if (label1.InvokeRequired)
{
label1.Invoke((MethodInvoker)delegate
{
updateStatus();
});
}
else
{
if (T == null)
{
label1.Text = "Task not started.";
}
else if (!T.IsCompleted)
{
label1.Text = "Task running...";
}
else
{
label1.Text = "Task completed.";
}
}
}
}
It might give you some more ideas.
I have a windows form app that display restaurant orders. I want to load the code every 5 seconds to check if there is a new order to display.
I have a timer created in the form designer:
public void timer1_Tick(object sender, EventArgs e)
{
}
public void DisplayRestaurantOrder()
{
//Display restaurant order here
}
private void Form1_Load(object sender, EventArgs e)
{
DisplayRestaurantOrder();
timer1.Interval = 5000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (true)//check new order
{
DisplayRestaurantOrder();
}
}
1) Set the timer's Interval property to 5000 (milliseconds)
2) Create a method which loads the data e.g.
private void LoadOrders()
{
// ... do stuff here
}
3) In the timer's Tick event handler make a call to the load method, in this case LoadOrders:
public void timer1_Tick(object sender, EventArgs e)
{
LoadOrders();
}
4) In the Form.Load event do timer1.Start();, and maybe also a initial call to the load method, to make a Form.Load event handler just double click the form:
private void Form1_Load(object sender, EventArgs e)
{
//LoadOrders(); //this is the initial load call.
//timer1.Start();
}
as a result you should have something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadOrders();
timer1.Start();
}
public void timer1_Tick(object sender, EventArgs e)
{
LoadOrders();
}
private void LoadOrders()
{
// ... do stuff here
}
}
UPDATE (sins the OP wants to load what is in the constructor):
If what is needed to be loaded, is in the Form1 constructor then just move everything from in there to a new method and make a call to that method in both the timer1_Tick handler and in the constructor itself, e.g.:
public Form1()
{
//InitializeComponent();
Load();
}
//should be kept as to start the timer.
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
public void timer1_Tick(object sender, EventArgs e)
{
Load();
}
private void Load()
{
//InitializeComponent(); //this shouldn't be called more than once as it can create duplicate objects, i.e. buttons, menu strips, etc.
// ... do other stuff here
}
I thought we should raise an event
This is what I have found:
event OnButtonClicked ()EventArgs;
HTMLButtonClickEventArgs:EventArgs
{
String ButtonName;
}
I am doing a web Browser control and this is the code I wrote for its button clicked,but I want to know on which button user clicks:
public delegate void ButtonPressedEventHandler(object sender, EventArgs e);
public event ButtonPressedEventHandler ButtonPressed;
void OnButtonPressed()
{
if (ButtonPressed != null)
ButtonPressed(this, new EventArgs());
}
I write an example for you:try this:
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<html><body><button id=\"btn1\" type=\"button\">Click Me!</button><button id=\"btn2\" type=\"button\">Click Me!</button></body></html>";
}
Call Click event:
//Edited
bool First_Call = true;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (First_Call)
{
webBrowser1.Document.Click += new HtmlElementEventHandler(Document_Click);
First_Call = false;
}
}
Get Active Element When User click on document but
void Document_Click(object sender, HtmlElementEventArgs e)
{
// **Edited**
//Check Element is Button
if (webBrowser1.Document.ActiveElement.TagName == "BUTTON")
{
MessageBox.Show(webBrowser1.Document.ActiveElement.Id);
}
}