How to click a button through coding? - c#

I have two buttons in my program and i want that when i press first button the second button is clicked automatically ( in the event handler of first button , i want to press the second button through coding).
private void button1_Click(object sender, EventArgs e)
{
passWord = pwd.Text;
user = uName.Text;
loginbackend obj = new loginbackend();
bool isValid = obj.IsValidateCredentials(user, passWord, domain);
if (isValid)
{
loginbackend login = new loginbackend();
passWord = pwd.Text;
login.SaveUserPass(passWord);
HtmlDocument webDoc = this.webBrowser1.Document;
HtmlElement username = webDoc.GetElementById("__login_name");
HtmlElement password = webDoc.GetElementById("__login_password");
username.SetAttribute("value", user);
password.SetAttribute("value", passWord);
HtmlElementCollection inputTags = webDoc.GetElementsByTagName("input");
foreach (HtmlElement hElement in inputTags)
{
string typeTag = hElement.GetAttribute("type");
string typeAttri = hElement.GetAttribute("value");
if (typeTag.Equals("submit") && typeAttri.Equals("Login"))
{
hElement.InvokeMember("click");
break;
}
}
button3_Click(sender, e);
label1.Visible = false ;
label3.Visible = false;
uName.Visible = false;
pwd.Visible = false;
button1.Visible = false;
button2.Visible = true;
}
else
{
MessageBox.Show("Invalid Username or Password");
}
}
private void button3_Click(object sender, EventArgs e)
{
HtmlDocument webDoc1 = this.webBrowser1.Document;
HtmlElementCollection aTags = webDoc1.GetElementsByTagName("a");
foreach (HtmlElement link in aTags)
{
if (link.InnerText.Equals("Show Assigned"))
{
link.InvokeMember("click");
break;
}
}
}

I think what you're describing is that you want to call a method when button B is clicked, but then also call that method when button A is clicked.
protected void ButtonA_Click(...)
{
DoWork();
}
protected void ButtonB_Click(...)
{
// do some extra work here
DoWork();
}
private void DoWork()
{
// do the common work here
}
Depending on your implementation in the event handlers, you can also just call the event handler of the second button from that of the first, but the above way is the 'right' way to do it.

You could just call the method.
private void btnA_Click(object sender, EventArgs e)
{
doA();
}
private void doA()
{
//A stuff
}
private void btnB_Click(object sender, EventArgs e)
{
doA();
doB();
}
private void doB()
{
//B stuff
}
Or call the _Click method directly;
private void btnB_Click(object sender, EventArgs e)
{
btnA_Click(sender, e);
doB();
}

I guess, you don't really care whether the button is clicked or not, you just care that the code of the second button is executed. So... just call it:
void button1_Click(...)
{
button2_Click(...);
}

Related

GO to onother task with butten clicked

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

How do I only use one button for this?

Alright, so I'm trying to practice my windows forms skills and I've been trying to figure out a simple thing such as making a button that says "Show message", when you click it, it makes an invisible label Visible that says "Hey man" and the text of the button should change to "Hide message".
When you click it the label should go invisible again. I tried to do it with if statements for a while but didn't really figure it out, so I just used 2 buttons which made it a million times more simple. I'm just wondering, how would I go about doing it with only 1 button?
My code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void btnShow_Click(object sender, EventArgs e)
{
lblShow.Visible = true;
btnShow.Visible = false;
btnHide.Visible = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnHide_Click(object sender, EventArgs e)
{
lblShow.Visible = false;
btnShow.Visible = true;
btnHide.Visible = false;
}
}
}
You could add a bool to your class, and change the bool when you click the button
bool showLabel = true;
private void btnToggle_Click(object sender, EventArgs e)
{
if (showLabel)
{
lblShow.Visible = true;
}
else
{
lblShow.Visible = false;
}
showLabel = !showLabel;
}
This can be done quite simply with only two lines of code:
private void btnToggle_Click(object sender, EventArgs e)
{
lblShow.Visible = !lblShow.Visible;
btnToggle.Text = lblShow.Visible ? "Hide Message" : "Show Message";
}
It can technically be done in one line, but I think it's less readable:
btnToggle.Text = (lblShow.Visible = !lblShow.Visible) ? "Hide Message" : "Show Message";
I suggest keeping a state variable and updating the form based on the value of the state variable. This is a flexible pattern where you can accommodate more than one state, which means you can have combinations of buttons that produce different results.
Try this:
public partial class Form1 : Form
{
public enum FormState
{
MessageHidden,
MessageVisible,
Default = MessageHidden
}
FormState state = FormState.Default;
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
UpdateUI();
}
protected void UpdateUI()
{
switch (state)
{
case FormState.MessageHidden:
label1.Visible = false;
button1.Text = "Show Message";
break;
case FormState.MessageVisible:
label1.Visible = true;
button1.Text = "Hide Message";
break;
default:
throw new NotImplementedException();
}
}
private void button1_Click(object sender, EventArgs e)
{
switch (state)
{
case FormState.MessageHidden:
state = FormState.MessageVisible;
break;
case FormState.MessageVisible:
state = FormState.MessageHidden;
break;
default:
throw new NotImplementedException();
}
UpdateUI();
}
}
Example screenshots
FormState.MessageHidden
FormState.MessageVisible
You can refactor using one method to set the label visibility like that:
private void btnToggle_Click(object sender, EventArgs e)
{
lblShow.Visible = !lblShow.Visible;
btnToggle.Text = lblShow.Visible ? "Hide Message" : "Show Message";
}
With the VS Form Desiner you can set the label Visible property to true and the button Text to nothing.
And in the FormLoad event you call the method to set the things :
btnToggle_Click(this, null);

