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();
}
}
Related
i have Form1 and Form2, in Form1 i have some textboxes like username,password and more... and a textbox "region". when user hits "region"(Form1.hide()), then Form2 opens witch has 5 labels with names of regions on it.
so how can i make that when user clicks on a name of region in Form2, Form1 will have the region on it? and keep all the data that the user entered before the region click.
something like this(in form 2):
private void center_Click(object sender, EventArgs e)
{
this.Hide();
Form1.region = "center";
Form1.show();
}
Try creating an instance of Form2 and calling ShowDialog() method to show it
Form2 form2= new Form2();
form2.ShowDialog();
When creating Form2, just pass Form1 as a parameter and edit the textbox value in your click event.
On form1:
private void click_on_region(object sender, EventArgs e)
{
this.Hide();
Form2 frm2 = new Form2(this);
Form2.Show();
}
on form2:
Form1 _frm1;
public Form_Main(Form1 frm)
{
InitializeComponent();
_frm1 = frm;
}
private void center_Click(object sender, EventArgs e)
{
this.Hide();
_frm1.textBox_region.Text = whateverobject.Text;
_frm1.Show();
}
This might not be the prettiest but It'll do for starters.
Form 1 Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.ShowDialog();
textBox_Region.Text = objForm2.RegionName;
}
}
And Form 2 Code
public partial class Form2 : Form
{
public string RegionName
{
get
{
return textBox_Form2_Region.Text.ToString();
}
set { }
}
public Form2()
{
InitializeComponent();
}
}
On Form 2
private void center_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.textbox_region = whateverobject.text;
this.hide();
frm1.show();
}
this will bring up a form1 with the region text on it.
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();
}
}
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();
}
}
First of all, I am a newcomer to C# and programming in general. I've searched pretty thoroughly, but I can only find instances where someone wants to open another form and hide the one that the button was pressed on.
In this instance, I'm having issues with my program continuously running when I press the (X) on any form other than the main "Form1".The form-to-form navigation works fine. i.e.: clicking a button hides the main window and opens the appropriate form, and the "back" button on that form hides itself and shows (I guess another instance) of the previous "main" form. --I could probably use some guidance in that, too. lol
I wouldn't mind if it closed the entire application if the X was pressed, but I need to have the "X" present on all windows and all windows need to exit the entire app if the X is pressed. Any suggestions?
Thanks in advance,
Code:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void btnTransaction_Click(object sender, EventArgs e)
{
Transaction transactionForm = new Transaction();
Form mainForm = this;
transactionForm.Show();
mainForm.Hide();
}
}
Transaction Form:
public partial class Transaction : Form
{
public Transaction()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
Form1 mainForm = new Form1(); //not sure if I'm doing this right..
this.Hide(); //I don't know how to "reuse" the original Form1
mainForm.Show();
}
}
I would recommend you create an MDI Container for this. Drag and drop a MenuStrip from the ToolBox to Form1 and then create a ToolStripMenuItem "form2" in MenuStrip.
Now you can call your form2 in form1 like this
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
IsMdiContainer = true;
}
Form2 frm2;
public void CreateMdiChild<T>(ref T t) where T : Form, new()
{
if (t == null || t.IsDisposed)
{
t = new T();
t.MdiParent = this;
t.Show();
}
else
{
if (t.WindowState == FormWindowState.Minimized)
{
t.WindowState = FormWindowState.Normal;
}
else
{
t.Activate();
}
}
}
private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
{
CreateMdiChild<Form2>(ref frm2);
}
}
When by clicking ToolStripMenuItem you fire ToolStripmenuItem event , it will show Form2 as child in form1 i.e the mdi container and will be closed when you close form1.
public partial class Transaction : Form
{
Form1 _mainForm;
public Transaction(Form1 mainForm)
{
InitializeComponent();
_mainForm = mainForm;
}
private void button4_Click(object sender, EventArgs e)
{
this.Close(); //since you always create a new one in Form1
_mainForm.Show();
}
}
You can use the use Form.ShowDialog()
When this method is called, the code following (code below the 'ShowDialog()') it is not executed until after the dialog box is closed.
private void button4_Click(object sender, EventArgs e)
{
Form1 mainForm = new Form1();
this.Hide();
mainForm.ShowDialog();
this.Show();
}
You could use the ShowDialog() method that will require the user to interact with the new form before returning to the previous form. For instance you could try this:
public void btnTransaction_Click(object sender, EventArgs e)
{
using (var transactionForm = new Transaction())
{
this.Hide();
if (transactionForm.ShowDialog() == DialogResult.OK)
{
this.Show();
}
}
}
The DialogResult is something you can set on the TransactionForm like so:
private void button4_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
That's a pretty standard way of forcing interaction on a new form.
protected override void OnClosing(CancelEventArgs e)
{
this.Hide();
menu menu = new menu("administrator");
menu.ShowDialog();
this.Close();
}
//happy coding
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
}