C# Windows Form disappears suddenly - c#

I am using C# + VSTS2008 + .Net 2.0 to develop a Windows Forms application. In button1 event handler of Form1, I create Form2, then Form2 appears. Then when trigger event handler of button2 of Form2 (button2 is Form2's UI button element), after executing button2 event handler, Form2 will disappear. Here is part of my code which creates Form2. Any ideas what is wrong to cause Form2 disappear?
// button1 belongs to Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2("www.google.com");
form2.ShowDialog();
}

Form2 is being opened as a dialog. Could the button being pressed also be defined as the CancelButton (The CancelButton property will be on form2). Doing that will automatically dismiss the dialog when the event handlers have completed.

Without more details it's impossible to say for sure, but if button2 in Form2 has a value assigned to the DialogResult property, this will cause the form to hide automatically when the button is clicked. Open Form2 in the designer, select the button and check in the property grid. If the DialogResult property is anything else than "None", this is expected behaviour.
From the MSDN documentation of the Button.DialogResult property:
If the DialogResult for this property
is set to anything other than None,
and if the parent form was displayed
through the ShowDialog method,
clicking the button closes the parent
form without your having to hook up
any events. The form's DialogResult
property is then set to the
DialogResult of the button when the
button is clicked.

Related

It exists a way to open a form and close a parent form from a User Control in Winforms C#? [duplicate]

I have a C# Form, where you have Form1 shown at the start, and when you press Go you are taken to Form2. Form1 is hidden and Form2 is shown.
Now when you exit form2, the whole application should be closed. I am using Application.Exit() when I press the exit button.
I am facing problems if the user presses X or ALT+F4 or RightClick->Close.
The form will close but the hidden form will stay opened.
How can I fix that? When I press one of these control button, for all hidden forms to also close?
I tried form1_Close and Form1_Closing function but they didn't seem to work.
Try this:
Hide();
Form2 form2 = new Form2();
form2.Closed += (s, args) => this.Close();
form2.Show();
This will close Form1 when you close Form2. If the user presses X or ALT+F4 or RightClick -> Close on Form2 The Form2 and the hidden Form1 will close.
Use the FormClosed event on Form2 to exit the application.
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}

Open a new winform by doubleclick and close the caller window

I have a C# winform called Form1, and this winform has a list and a button.
I added a click() event to the button, and a doubleclick() event to the list.
Both events call to the same method: (in form1.designer.cs)
this.myList.DoubleClick += new System.EventHandler(this.myMethod);
this.myButton.Click += new System.EventHandler(this.myMethod);
In myMethod, I want to do the following operations:
open a new winform of kind Form2, and make it the active winform.
close the caller winform (of kind Form1), there is no need for this form anymore.
I did it like this: (in form1.cs)
private void myMethod(object sender, EventArgs e)
{
Form2 frm = new Form2();
this.dispose();
}
when myMethod is being called by list doubleclick event, when myMethod ends, there is a null pointer exception.
When it's being called by the button click event, it works properly.
I tried this.close() as well, and got the same behavior.
my questions:
How should I write myMethod properly so it will make the wanted operations for the button click event and also for the list doubleclick event?
What is the difference between the button and the list? why does it work properly for the button, but crashes for the list?
Thanks
You can hide the Form1 and show Form2. This will raise some issue like closing Form2
won't close Form1.
this.Hide();
var form2 = new Form2();
form2.ShowDialog();

In C# Windows Forms, does setting a form's CancelButton property to a button automatically close the form on the button click event?

Or by setting the CancelButton property of the form, we just ensure that the button gets enabled on pressing Esc key on the form, but the click event needs to be handled by writing a separate event handler?
In C# Windows Forms, does setting a form's CancelButton property to a button automatically close the form on the button click event?
Yes, because doing so sets the DialogResult property of that button to DialogResult.Cancel. As the documentation for that property states:
If the DialogResult for this property is set to anything other than None, and if the parent form was displayed through the ShowDialog method, clicking the button closes the parent form without your having to hook up any events. The form's DialogResult property is then set to the DialogResult of the button when the button is clicked.
Do note that important caveat: "if the parent form was displayed through the ShowDialog method". The only way your form will automatically close is if you displayed it using frm.ShowDialog(). If you used the Show method, it won't automatically close. You need to write code to manage that yourself.
Only if the form is a dialog. Test it yourself

Control.Focus() bug

I have some problems with Form control focusing.
On form1 I click a button and run the code below:
private void btnTest_Click(object sender, System.EventArgs e)
{
form2 = new Form2();
Application.Idle += new EventHandler(Application_Idle);
form2.Show();
form2.Activate();
form2.textBox1.Focus();
Form3 form3 = new Form3();
form3.ShowDialog();
}
Then, after this CLR I run the event Application_Idle on which I add a method that must focus on the textBox2 control..
private void Application_Idle(object sender, EventArgs e)
{
form2.textBox2.Focus();
form2.textBox2.Select();
form2.textBox2.Focus();
Application.Idle -= new EventHandler(Application_Idle);
}
But when I click the button on form1, I see Form2 showing, Form3 showing and then Application_Idle method raise, but form2.textBox2 control doesn't get focused...
If I comment out the form3.ShowDialog(); line it's works fine, but how do I focus a form element with another form activation?(form3.ShowDialog()) ?
Remark added:
Problem in also is I have a strict architecture and all I can change is Application_Idle method.
The issue you are having is with modality:
Forms and dialog boxes are either modal or modeless. A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application.
Dialog boxes that display important messages should always be modal. The About dialog box in Visual Studio is an example of a modal dialog box. MessageBox is a modal form you can use.
Modeless forms let you shift the focus between the form and another form without having to close the initial form. The user can continue to work elsewhere in any application while the form is displayed.
When you use ShowDialog, the form that is shown prevents the caller from returning control until the dialog box is closed. If this is not the desired effect, you can use the Show method.
You could focus the textfeld, when the form itself got the focus:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.GotFocus += (s, e) =>
{
this.textBox2.Focus();
};
}
}
As John Koerner stated, you cannot set focus to Form 2 while Form 3 is open because of modality.
Since you stated that a user input in Form 3 is necessary to proceed, you should change your approach. You can place a listener watch for Form 3's closing. Only then can you set the focus somewhere else
form3.FormClosed += Application_Idle

C#: How to cancel the close action for a button that's set as the Accept or Cancel button on a form?

I have a button btnOK on my form, with a DialogResult property of OK. The form's AcceptButton property is set to btnOK. So if I click the button, the form closes automatically.
Now, inside the btnOK_Click() method, I want to ability to cancel out of the close action, e.g. if there was an error I want to show a message box and not close the form.
How do I do it?
On error add:
this.DialogResult = DialogResult.None
IMO you just don't have to set DialogResult property on the button, but set it directly on your form in the btnOK_Click event:
private void btnOK_Click(object sender, EventArgs e)
{
if (yeahLetsClose)
this.DialogResult = DialogResult.OK; // form will close with OK result
// else --> form won't close...
}
BTW, AcceptButton property is related to ENTER key (when you press it on your form, the AcceptButton will be pressed)
Add an event handler for the form close event. The EventArgs parameter should have a Cancel property.

Categories