Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How can i call method of a window form from another form closing event ?
Suppose second for is getting closed and i want to call method of first form when second get closed to update some changes on first window form.
You can add an event handler to the form_closing event in the first form and handle it accordingly.
Somewhere in form1
form2.Form_Closing += yourhandler;
This assumes that form 2 has a a control called TextBox1, when form 2 closes the lambda expression will be called and transfer data to form 1.
public partial class Form1 : Form
{
private Form2 openedForm2 = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Not sure if you would want more than 1 form2 open at a time.
if (this.openedForm2 == null)
{
this.openedForm2 = new Form2();
//Here is your Event handler which accepts a Lambda Expression, code inside is performed when the form2 is closed.
this.openedForm2.FormClosing += (o, form) =>
{
// this is Executed when form2 closes.
// Gets text from Textbox1 on form2 and assigns its value to textbox1 on form 1
this.textBox1.Text = ((Form2)o).Controls["TextBox1"].Text;
// Set it null so you can open a new form2 if wanted.
this.openedForm2 = null;
};
this.openedForm2.Show();
}
else
{
// Tells user form2 is already open and focus's it for them.
MessageBox.Show("Form 2 is already open");
this.openedForm2.Focus();
}
}
}
Pass a reference from your first form to your second. Say you create your second form this way (From your first form):
Form2 frm2 = new Form2();
frm2.referenceToFirstForm = this
In your second form you should have this:
public Form1 referenceToFirstForm
Then in your OnClosing event you can reference referenceToFirstForm
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have Form1 and in that form, i have this code
Form2 frm = new Form2();
frm.ShowDialog();
So now is my question: how to know if form2 is called by the way like that
in Button event in form2?
In button event in form2, I want to check if ShowDialog() is called FROM FORM1 (NOT FROM ANOTHER FORM), if button is clicked, form2 is closed!
You can use Form.Owner Property.
Form1:
Form2 frm = new Form2();
frm.ShowDialog(this); // owner parameter
Form2:
if (this.Owner != null)
{
// Owner is not null, there is a calling form
// Do something
if (this.Owner is Form1)
{
Form1 form1 = (Form1)this.Owner; // Form1 called this form!
}
}
Please try to make more precise what you are asking.
public partial class Form2: Form
{
public static bool wasCalledFromForm1 = false;
public Form2 (bool form1Called = false)
{
InitializeComponent();
wasCalledFromForm1 = form1Called;
}
private void Button1_Click(object sender, System.EventArgs e)
{
if (wasCalledFromForm1) this.Close;
}
}
Button 1 to open Form2:
Form2 form2 = new Form2(true);
form2.ShowDialog();
From what I understand you want to know if there is a way to check if the showDialog has been called successfully or not, for that you can use the following code snippet:-
Form2 frm = new Form2();
var result = frm.ShowDialog();
if (result == DialogResult.OK)
{
// apply your logic
}
PS:- In future please compose your questions properly and carefully so that its easier for us to answer :) :) :)
A example with constructor.
form2 frm2 = new Form2(calledByFrm1: true);
frm2.ShowDialog();
// ...
class Form2 ...
{
boolean calledByForm1;
public Form2(boolean calledByForm1)
{
this.calledByForm1 = calledByForm1;
}
public Form2_Onload ....
{
if (this.calledByForm1)
{
// your logic here
}
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I open a form but I can't close it with the same button. How I can do this?
I try doing this:
...
{
var openform = new Form2();
if (openform != null) openform.Show();
else openform.Hide();
}
It seems you have to implement the following logic:
If there's an opened Form2 instance, close it
Otherwise create and show the new Form2 instance.
If it's your case we should look for the opened Form2 instance(s) first; and only then create it with new (if required)
using System.Linq;
...
// Search: do we have opened Form2 instances?
Form2 openform = Application
.OpenForms // among all opened forms
.OfType<Form2>() // of type Form2
.LastOrDefault(); // in case we have several instances, let's choose the last one
if (null == openform) { // no Form2 instance has been found
openform = new Form2();
openform.Show();
}
else { // Instance (openform) has been found
openform.Close(); // Or openform.Hide();
}
private Form2 form2;
private void button1_Click(object sender, EventArgs e)
{
if (form2 == null || form2.IsDisposed)
{
// Either no form has been created or the last one created has been closed.
form2 = new Form2();
form2.Show();
}
else
{
form2.Close();
}
}
If you like null propagation:
if (form2?.IsDisposed == false)
{
form2.Close();
}
else
{
// Either no form has been created or the last one created has been closed.
form2 = new Form2();
form2.Show();
}
I think it will be more intuitive to the operator if the other form would be closed like all other forms in windows: press a Close button, click the upper right cross, select ALT-F4, etc.
However, if you really want to close the other form from your main-form, you should not just close it, but you should ask the other form nicely if it would be kind enough to close itself. This way you give the other form the possibility to ask the operator some questions, for instance ask if the changed items need to be saved.
private Form myForm = null;
public void OnButton1_Clicked(object sender, ...)
{
if (this.myform == null)
{ // not shown yet. Show it now:
this.myForm = new Form2()
this.myForm. properties = ...
// make sure I get notified if the Form closes in any way:
this.myForm.Closed += onMyFormClosed;
// show the form
this.myform.Show(this);
}
else
{ // ask the form nicely to close itself
this.CloseForm();
// this might (or might not) lead to the event Form.Closed
}
}
private void OnMyFormClosed(object sender, ...)
{
if (!object.ReferenceEquals(sender, this.myForm))
{ // someone else is closed. I have nothing to do with this
return;
}
// if here, my Form is closed. Save to Dispose and assign Null
this.myForm.Dispose();
this.myForm = null;
}
}
public void ShowFo
Use bool variable as a switch on button just like following and make openform global.
var openform = new Form2(); // it must be defined out
of the method and within the class as global variable.
private static bool isOpen = true;
Within Button Click method
if (isOpen) openform.Show();
else openform.Hide();
isOpen = !isOpen;
Tested Code
public partial class Form2 : Form
{
public Form2() => InitializeComponent();
Form1 openForm = new Form1();
private static bool isOpen = true;
private void button1_Click(object sender, EventArgs e)
{
if (isOpen) openForm.Show();
else openForm.Hide();
isOpen = !isOpen;
}
}
Check this one tested.
https://youtu.be/o9I77dhEvYg
Maybe something like this will help you:
Form2 openform = new Form2();
if (Application.OpenForms.OfType<Form2>().Count() > 0)
openform.Hide();
openform.Show();
Although I didn't fully understand the context of your question - you might need to adapt the code. You can also check this How to check if a windows form is already open, and close it if it is?.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
so i have this frmUser but to close this i have to call frmPass (which i called using ShowDialog()instead of Show()) that will confirm first if the user is an admin but the problem is when i execute the codes below
frmUser us = new frmUser(lblEID.Text, lblAdmin.Text, lblType.Text);
us.Hide();
this.Hide();
frmPass only hides itself and not along with frmUser. Also here's my code calling frmPass from frmUser
frmPass pass = new frmPass(lblAID.Text, lblName.Text, lblType.Text, "User Module");
pass.ShowDialog();
In your password form, you should set DialogResult property of that form. In example; on your frmPass, when user clicks on button Ok, your code checks if username and password are valid. If everything is ok set dialog result as OK, like this:
private void btnOk_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Ok;
}
that will close form.
In frmUser you can, if needed, check what is DialogResult of frmPass form, like this:
frmPass pass = new frmPass(lblAID.Text, lblName.Text, lblType.Text, "User Module");
DialogResult dr = pass.ShowDialog();
if (dr == DialogResult.Ok)
{
//do code if form is closed with OK dialog result
}
else
{
// some warning to user...
}
The problem is that you are trying to hide the wrong instace of frmUser. I assume that the first code passage is inside the class frmPass. There you create a new instance which you try to hide. But you want to hide the original instance.
As Nino already suggested I also would suggest to use the DialogResult to check whether the admin is confirmed. But may be you have already a different mechanism. Nevertheless you need to place the hiding code line into the class frmUser after the call of pass.ShowDialog();
private void button1_Click(object sender, EventArgs e)
{
formPass pass = new formPass();
pass.ShowDialog();
if(AdminIsConfirmed)
{
this.Hide();
}
}
leave the other this.Hide() in the frmPass to close that.
EDIT
Another possibility would be to pass the frmUser instance into the constructor of frmPass and have a variable of type frmUser in frmPass. You just have to overload it :
public partial class formPass : Form
{
formUser us;
public formPass(/* all that stuff that you have plus*/, formUser us)
{
InitializeComponent();
this.us = us;
}
call frmPass like this now:
frmPass pass = new frmPass(lblAID.Text, lblName.Text, lblType.Text, "User Module", this);
now you can just remove the line:
frmUser us = new frmUser(lblEID.Text, lblAdmin.Text, lblType.Text);
and the whole thing will work and hide properly.
This question already has answers here:
Values between windows forms in C# .NET
(2 answers)
how to access winform components from another class?
(2 answers)
Closed 8 years ago.
I have a project that contains two forms, Form1 and Form2. Form1 creates a new instance of Form2. In Form2, I am entering a number for a timer, then in Form1, I want to pull that number, set and start that timer.
However, I am unable to pull that number from Form2 into Form1 during run time. Does anyone know if it is possible to do this. Thanks for the help!
Take a look at
DialogResult.
The idea is to open form like this :
Form myForm = new Form();
if(myForm.ShowDialog() == DialogResult.OK)
{
// Access the value;
Console.out.WriteLine(myForm.TheValue);
}
In myForm you'll have something like that
private string sTheValue = null;
public string TheValue
{
get { return this.sTheValue; }
}
private void Button_Click(object sender, EventArgs e)
{
this.sTheValue = "Hello World !";
this.DialogResult = DialogResult.OK;
this.Close();
}
var form2 = new Form2(this);
// OR
// var form2 = new Form2();
// form2.Form1 = this; // A property created by you, in form1
form2.ShowDialog();
Somewhere else in form2
this.Form1.No = int.Parse(txt.Text); // No is a property in Form1 which is listening for changes...
I'm using two forms and I disable the first one when the second form shows up. I couldn't find a way to enable the first form when the second one is closed.
Passing a parameter could be a solution but I bet there is a simpler way.
First I thought of enabling the first form on the destructor of the second but could not do it.
Anyone have any suggestions?
You can show second form with ShowDialog() - form will be shown as modal, first form will be enabled only when second will be closed.
For future problems you can have a field in second form to have instance of first one, and use that instance, if you need, for example you can use custom constructor:
class SecondForm: Form
{
FirstForm _parentForm;
public SeconForm(FirstForm form)
{
InitializeComponent();
_parentForm = form;
}
void DoSomethingWithParent()
{
_parentForm.DoSomesting();
}
}
As has been mentioned, in this specific case it probably makes sense to use a modal dialog for opening the second form.
To cover the case when that isn't applicable, the accepted best practice would be to subscribe to the FormClosing event of the second form from the first, and in the event handler you could enable "yourself" and do anything else that you might want to do as a result of the other form being closed. Here is a simple example:
public partial class ParentForm : Form
{
private void button1_Click(object sender, EventArgs e)
{
ChildForm child = new ChildForm();
child.FormClosing += new FormClosingEventHandler(child_FormClosing);
Hide();
child.Show();
}
private void child_FormClosing(object sender, FormClosingEventArgs e)
{
Show();
}
}