How could I close form? [closed] - c#

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 7 years ago.
Improve this question
When I click button from Form 1, the data of form 2 will display. So, I click button from form 1 again. The next data will add in new form 2, but form before still display. Therefore I have 2 form display. How could I close form before and display next form?

In order to make this, you need to store the reference to your form and manipulate with it.
What you want - close Form2 every time and open again:
public class Form1
{
private Form2 _form2;
public Button1_Click(object sender, EventArgs e)
{
if (_form2 != null)
{
_form2.Close();
}
_form2 = new Form2();
_form2.Label1.Text = DateTime.Now.ToString(); // or any other actions with form
_form2.Show();
}
}
It will close the previously used form and create a new one every time.
What you can also do - reuse this form and add some kind of "close form" button:
public class Form1
{
private Form2 _form2;
public Button1_Click(object sender, EventArgs e)
{
if (_form2 == null)
{
_form2 = new Form2();
_form2.Show();
}
_form2.Label1.Text = DateTime.Now.ToString(); // or any other actions with form
}
public Button2_Click(object sender, EventArgs e) // close button
{
if (_form2 != null) {
_form2.Close();
_form2 = null;
}
}
}
It will simply create a form at first time, and then just change its text when you click your button.

Related

How to check if form is called from another form C# [closed]

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
}
}
}

How i can open a form and close it by one button [closed]

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?.

C# WPF Window creation [closed]

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
I'm creating a WPF project based on C#.
While working on my project, I have ran into some troubles in how to create a single instance window (Like if i want to open the window X, it cannot be opened again unless it is closed). I found many articles online but they seemed lika lot of work, that it seemed easier to just disable the button when the window is open and enable it when the window is closed.
On the other hand I thought of an alternative solution which worked, but I want to see if it will be heavy on my system when a lot of windows are created.
The solution is shown below.
public partial class MainWindow : Window
{
Window1 wndw = new Window1();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!wndw.IsActive)
wndw.Show();
}
}
}
As you can see, i created the window at the start, and when the button to open it is clicked, it checks if it is active or not to show.
If I follow the latter method, will all the windows be loaded on MainWindow creation, or will the components only load when the event window1.show() is launched?
Thank you for your support
This is how I would have done it:
MainWindow code:
Window1 window1 = null; // global reference var
private void btnOpenWin1_Click(object sender, RoutedEventArgs e)
{
if (window1 == null)
{
window1 = new Window1();
window1.Closed += Window1_Closed; // register event to detect when Window1 closed
window1.Show();
}
else
{
window1.Focus();
}
}
// register event to remove Window1 ref to be able to open it again
private void Window1_Closed(object sender, EventArgs e)
{
window1 = null;
}

calling a method from another window form closing event. c# [closed]

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

Why doesn't my second form show up? [closed]

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 3 years ago.
Improve this question
I made a timer, but when I press a button nothing happens.
My code is:
private void button3_Click(object sender, EventArgs e)
{
Instellingen form = new Instellingen();
form.Show();
}
Instellingen.cs code:
public partial class Instellingen : Form
{
public Instellingen()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
MessageBox.Show("text", "text");
}
}
}
The button3 event doesn't even fire (confirmed by adding a breakpoint, it doesn't get there)
Check if the button's event realy connected to the method button3_Click.
Put a breakpoint in the first line of button3_Click and check if the code get there.
Make sure your button's click event is button3_Click. Probably your button's click event is empty.

Categories

Resources