I have two forms Form1 and Form2.
Inside Form1 I call Form2 and I want to intercept user button click choice. If user on Form2 clicked on Ok or Cancel button, so I try
var editForm = new Form2();
editForm.ShowDialog();
if (editForm.DialogResult == DialogResult.OK)
{
MessageBox.Show("ok btn is pressed!");
editForm.Dispose();
}
else
{
MessageBox.Show("cancel btn is pressed!");
editForm.Dispose();
}
on Form2 I have click events
private void BtnOk_Click(object sender, EventArgs e)
{
_Repository.Create(mydata);
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
Display message is show only on Cancel button and not on Ok. What I'm doing wrong?
Set dialog result before closing.
private void BtnOk_Click(object sender, EventArgs e)
{
_Repository.Create(mydata);
DialogResult = DialogResult.Ok;
Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
On your Form1 :
using (Form2 editForm = new Form2())
{
editForm.ShowDialog();
if (editForm.DialogResult == DialogResult.OK)
{
MessageBox.Show("ok btn is pressed!");
editForm.Dispose();
}
else
{
MessageBox.Show("cancel btn is pressed!");
editForm.Dispose();
}
}
And on Form2 :
private void BtnOk_Click(object sender, EventArgs e)
{
_Repository.Create(mydata);
this.DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
Related
I have two forms, form 1 have textboxes, radioboxes, save button to save the data required and transaction list button to open the second form. Form 2 have listbox. I want help with save button to save data of textboxes and radioboxes and then clicking transaction list button to display them on listbox in form 2.
Form1
namespace form1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void trans_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog();
}
private void save_Click(object sender, EventArgs e)
{
}
}
}
Form2
namespace form1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void back_Click(object sender, EventArgs e)
{
this.Close();
Form1 frm = new Form1();
frm.ShowDialog();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
I usually add an Initialize in the second form that has parameters for each of the items you want to pass to the second form. In the trans_Click method, add a call to initialize(a,b,c,d). I would move the frm.ShowDialog(); also.
I have a form1 that opens a form2 that opens a form3. I want to return to form1 from form3 using a button.
form1
private void form2button_Click(object sender, EventArgs e)
{
this.Hide();
form2 f2 = new form2();
f2.ShowDialog();
this.Show();
}
form2
private void form3button_Click(object sender, EventArgs e)
{
this.Close();
form3 f3 = new form3();
f3.ShowDialog();
}
private void exitbutton_Click(object sender, EventArgs e)
{
this.Close();
}
form3
private void mainmenubutton_Click(object sender, EventArgs e)
{
this.Close();
}
private void backbutton_Click(object sender, EventArgs e)
{
this.Close();
form2 f2 = new form2();
f2.ShowDialog();
}
But when in form3, after clicking the back button it shows form2 but form3 is still in the background. I have fixed this by adding this.Hide(); before this.Close();
I would like to know the logic behind why this happening.
If I understand you right then this is what you need to in order to go back to the first form:
form1:
private void form2button_Click(object sender, EventArgs e)
{
this.Hide();
Account form2 = new Account();
form2.ShowDialog();
this.Show();
}
form2:
private void form3button_Click(object sender, EventArgs e)
{
this.Hide();
form2 f2 = new form2();
f2.ShowDialog();
this.Close();
}
private void exitbutton_Click(object sender, EventArgs e)
{
this.Close();
}
form3:
private void mainmenubutton_Click(object sender, EventArgs e)
{
this.Close();
}
private void backbutton_Click(object sender, EventArgs e)
{
this.Close();
}
Also is there a difference between this.Hide(); and Hide(); ?
No, there's no any difference
Is there a way to have it appear in the same position?
form2 f1 = new form2();
f1.Location = Location; //location of any form, it also clould be f3.Location if it is exists
f1.ShowDialog();
Also is there a difference between this.Hide(); and Hide(); ?
No, there isn't.
My problem is very simple: i have a combobox in form1, i have a button that open form2 to write into a textbox the new item to add. Here my code:
Form1:
public static string new_item;
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
}
Form2:
private void btn1_Click(object sender, EventArgs e)
{
Form1.new_item = textBox1.Text;
combobox.Items.Add(new_item);
this.Close();
}
But the new item is not added to my comobobox.
I tried to refresh th combobox but i have the same result.
Thank you.
You need to add the item to your ComboBox after closing Form2:
public static string new_item;
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
comboBox1.Items.Add(new_item); //this is missing in your code
}
But a better way would be creating a public property in Form2 to pass the string back:
public string Value { get; set; }
private void btn1_Click(object sender, EventArgs e)
{
this.Value = textBox1.Text; //pass the TextBox value to the property
this.DialogResult = DialogResult.OK; // Cancel would mean you closed/canceled the
// form without pressing OK-Button (btn1)
this.Close();
}
Than in Form1 you can access the property and add the new item:
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
if(f2.ShowDialog() == DialogResult.OK) //check the result
{
comboBox1.Items.Add(f2.Value);//Add the new item
}
}
Assuming combo's name is combobox.
Form1:
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
if (f2.ShowDialog() == DialogResult.OK)
combobox.Items.Add(f2.ItemValue);
}
Form2:
public string ItemValue {get {return textBox1.Text;} };
private void btn1_Click(object sender, EventArgs e)
{
Form1.new_item = textBox1.Text;
this.DialogResult = DialogResult.OK;
}
I have a button called btnChallenge. The desired action is when it is clicked, the form cannot be closed.
Here is what I have so far:
public void btnChallenge_Click(object sender, EventArgs e) { }
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// not sure on this if statement
if (btnChallenge.Click)
{
e.Cancel = true;
}
}
You could try it this way:
Declare a private variable inside a form:
private bool _closedFromMyButton;
Then on FormClosing event check that property:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (_closedFromMyButton) // If closed from MyButton, don't do anything. let the form close.
return;
Hide(); // Hide the form (or not, it's up to you; this is useful if application has an icon in the tray)
e.Cancel = true; // Cancel form closing
}
Then on some button click (if desired), put this code to close the form only from that button (or menuitem or toolbar button, etc.):
private void MyButtonClick(object sender, EventArgs e)
{
_closedFromMyButton = true;
Application.Exit(); // Or this.Close() if you just want to close the form.
}
You could define a variable which goes to true when you press the button and check on close if the variable is true
e.g.
private bool btnClicked = false;
public void btnChallenge_Click(object sender, EventArgs e)
{
btnClicked = true;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(btnClicked)
{
e.Cancel=true;
}
}
You can just call the this.Close() method, this will call the Form1_FormClosing event:
public void btnChallenge_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//code here...
}
If you want to prevent the users by closing your form just after they have pressed some other button, then this code will help you.
private bool close_state=false; // hold the button state
// method to change the close_state by button click
private void Button1(object sender, EventArgs e)
{
close_state = true;
// if required you can toggle the close_state using an if statement
}
// Then on FormClosing event check that property:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (close_state) {// If the button is pressed
e.Cancel = true; // Cancel form closing
}
}
You may implement some other way to close the form....
I am trying to call one EventHandler method from another. For example, I would like to link Logout button with form exit, so I have this code:
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Bla, bla?", "Logout", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
and I want to call it from this event:
private void btnLogOut_Click(object sender, EventArgs e)
{
FormMain_FormClosing(null, 'not sure what goes here');
}
Try this:
private void btnLogOut_Click(object sender, EventArgs e)
{
FormMain_FormClosing(null, null);
}
or
private void button1_Click(object sender, EventArgs e)
{
Form1_FormClosing(
null,
new FormClosingEventArgs(CloseReason.UserClosing, false));
}
Even if my answer cover how to link event handlers part, this particular solution leads you to a problem: form won't close clicking button.
Correct solution is
private void button1_Click(object sender, EventArgs e)
{
Close();
}
Handling the event and asking for confirmation are seperate things:
private static bool UserConfirmedToLogout()
{
return MessageBox.Show("Bla, bla?", "Logout", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK;
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = !UserConfirmedToLogout();
}
private void btnLogOut_Click(object sender, EventArgs e)
{
Close();
}
When Close() is called, the FormClosing event is fired too.