Windows Develop Bookmark and Share   
 index > Windows Forms General > problem with winform closing
 

problem with winform closing

I do have two forms

in form1, when button is clicked, it opens form2

where the user has to fill in certain textboxes

after the 'done' button is clicked in form2

the values from textbox should be read( I do have properties to 'set' the values in second form)

and if any of the textboxes are empty, a message box should popup and remind them to fill the text boxes

but the Form 2 should stay intact after the user closes the messagebox

incase every textbox is filled, the form2 should close and return a concatenated string to form1

this is my functionality.

my problem here is after messageboxes are closed by the user, I am unable to find a statement to keep form2 open,

it right away executes the close() method for the form2.

can someone help me in this regard.

yprudhvi  Thursday, April 10, 2008 1:22 PM

Subscribe, in the form, to the OnClosing event of Form2. Create a method that handles this event, and within this method, if the fields are not filled in properly, set e.Cancel to true.

Code Snippet

private void Form3_FormClosing(object sender, FormClosingEventArgs e)

{

if (textBox1.Text == null || textBox1.Text == string.Empty)

{

MessageBox.Show("You haven't filled everything in!");

e.Cancel = true;

}

}

David M Morton  Thursday, April 10, 2008 1:42 PM

Hi,

on form2 use the FormClosing event.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

if (MessageBox.Show("Something is wrong") != DialogResult.OK)

e.Cancel = true;

}

The FormClosingEventArgs have two properties Cancel (to prevent closing) and CloseReason (why closing happens).

See

http://msdn2.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.aspx

regards

Philipp

Philipp Merz  Thursday, April 10, 2008 1:43 PM

Subscribe, in the form, to the OnClosing event of Form2. Create a method that handles this event, and within this method, if the fields are not filled in properly, set e.Cancel to true.

Code Snippet

private void Form3_FormClosing(object sender, FormClosingEventArgs e)

{

if (textBox1.Text == null || textBox1.Text == string.Empty)

{

MessageBox.Show("You haven't filled everything in!");

e.Cancel = true;

}

}

David M Morton  Thursday, April 10, 2008 1:42 PM

Hi,

on form2 use the FormClosing event.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

if (MessageBox.Show("Something is wrong") != DialogResult.OK)

e.Cancel = true;

}

The FormClosingEventArgs have two properties Cancel (to prevent closing) and CloseReason (why closing happens).

See

http://msdn2.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.aspx

regards

Philipp

Philipp Merz  Thursday, April 10, 2008 1:43 PM

You can use google to search for other answers

Custom Search

More Threads

• How to skin Windows Forms?
• Simple Form question
• Delete columns of a Grid Master-Detail
• dataGridView as a calendar
• Loading colored and animated cursors from the resources
• How to show the caps lock is on balloon warning like Windows
• ADO.Net question
• problems with mdi form
• NullReferenceException when updating parent property
• How to keep winform run normally when changing resolution in Win2K & XP