Close Parent with Modal Form - c#

private void btnOpenModalWindow_Click(object sender, EventArgs e)
{
using (var modalForm = new modalForm(EventDetails))
{
modalForm.ShowDialog(this);
}
}
Inside the modal window:
private void btnOk_Click(object sender, EventArgs e)
{
Close();
}
I want to close the parent form also when I close the modal window but I'm not sure how to do that from the modal window.

Just call the Owner that you already passed as parameter to ShowDialog
private void btnOk_Click(object sender, EventArgs e)
{
this.Owner.Close();
}

Actually, a cleaner way is to do:
private void btnOpenModalWindow_Click(object sender, EventArgs e)
{
using (var modalForm = new modalForm(EventDetails))
{
if (modalForm.ShowDialog() == DialogResult.OK)
{
this.Close();
}
}
}
Allows you to check the return code if you don't want to exit. Or just close after ShowDialog.

Found exactly what I was looking for, and its so ridiculously simple I'm ashamed to have even asked.
Application.Exit();

Related

How to create a close button while the original close button is to minimize?

i want to have a custom close window.
My scenario is:
The original X button function's is to minimize the window, while the custom will actually close it. My default X is like below:
// add handler by double clicking on Closing event in Properties Box
// My below X button
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
However, when i try to create the custom close button like this:
void closeButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
It will just minimize like the default. So how do i it? Thanks for reading.
private bool _forceClose;
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(!_forceClose)
{
e.Cancel = true;
this.Hide();
}
}
void closeButton_Click(object sender, RoutedEventArgs e)
{
_forceClose = true;
Close();
}
You can remove closing event in the button click
public MainWindow()
{
InitializeComponent();
this.Closing += MainWindow_Closing;
}
void MainWindow_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Closing -= MainWindow_Closing;
this.Close();
}

