I am new to programming. I am using window form in VisualStudio C#.
My problem is after clicking the first button in my Window Form, It opens the browser and go to Url that I want to login and after that when I click the Second button on my Window Form, it doesn't run the second block of codes. I don't get any error message.
Can anyone help me because I am totally a beginner. Thank you so much in advance!
public partial class Form1 : Form
{
IWebDriver driver = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
driver = new FirefoxDriver();
driver.Url = "https://accounts.google.com/ServiceLogin";
driver.Manage().Window.Maximize();
}
private void button2_Click(object sender, EventArgs e)
{
driver.Url = "https://accounts.google.com/ServiceLogin";
var email = driver.FindElement(By.Id("Email"));
email.SendKeys("-------------");
var password = driver.FindElement(By.Id("Passwd"));
password.SendKeys("---------");
password.FindElement(By.Id("signIn"));
Add button2.Click += button2_Click; to your Form constructor, right after InitializeComponent();. This line adds the event handler button2_Click to the event Button.Click of button2.
Normally, this kind of stuff does the designer for you. If you prefer this way, go to the preview page of your Form, then to the property manager, click the lightning "events" and double click your desired event, in this case Click. Having this done, the method body for the button2 click event handler will be generated.
Related
So, I'm making a payroll management system as a hobby project to help my resume and general knowledge of c#. So, I'm making a UI and I can open a new window just fine with this code:
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct();
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.Show();
this.Hide();
}
however, I don't know the event to check when the little red "x" button is clicked, because when that button is clicked, I want to go back to the main screen because I hide the main screen when that button is clicked, and when i click the red "x" on the screen that just opened, it closes, but the application continues to run in the background.
If there is some better way to manage multiple menus, I'm open to suggestions, however, this is what I've found easiest.
Thanks in advance
I second Robert Harvey's suggestion; this gives the user the reassurance tha tht emain window is still open/ nothing got lost, but it's unreachably "behind" the CreateAdminAcct form while the CreateAdminAcct form is open
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct();
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.ShowDialog();
//do any code here that needs to access createAcct before it's lost
MessageBox.Show(createAcct.NewAdmin.Name);
}
If you really do want to hide your main form, pass the main form itself to createAcct, and make it createAcct's job to re-open the main form when it is closing
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct(this); //note passing this form to constructor
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.Show();
}
class CreateAcctForm : Form{
private Form _showWhenClosing;
CreateAcctForm(Form revertTo){
InitializeComponent();
_showWhenClosing = revertTo;
}
}
void Form_Closing(object sender, ...){ //event
_showWhenClosing.Show();
}
Side note: please rename your controls after you drop them ona form. code that's stuffed with label57, textbox25 is effectively obfuscated and really wearisome to follow
In my WPF application, I have one main window (Window.xaml). Which has a button, if user click the button we want to open same window again(Window.xaml). Once again the user clicks the same button we want to open the same window again.
so how do I open the same window again?
You can create new instance of your current window in button click event and achieve it.
Code snippet:
private void button_Click(object sender, RoutedEventArgs e)
{
var currentWindow = new Window();
currentWindow.Show();
}
You have to create a new instance of the window (e.g. MainWindow class) and call the show method.
Just some pseudo code:
void Button_clicked()
{
new MainWindow().Show();
}
I want to run some logic when the user presses the "x" in the upper right hand corner of my Windows Form application. There is a logout button but I am confident the user will not always logout. So I will run same logic on the click of "x". I have the following, but it will not hit the breakpoint.
Code
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//sql server queries to be executed
lblSucessMessage.Text = "Succesfully logged out";
}
Easiest way is to go to the Form's properties, find the FormClosed event, and double click on it. It will add the event to the Form and create the corresponding method. Move your code to the newly created method and delete the method you wrote.
That should get you rolling.
You have 'closed' and 'closing' events, that you can just implement in the constructor. Example:
public Form1()
{
InitializeComponent();
Closed += (sender, args) =>
{
/*Handle event*/
};
Closing += (snd, args) =>
{
/*Handle event*/
};
}
You need to attach handler to event. This can be done in constructor (as well as in designer of your form);
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormClosing += Form1_FormClosing;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//sql server queries to be executed
lblSucessMessage.Text = "Succesfully logged out";
}
}
EDIT: If I change in Home Form private to public void then I must do a kinda concvert to bool from void... but I don't know how that works. Can you help me guys?
I am stuck here in the code.... I wanted to know how to access to my other form which has menustrip from another form.
E.G:
I want that clicking on the menustrip from other form where menustrip doesn't exists.
Here is the code:
Form 1
Home frm = new Home();
frm.IsMdiContainer = true;
if(frm.Controls["todasEntradasToolStripMenuItem"].Click += frm.todasEntradasToolStripMenuItem_Click)
{
{something}
}
The form Home is "frm" variable and it is where it has the menu strip. I want help to change the protection level so that this form (Form1) can accept this code... Anyone can help me please?
Solution 1 (nice):
Add your Click event in some Init-method or the constructor in Home. There you can access your control.
todasEntradasToolStripMenuItem.Click += todasEntradasToolStripMenuItem_Click;
Also in Home you define a new event:
public event EventHandler<EventArgs> TodasEntradasToolStripMenuItemClick;
private void OnTodasEntradasToolStripMenuItemClick(EventArgs e)
{
if (todasEntradasToolStripMenuItem != null)
{
TodasEntradasToolStripMenuItemClick(this, e);
}
}
In the Click handler you raise your own public event:
private void todasEntradasToolStripMenuItem_Click(object sender, System.EventArgs e)
{
OnTodasEntradasToolStripMenuItemClick(e);
}
In Form1 you add your Handler to this public event:
Home frm = new Home();
frm.TodasEntradasToolStripMenuItemClick += frm_TodasEntradasToolStripMenuItemClick;
In this handler you can "do something":
private void frm_TodasEntradasToolStripMenuItemClick(object sender, EventArgs e)
{
// Do something
}
Solution 2 (do not do it):
You asked for changing the protection level. So you can change
private todasEntradasToolStripMenuItem
in Home to
internal todasEntradasToolStripMenuItem
or even
public todasEntradasToolStripMenuItem
But I do not suggest you not to do this. You should choose Solution 1. With Solution 2 you would open Home for more changes than you have to.
On form1, I have registerButton that create new registerForm with an acceptButton on it. Both dynamically created:
private void registerButton_Click(object sender, EventArgs e)
{
registerButton.Enabled = false;
Form registrationForm = new Form();
registrationForm.Text = "Register new account";
registrationForm.Visible = true;
Button createButton = new Button();
createButton.Text = "Accept";
registrationForm.Controls.Add(createButton);
createButton.Click+= new EventHandler(createButton_Click);
}
How can I close registerForm after clicking acceptButton without closing the form1?
You've lost the reference to the registration form instance. But you can always get it back from the sender argument that's passed to the Click event handler. Like this:
private void registrationButton_Click(object sender, EventArgs e) {
var btn = (Control)sender;
btn.FindForm().Close();
}
For your code (which I don't recommend to use) fix will be
createButton.Click += (s,e) => registrationForm.Close();
When you attach this lambda as event handler, you have opportunity to capture registrationForm instance in a closure. Thus form instance will be available when click event will happen, and you will be able to close this form.
Better approach: instead of adding button dynamically to form, place this button statically in designer and attach click event handler which will close the form:
private void acceptButton_Click(object sender, EventArgs e)
{
Close();
}
Usage of registration form will be simple as:
private void registerButton_Click(object sender, EventArgs e)
{
Form registrationForm = new Form();
registrationForm.Show();
}
Add this.Close() at the click event of acceptButton.
You can attach an event to it dynamically. While creating the button, do acceptButton.Click += new System.EventHandler(accepButton_click); and create matching function or press Tab twice after doing the +=.