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 I 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");
}
}
my question is to find 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 my first solution, I added in the first button :
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're approaching this issue the wrong way. First you are not looking for a way to click two buttons. You are looking for a way to click one button and execute an operation if a condition is met.
Basically you just need to call button2.PerformClick() in your WebBrowser DocumentCompleted method. If however you want to ensure that button1 was clicked, you can set a bool in button1 Click method and reset it in the button2 Click method.
If you need to perform more than one button click in your document complete method after the first button click, you can add them to your DocumentCompleted method like this:
First subscribe normally.
webBrowser1.DocumentCompleted += Document_Completed;
Normally you can generate the method stub by pressing TAB after subscribing to the event. The method could be as follows:
private void Document_Completed(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//here you add the other buttons that must be clicked when document complete
button1.PerformClick(); button2.PerformClick();//etc
//you could even iterate all over the buttons on the form and click each of them if it meets a certain condtition.
}
I hope that answers your question :)
DocumentCompleted is executed each time your WebBrowser control finishes downloading a document.
You only need to subscribe once.
Related
I have a Main Window, which is the frame that displays different pages in my project, I would like when a button on a page is clicked, to change the page in the frame.
The problem is I can't seem to get the Frame to contain the event handler for when the button is clicked, it seems local to the page itself. Is there a way for the Frame to see when the page's button is clicked and change the page accordingly?
Main.Content = new Page.Intro();
However when the button Next is clicked within Page.Intro() I want within the Main Window Class to the display
Main.Content = new Page.Home(userinformation);
I tried to follow How to click a button of the page to change the source of the frame of Windows? which is similar to what I'm trying to achieve, although my pages have parameters I need to pass so I can't call MainContent.Source = new Uri("Home.xaml", UriKind.Relative);as there is no where to pass userinformation.
as there is no where to pass userinformation
Navigate method not only takes a Uri, it has an overload that takes an object as parameter. So this works for you
//button click in Page.Intro
private void Button_Click(object sender, RoutedEventArgs e)
{
this.NavigationService?.Navigate(new Page.Home(userinformation));
}
I program in Visual Studio 2013 C#.
I want to have a functionality to disable for short time a button.
I tried button.Enabled=false, but I see that when I click it during it is disabled then the click action starts right after I get that button enabled in other place of the program.
How to clear that event or block them when button is disabled?
Best regards,
Krzysztof
for disable button click.
button1.Click -= button1_Click; // here button1_Click is your event name
for enable button click.
button1.Click += button1_Click; // here button1_Click is your event name
This is the extremely simple way that I found on the internet and it works.
Disable the button(s)
Run whatever is needed to be performed on the procedure
Run this command Application.DoEvent();. This command enqueues all the click events (as many as the user clicked) and ignore/dispose them
Enable the button(s) at the end
Good Luck
Can't you just check for button's state before executing commands in the method?
For example:
void Click(object sender, EventArgs e)
{
if(!(sender as Button).Enabled)
return;
//other statements
}
Try
YourButton.Attributes.Add("onClick", "");
To remove its onClick altogether.
and then
YourButton.Attributes.Add("onClick", "YourButton_Click");
To add it again.
But your program shouldn't execute the Click when you say it does. It's likely there's something wrong in your code's logic.
Look at this code below, after click button2, button 1 is disabled. while button 1 is disabled, click button 1. After button 1 enable automatically after 5 seconds textBox1 update to "Update".
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "Update";
}
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = false;
Thread.Sleep(5000);
button1.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
btn.Visibility = Visibility.Collapsed;
You can try this.
ALL WRONG!
"button1_Click" is not the standard handler behind a button, so it cannot be removed. That is your own function.
YourButton.Attributes does not exist at all.
Nobody even mentioned "Web Forms".
Your description does not match the Windows Forms Button behavior.
The solution for the question you really asked is:
Use a usual Windows Forms Button or override the click event to check again if the button is enabled.
I need to know if I can press a picturebox two times or more in the same run time to do a function as many time as the user want ?
I mean that to run the method more than one time
"Click action in my program occur in the first time i click and i want it to be every time the picture box is clicked. "
private void img1_Click(object sender, EventArgs e)
{
//...
}
I try to but it runs in the first press only
Any suggestions?
Click action will occur every time picture box is clicked. But if you are asking about different methods like doing something on click and something different on double click then you can define 2 different methods for click and doubleclick.
If you want do call some function for every time he clicks and the UI needs to be uninterrupted, you may want use ThreadStart
Here is a sample hope that is what you want. And please elaborate on your question.
private void pictureBox1_Click(object sender, EventArgs e)
{
new ThreadStart(YourFunction).BeginInvoke(null, null);
}
private void YourFunction()
{
Thread.Sleep(5000);
MessageBox.Show("YourFunction called!");
}
What I have going on is a Invokemember("Click"), the problem is I want to be able to grab the resulting innerhtml. The problem is i'm unsure of how/if it's possible to wait until the resulting action of the invokemember("click") is resolved. Meaning, in a javascript when you perform this click it will take you ot the next 20 items listed. However, i'm unsure of how to tell when that javascript will be fully loaded. Below is what I'm using.
private void button1_Click(object sender, EventArgs e)
{
HtmlElement button = webBrowser1.Document.GetElementById("ctl08_ctl00_InventoryListDisplayFieldRepeater2_ctl00_BlockViewPaging_Next");
button.InvokeMember("click");
HtmlElement document = webBrowser1.Document.GetElementsByTagName("html")[0];
}
One possible solution is to modify your "click" event handler in javascript so it changes the value of some hidden input field right before exiting the method (after all work is done). You can attach to the event of changing field from C# code and act when it's fired.
// Your init method - you can call it after `InitializeComponent`
// in the constructor of your form
Init() {
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
webBrowser1.Document.GetElementsByTagName("statusField")[0].AttachEventHandler("onchange", WorkDone);
}
void WorkDone(object sender, EventArgs e) {
HtmlElement document = webBrowser1.Document.GetElementsByTagName("html")[0];
}
That's the raw solution, I haven't yet checked whether "onchange" is a correct DOM event.
Also, you can't attach to DOM events before the document is completely loaded, that's why I put the attaching logic in the handler of DocumentCompleted event.
I have a GUI app, that has a button added. Within several plugin dll's, a new button is created and needs to be added to the GUI in place of the existing one.
Is there a way to simply say ButtonA = ButtonB? Or do I have to remove the button from the GUI at runtime and then add the new one?
Thanks.
Or you can just link it to another handler, something like:
your old Click event handler
private void ButtonA_Click(object sender, EventArgs e)
{
//Do sth
}
your new Click Event handler (like if you create a new button)
private void ButtonB_Click(object sender, EventArgs e)
{
//Do sth
}
then you need to remove the first handler and add your new handler:
ButtonA.Click -= this.ButtonA_Click;
ButtonA.Click += new EventHandler(ButtonB_Click);
You button is added to some parent (the Form for example). That means you have to remove the original button from the Form's Controls collection, and add the new button. Or, you can replace the button inside the Controls collection.
Let's say after iterating through the collection, you find that the button is the 5'th element, you can do something like this:
this.Controls[4] = ButtonB;
I'm not 100% sure if this means that you will have to manually invalidate the screen to update the GUI. In that case call ButtonB.Invalidate();
Copy properties you are interested in from the first button to the second that is created, and then either remove first button from the Controls collection or set .Visible to false.
As for events, you'll have problem there, because you'll have to call old button event handlers manually - there is no way in .NET to read OnClick (or any other event) subscribers.