How can I find what Form opened the form currently open? (C#, WinForms)

So I basically have a form that is opened and I need to check what form called that one to open
"Parent form"
private void btnEnter_Click(object sender, EventArgs e)
{
this.Close();
newForm nf = new newForm();
nf.Show()
}
"Opened form"
private void newForm_Load(object sender, EventArgs e)
{
if parent is ("oldForm") // Need to know how to code for this line.
{
//do some stuff here
}
else
{
//Do something different
}
}
So for example if oldForm was the form that called this one, something specific would happen that wouldn't if "anotherForm" called it for example
You could just add a 'Parent' property to your forms, and set it before calling Show.
private void btnEnter_Click(object sender, EventArgs e)
{
this.Close();
newForm nf = new newForm();
nf.Parent = this;
nf.Show()
}
Your form would look like this:
public class MyForm
{
public Form Parent {get;set;}
private void newForm_Load(object sender, EventArgs e)
{
if (this.Parent is oldForm)
{
//do some stuff here
}
else
{
//Do something different
}
}
}
Note that if (this.Parent is oldForm) is equivalent to if (this.Parent.GetType() == typeof(oldForm))
As one of the comments said, if you're only using the Parent property to make this one decision, you'd be better to define it as a Boolean property, called DoSomething that indicates what it does. Combining this with the other suggestion gives:
public class MyForm
{
private bool specialMode;
public MyForm(bool mode)
{
this.specialMode = mode;
}
private void newForm_Load(object sender, EventArgs e)
{
if (this.specialMode)
{
//do some stuff here
}
else
{
//Do something different
}
}
}
which you'd call like this:
private void btnEnter_Click(object sender, EventArgs e)
{
this.Close();
newForm nf = new newForm(true); // SpecialMode = ON
nf.Show()
}

New form fails to appear

Can someone explain why my new form doesn't appear? The old form closes but it isn't replaced by my new one:
namespace Pong
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
form.Show();
this.Close();
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Is "Menu" the startup form for your application? If yes, then when it closes the entire application closes.
One solution would be to hide the current form, display "form" with ShowDialog(), then call Close():
private void PlayButton_Click(object sender, EventArgs e)
{
this.Visible = false; // hide the current form
Application.DoEvents();
PongForm form = new PongForm();
form.ShowDialog(); // code stops here until PongForm is dismissed
this.Close(); // now close the form (and presumable the whole app)
}

getting System.ObjectDisposedException: Cannot access a disposed object.Modeless Dialogs in C#

I'd like to generate a modeless dialog box, whenever I close the box and want to open it again I am getting an error saying
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'TransactionHistoryDialog'.
at System.Windows.Forms.Control.CreateHandle()
here is my code for creating the modeless dialogbox
public partial class TransactionHistoryDialog : Form
{
private static TransactionHistoryDialog instance;
private TransactionHistoryDialog()
{
InitializeComponent();
}
public static TransactionHistoryDialog CreateForm()
{
if (instance == null)
{
instance = new TransactionHistoryDialog();
}
return instance;
}
private void TransactionHistoryDialog_FormClosing(object sender, FormClosingEventArgs e)
{
instance = null;
}
private void buttonClose_Click(object sender, EventArgs e)
{
instance = null;
}
private void buttonTransactionHistoryClose_Click(object sender, EventArgs e)
{
this.Dispose();
}
}
then in my main form whenever the transactionHistory button is clicked transaction dialog shows up : here is my code for event of clicking transaction button
private void buttonTransferHistory_Click(object sender, EventArgs e)
{
TransactionHistoryDialog transactionHistory = TransactionHistoryDialog.CreateForm();
transactionHistory.updateTextBox();
transactionHistory.Show();
}
I have search a lot, but could not find where the problem is. can any one please give me some hints ?
Because closing the Window disposes it. You need to create a new one after it has closed, you cannot show the same Window after it has closed. If you want to show the same one don't close it and in a handler set Visibility to Hidden instead then add another method, e.g. UnHide(), set it back to Visible when wanting to show the same instance again.
I prefer just creating a new one:
TransactionHistoryDialog openTransactionHistoryDialog;
private void buttonTransferHistory_Click(object sender, EventArgs e)
{
if(openTransactionHistoryDialog == null)
{
openTransactionHistoryDialog = new TransactionHistoryDialog();
openTransactionHistoryDialog.updateTextBox();
openTransactionHistoryDialog.Closed += OnTransactionHistoryDialogClosed;
}
openTransactionHistoryDialog.Show();
}
private void OnTransactionHistoryDialogClosed(object sender, EventArgs e)
{
openTransactionHistoryDialog = null;
}
UPDATE: There is an "official" example of a modeless dialog box at the bottom of this page: http://msdn.microsoft.com/en-us/library/aa969773(v=vs.110).aspx.

How to force the form focus?

How can I force the focus of an form? .Focus() is not working for me.
private void button1_Click(object sender, EventArgs e) {
var form = new loginForm();
if (Application.OpenForms[form.Name] == null) {
form.Show();
} else {
form.Focus();
}
}
What am I doing wrong?
You need to show the form first - use the Show() method:
var form = new loginForm();
form.Show();
Edit: (updated question)
For an existing form calling Activate() might be more appropriate, this also brings the form to the front:
private void button1_Click(object sender, EventArgs e)
{
var form = new loginForm();
if (Application.OpenForms[form.Name] == null)
{
form.Show();
}
else
{
Application.OpenForms[form.Name].Activate();
}
}
If the form is minimized you need to subscribe to the Activated event to change your window state to FormWindowState.Normal:
private void loginForm_Activated(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
Try this:
this.BringToFront();
this.Activate();
it should be
private void button1_Click(object sender, EventArgs e) {
var form = new loginForm();
if (Application.OpenForms[form.Name] == null) {
form.Show();
} else {
Application.OpenForms[form.Name].Focus();
}
}
None of the previous answers worked for me, but this does:
protected override void OnShown(EventArgs e)
{
Focus();
}
None of the focusing methods worked if called before this event. Hope it helps.
On start of the form we add
this.BringToFront();
this.Activate();

Categories