When I click a button a form is opened, but if the form is already open, then the app should display the message "Form already open!" and do nothing else.
My problem is, once I close the window [x] I can't open the form again.
Here's the code:
Form2 decript_form = new Form2();
private void button2_Click(object sender, EventArgs e)
{
if (!decript_form.Visible)
decript_form.Show();
else
MessageBox.Show("Form already open!");
}
When the "Close" button is pressed, you want it to just "hide" the form...you need to use e.Cancel to stop it going on and closing.
If you really really want to close the Form2 window instead of hiding it while your application is running....then call ReallyClose....so that the close isn't prevent (then create a new decript_form or null it out).
(Alternatively decript_form.Dispose() would force real closure too)
public partial class Form2 : Form
{
private bool m_bReallyClose = false;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (!m_bReallyClose)
{
this.Visible = false;
e.Cancel = true;
}
}
public void ReallyClose()
{
m_bReallyClose = true;
this.Close();
}
}
public partial class Form1 : Form
{
Form2 decript_form = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!decript_form.Visible)
decript_form.Show();
else
MessageBox.Show("Form already open!");
}
private void button2_Click(object sender, EventArgs e)
{
decript_form.Dispose(); // or .ReallyClose();
decript_form = new Form2();
}
}
I assume that you talk about the [x] on Form2 being pressed. It that's the case you should handle the Closing event in Form2() and add
this.Hide();
to the handler. Even a closed window is still 'shown' until it is hidden.
class Form2
{
override protected void OnClosing(CancelEventArgs e)
{
Hide();
}
}
Related
This is how I open a new form
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//Sets the Add New Button to Color WhiteSmoke
this.button1.BackColor = Color.WhiteSmoke;
//Hides current form (Form 1)
this.Hide();
//Displays Form 2
f2.ShowDialog();
}
This is how I close my forms. What am I doing wrong?
private void Form1_Close(object sender, FormClosingEventArgs e)
{
foreach (Form form in Application.OpenForms)
{
Environment.Exit(0);
}
}
Instead of calling Environment.Exit(0), try calling Close():
foreach (Form form in Application.OpenForms)
{
form.Close();
}
Another option:
Application.Exit();
Try putting your code in the Form1_FormClosing() Method:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (Form form in Application.OpenForms)
{
// Make sure form is visible before closing //
form.Show();
form.Close();
}
Environment.Exit(0);
}
Form1's close event is not called if the form is hidden. So don't hide the Form. In fact - never hide the form :)
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//Sets the Add New Button to Color WhiteSmoke
this.button1.BackColor = Color.WhiteSmoke;
//Hides current form (Form 1)
this.Hide(); //<-- delete this
//Displays Form 2
f2.ShowDialog();
f2.Dispose(); //<-- necessary when using ShowDialog()
}
And delete all of this:
private void Form1_Close(object sender, FormClosingEventArgs e)
{
foreach (Form form in Application.OpenForms)
{
Environment.Exit(0);
}
}
That should make the process close then you close Form1
Here, simple solution is that pass your Form1 object reference in Form2 contructor and call Form1.Close in Form2_Closing Event.
public partial class Form1 : Window
{
public Form1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Form2 f2 = new Form2(this);
this.Hide();
f2.Show();
}
}
Call Form1 close event when Form2 is closed.
public partial class Form2 : Window
{
Form1 frm;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 frm)
{
InitializeComponent();
this.frm = frm;
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
frm.Close();
}
}
A have a form that will show another form when you click logout. I want to close or hide both forms when you click yes and it will go to my login form. Any help would be appreciated.
You can use Form's Owner property to set child form's owner:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Owner = this;
form2.ShowDialog();
}
}
Then in your secondary form you close it:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.Owner != null)
this.Owner.Close();
}
}
This is a WinForm solution:
private void bLogout_Click(object sender, EventArgs e)
{
DialogResult result= MessageBox.Show("Are you sure want to logout?", "Confirm", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
this.Hide();
new frmLogin().Show();
}
}
Basically; Form1 has 2 buttons, Form2 has 1 button.
When you click Form2's button it checks which button on Form1 you clicked, opening Form3 or Form4 depending on which button you clicked (on Form1).
So I've utilized Mark Halls first method of passing variables between forms. Now for the second half of my closed refinement.
Form1
private void btnLogin_Click(object sender, EventArgs e)
{
// Call function while storing variable info.
Account("login");
}
private void btnRegister_Click(object sender, EventArgs e)
{
// Call function while storing variable info.
Account("register");
}
// Function used to pass Variable info to Account form while opening it as instance.
private void Account(string formtype)
{
// Generate/Name new instant of form.
frontend_account frmAcc = new frontend_account();
// Pass variable to instance.
frmAcc.CheckButtonClick = formtype;
// Show form instance.
frmAcc.Show(this);
// Hide this instance.
this.Hide();
}
Form2
// String Variable to store value from Login.
public string CheckButtonClick { get; set; }
private void btnContinue_Click(object sender, EventArgs e)
{
// If statement to open either Main form or Registration form, based on Login variable.
if (CheckButtonClick == "login")
{
// Generate/Name new instant of form.
frontend_main frmMain = new frontend_main();
// Show form instant.
frmMain.Show();
// Close this instant.
this.Close();
}
else if (CheckButtonClick == "register")
{
// Generate/Name new instant of form.
frontend_register frmReg = new frontend_register();
// Show form instant.
frmReg.Show();
// Close this instant.
this.Close();
}
}
On Form2 there are TWO radio buttons, can I adept that code to set the focus of a tab control when a form is opened? ie. if radClient is checked set focus on tabcontrol after opening winform, else if radStudent is checked set focus on tabcontrol (other page) after opening winform... and i guess don't open a winform if no radio is checked.
I believe this will set the focus;
// Sets focus to first tab.
tabRegister.SelectedTab = tabRegister.TabPages[0];
// Sets focus to second tab.
tabRegister.SelectedTab = tabRegister.TabPages[1];
In your example the first problem I see is you are closing your parent form which closes your Form1 and disposes of Your Form2, What I would do is Hide Form1 instead of Closing it, I would then create a public property on Form2 to pass in the Button that was selected. But anytime you are opening and closing multiple Forms it can get messy, what I would do would be to create UserControls for your additional Forms and swap them out in a Panel. The first example is how to do it the way that you asked.
Form
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
ShowForm2("login");
}
private void btnRegister_Click(object sender, EventArgs e)
{
ShowForm2("register");
}
private void ShowForm2(string formtype)
{
Form2 f2 = new Form2(); // Instantiate a Form2 object.
f2.CheckButtonClick = formtype;
f2.Show(this); // Show Form2 and
this.Hide(); // closes the Form1 instance.
}
}
Form2
ublic partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string CheckButtonClick { get; set; }
private void button1_Click(object sender, EventArgs e)
{
if (CheckButtonClick == "login")
{
Form3 f3 = new Form3(); // Instantiate a Form3 object.
f3.Show(); // Show Form3 and
this.Close(); // closes the Form2 instance.
}
else if (CheckButtonClick == "register")
{
Form4 f4 = new Form4(); // Instantiate a Form4 object.
f4.Show(); // Show Form4 and
this.Close(); // closes the Form2 instance.
}
}
}
Form3 and Form4 note since Form1 is long forgotten to these forms I search for it to Open back up
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_FormClosed(object sender, FormClosedEventArgs e)
{
FormCollection frms = Application.OpenForms;
foreach (Form f in frms)
{
if (f.Name == "Form1")
{
f.Show();
break;
}
}
}
}
The second Option with UserControls has one Form with a Panel on it. It uses events to signal the Form to Change Controls plus a public property on UserControl2
public partial class Form1 : Form
{
string logonType;
public Form1()
{
InitializeComponent();
}
private void userControl1_LoginOrRegisterEvent(object sender, LoginOrRegisterArgs e)
{
logonType = e.Value;
userControl2.BringToFront();
}
private void userControl2_ControlFinshedEvent(object sender, EventArgs e)
{
if (logonType == "logon")
userControl3.BringToFront();
else if (logonType == "register")
userControl4.BringToFront();
}
private void userControl3_ControlFinshedEvent(object sender, EventArgs e)
{
userControl1.BringToFront();
}
private void userControl4_ControlFinshedEvent(object sender, EventArgs e)
{
userControl1.BringToFront();
}
}
UserControl1
public partial class UserControl1 : UserControl
{
public delegate void LoginOrRegisterHandler(object sender, LoginOrRegisterArgs e);
public event LoginOrRegisterHandler LoginOrRegisterEvent;
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
LoginOrRegisterArgs ea = new LoginOrRegisterArgs("logon");
LoginOrRegisterEvent(sender, ea);
}
private void button2_Click(object sender, EventArgs e)
{
LoginOrRegisterArgs ea = new LoginOrRegisterArgs("register");
LoginOrRegisterEvent(sender, ea);
}
}
public class LoginOrRegisterArgs
{
public LoginOrRegisterArgs(string s) {Value = s;}
public string Value {get; private set;}
}
UserControl2
public partial class UserControl2 : UserControl
{
public delegate void ControlFinishedHandler(object sender, EventArgs e);
public event ControlFinishedHandler ControlFinshedEvent;
public UserControl2()
{
InitializeComponent();
}
public string SetLogonType { get; set; }
private void button1_Click(object sender, EventArgs e)
{
ControlFinshedEvent(sender, new EventArgs());
}
}
UserControl3 & UserControl4 exactly the same except for different Class Name
public partial class UserControl3 : UserControl
{
public delegate void ControlFinishedHandler(object sender, EventArgs e);
public event ControlFinishedHandler ControlFinshedEvent;
public UserControl3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ControlFinshedEvent(sender, new EventArgs());
}
}
As I suggested in my comment, one of the best way I know to pass data between forms is to use events.
Basically, in the "child" forms, you declare an event that will be handled, or listened to, by the "main" form.
See the referenced answer from my comment, and if you have specific questions on how to adapt it, ask away.
Cheers
I have a bunch of windows forms. Each form has "Back" and "Next" buttons for switching forms. For example, clicking "Back" on Form3 then we go to Form2. Then clicking "Next" button on Form2 then Form3 is shown.
Now my question is that if we click "Next" from the very beginning, it works smoothly. However if I click "Back" on Form3 then Form2 is displayed, then click "Next" on Form3 go to Form3. The code doesn't goto Form3_Load event.
What is wrong in my code?
public partial class Form3 : Form
{
Form2 FormPrev;
Form4 FormNext;
List<DataRow> drlist = new List<DataRow>();
DataTable dt = new DataTable();
public Form3(Form2 _FormPrev)
{
InitializeComponent();
this.FormPrev = _FormPrev;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnNext_Click(object sender, EventArgs e)
{
ShowNext();
}
private void btnBack_Click(object sender, EventArgs e)
{
ShowPrev();
}
private void ShowNext()
{
if (FormNext == null)
FormNext = new Form4(this);
FormNext.Show();
this.Hide();
}
private void ShowPrev()
{
FormPrev.Show();
this.Hide();
}
private void Form3_Load(object sender, EventArgs e)
{
// blah blah.
}
Thanks.
A form's Load event is only fired when the form is invoked for the first time. If you subsequently hide the form and reshow it then this is not reloading the form so the form's Load event is not fired.
If you want to use an event to handle when the form is re-displayed then you should look at the following more suitable events:
Activated
Shown
VisibleChanged
Form Load event only gets fired before the form is shown for the first time.
You should use a different event, like maybe Form Activated or GotFocus.
That's a normal behaviour. Load is for load form, not for show. In your case you try to show hided form. If you want to use
form.Show()
than use not a
form.Hide()
but
form.Close()
UPD:
Code should be:
public partial class Form3 : Form
{
List<DataRow> drlist = new List<DataRow>();
DataTable dt = new DataTable();
public Form3()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnNext_Click(object sender, EventArgs e)
{
ShowNext();
}
private void btnBack_Click(object sender, EventArgs e)
{
ShowPrev();
}
private void ShowNext()
{
Form4 formNext = new Form4();
formNext.Show();
this.Close();
}
private void ShowPrev()
{
Form2 formPrev = new Form2();
formPrev.Show();
this.Close();
}
private void Form3_Load(object sender, EventArgs e)
{
// blah blah.
}
}
But there is a problem with such colutions - you shouldn't close your main form.
I know how to go to another form in modal mode just like what I did below:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 myNewForm = new Form2();
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
myNewForm.ShowDialog();
}
}
This is my second form, how do I go back to the previous form?
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
// what should i put here to show form1 again
}
}
When you call ShowDialog on a form, it runs until the form is closed, the form's DialogResult property is set to something other than None, or a child button with a DialogResult property other than None is clicked. So you could do something like
public partial class Form1
{
...
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
newform.ShowDialog();
// We get here when newform's DialogResult gets set
this.Show();
}
}
public partial class Form2
{
...
private void button1_Click(object sender, EventArgs e)
{
// This hides the form, and causes ShowDialog() to return in your Form1
this.DialogResult = DialogResult.OK;
}
}
Although if you're not doing anything but returning from the form when you click the button, you could just set the DialogResult property on Form2.button1 in the form designer, and you wouldn't need an event handler in Form2 at all.
I use a static Form value Current in all my multiple form apps.
public static Form1 Current;
public Form1()
{
Current = this;
// ... rest of constructor
}
Then in Form2
public static Form2 Current;
public Form2()
{
Current = this;
// ... rest of constructor
}
Then you can from your button click do,
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
// what should i put here to show form1 again
Form1.Current.ShowDialog(); // <-- this
}