How can we return the name of the button on which the user clicks in a webBrowser control?

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

break onclick event from method

Button's OnClick event is called:
private void vkb7_Click_1(object sender, EventArgs e)
{
//method
button_start(object sender);
// do stuff 2
}
Some method is triggered:
private void button_start(object sender)
{
bool some = true;
if (some)
{
return; // I'd also like to break "button click"
}
}
I'd like to break "button click" event from method - how do I do it?
Simple, let button_start() return a boolean:
private bool button_start(object sender)
{
bool some = ...;
return some;
}
private void vkb7_Click_1(object sender, EventArgs e)
{
//method
if (button_start(object sender))
return;
// do stuff 2
}
If you are determining in button_start whether actions should occur in vkb7_Click_1, consider making button_start return bool and then use the return value in vkb7_Click_1.
On the other hand, if you are encountering an error condition in button_start, throw an exception.
You can't really "block" the event as it has already been fired. You can however handle the click manually depending on the result.
private void vkb7_Click_1(object sender, EventArgs e)
{
bool result = button_start(sender);
if (result)
DoSomething();
else
DoSomethingElse();
}
private bool button_start(object sender)
{
bool some = true;
if (some)
return false;
return true;
}
First, Set the return type of button_start to bool
Second, If you want to pass sender of vkb7 button or properties of vkb7 button,
Then you should pass it like this:
button_start(sender)
So the code would be like this:
private void vkb7_Click_1(object sender, EventArgs e)
{
//method
if(button_start(sender))
// do stuff 2
}
Then in the method
private bool button_start(object sender)
{
bool some = true;
if (some)
{
return true; // I'd also like to break "button click"
}
else
return false;
}

Closing event handlers C#

I have two forms, Form 2 is inheriting from Form 1.
What I need to do is that when I close both Form 1 and Form 2 another form which asks if user is sure to quit appears. Then if user clicks Yes, another form which asks if the user wants to save the game appears if and only if the form which the user closes is Form 2 and not Form 1 since for Form 1 there is no saving necessary.
This is what I managed to do:
// These are the Form 1 closing and closed event handlers:
private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
SureClose sc = new SureClose();
sc.StartPosition = FormStartPosition.CenterScreen;
sc.Show();
}
private void GameForm_FormClosed(object sender, FormClosedEventArgs e)
{
MainMenu menu = new MainMenu();
menu.Show();
}
Then in Sure Close: // Please note that Tournament is Form 2 inheriting from GameForm (Form 1)
private void yesButton_Click(object sender, EventArgs e)
{
this.Hide();
if (GameForm.ActiveForm is Tournament)
{
SaveGame sg = new SaveGame();
sg.StartPosition = FormStartPosition.CenterScreen;
sg.Show();
}
else
{
GameForm.ActiveForm.Close();
}
}
private void noButton_Click(object sender, EventArgs e)
{
this.Hide();
}
// This is the SaveGame Form:
private void saveButton_Click(object sender, EventArgs e)
{
// Still to do saving!
}
private void dontSaveButton_Click(object sender, EventArgs e)
{
this.Hide();
GameForm.ActiveForm.Close();
}
The problem is that when in the yesButton event handler in SureClose Form I have GameForm.ActiveForm.Close(), this is going back to the GameForm Closing event handler therefore the SureClose dialog is appearing again.
I tried doing: if (e.CloseReason() == CloseReason.UserClosing)
but obviously it doesn't work either because the reason of closing will always be the user :/
How can I solve this?
Thanks a lot for any help !
Form1 :
private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(SureClose())
{
SaveChanges();
}
else
{
e.Cancel = true;
}
}
private bool SureClose()
{
using(SureClose sc = new SureClose())
{
sc.StartPosition = FormStartPosition.CenterScreen;
DialogResult result = sc.ShowDialog();
if(result == DialogResult.OK)
{
return true;
}
else
{
return false;
}
}
}
protected virtual void SaveChanges()
{
}
Form2:
protected override void SaveChanges()
{
using(SaveGame sg = new SaveGame())
{
sg.StartPosition = FormStartPosition.CenterScreen;
DialogResult result = sg.ShowDialog();
if(result == DialogResult.OK)
{
//saving code here
}
}
}
SureClose form and SaveGame form:
private void yesButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void noButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}

Categories