Click here to Skip to main content
15,891,136 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# WinForms: Is putting a Form inside a Form a "Bad Thing" ? Pin
BillWoodruff24-Oct-13 17:49
professionalBillWoodruff24-Oct-13 17:49 
AnswerRe: C# WinForms: Is putting a Form inside a Form a "Bad Thing" ? Pin
Ravi Bhavnani23-Oct-13 12:12
professionalRavi Bhavnani23-Oct-13 12:12 
GeneralRe: C# WinForms: Is putting a Form inside a Form a "Bad Thing" ? Pin
BillWoodruff23-Oct-13 18:52
professionalBillWoodruff23-Oct-13 18:52 
GeneralRe: C# WinForms: Is putting a Form inside a Form a "Bad Thing" ? Pin
joost.versteegen24-Oct-13 3:23
joost.versteegen24-Oct-13 3:23 
GeneralRe: C# WinForms: Is putting a Form inside a Form a "Bad Thing" ? Pin
BillWoodruff24-Oct-13 17:48
professionalBillWoodruff24-Oct-13 17:48 
AnswerRe: C# WinForms: Is putting a Form inside a Form a "Bad Thing" ? Pin
WuRunZhe30-Oct-13 14:50
WuRunZhe30-Oct-13 14:50 
Questionclosing second form without closing first form Pin
alfie.max1522-Oct-13 23:34
alfie.max1522-Oct-13 23:34 
AnswerRe: closing second form without closing first form Pin
BillWoodruff23-Oct-13 2:20
professionalBillWoodruff23-Oct-13 2:20 
The simplest possible way to do this is:
C#
private void btnVisibleControl_Click(object sender, EventArgs e)
{
    secondForm.Visible = ! secondForm.Visible;
}
But, what if the second Form shows a 'CloseBox, and the user closes it ? To guard against that you can define a FormClosing EventHandler in the second Form:
C#
private void secondForm_FormClosing(object sender, FormClosingEventArgs e)
{
    this.Hide();
    e.Cancel = true;
}
Note that 'secondForm will be closed when your first Form is closed automatically only if the 'secondForm was created and shown in your first Form's code. Automatic closing of Forms created in the "Main" Form's code, when the Main Form closes, is a standard behavior of the Windows Form programming model.

But, there's a better way, where all the code is put in the main Form:
C#
private secondForm f2;

private string strClose = "Close Second Form";

private string strOpen = "Open Second Form";

private void FormTemplate_Load(object sender, EventArgs e)
{
    f2 = new secondForm();

    // by assigning 'secondForm's Closing EventHandler here:
    // we can synchronize the Button Text easily since the
    // the EventHandler will have access to the Button
    f2.FormClosing += f2_FormClosing;

    btnVisibleControl.Text = strOpen;
}

// prevent the instance of 'secondForm from actually closing
private void f2_FormClosing(object sender, FormClosingEventArgs e)
{
    f2.Hide();

    // update the Button Text
    btnVisibleControl.Text = strOpen;

    e.Cancel = true;
}

// change the Button Text so the end-user sees
// what pressing the Button will do
private void button1_Click(object sender, EventArgs e)
{
    if (f2.Visible)
    {
        btnVisibleControl.Text = strOpen;
    }
    else
    {
        btnVisibleControl.Text = strClose;
    }

    // or you can use this to make the code (above) simpler:
    // btnVisibleControl.Text = (f2.Visible) ? strOpen : strClose;

    f2.Visible = ! f2.Visible;
}


Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview


modified 23-Oct-13 10:45am.

GeneralRe: closing second form without closing first form Pin
alfie.max1524-Oct-13 7:21
alfie.max1524-Oct-13 7:21 
QuestionSQL Server Connection Pin
Member 1035350322-Oct-13 17:43
Member 1035350322-Oct-13 17:43 
AnswerRe: SQL Server Connection Pin
Ron Beyer22-Oct-13 18:06
professionalRon Beyer22-Oct-13 18:06 
AnswerRe: SQL Server Connection Pin
Nicholas Marty22-Oct-13 23:27
professionalNicholas Marty22-Oct-13 23:27 
QuestionC# Open Windows 8 Keyboard Pin
Kevin Marois22-Oct-13 13:15
professionalKevin Marois22-Oct-13 13:15 
AnswerRe: C# Open Windows 8 Keyboard Pin
BillWoodruff22-Oct-13 21:37
professionalBillWoodruff22-Oct-13 21:37 
QuestionParsing an XML File Pin
MarkB12322-Oct-13 9:18
MarkB12322-Oct-13 9:18 
AnswerRe: Parsing an XML File Pin
Richard Deeming22-Oct-13 9:38
mveRichard Deeming22-Oct-13 9:38 
GeneralRe: Parsing an XML File Pin
MarkB12322-Oct-13 10:03
MarkB12322-Oct-13 10:03 
GeneralRe: Parsing an XML File Pin
Richard Deeming22-Oct-13 10:59
mveRichard Deeming22-Oct-13 10:59 
GeneralRe: Parsing an XML File Pin
MarkB12323-Oct-13 6:49
MarkB12323-Oct-13 6:49 
QuestionHow to Get Site ID and site status in IIS 7 using Web Administration Pin
Member 1032854322-Oct-13 4:11
Member 1032854322-Oct-13 4:11 
QuestionHow to access .chm file present in zip file in C# winform application ? Pin
Pratik_P21-Oct-13 21:35
Pratik_P21-Oct-13 21:35 
AnswerRe: How to access .chm file present in zip file in C# winform application ? Pin
Marco Bertschi21-Oct-13 21:50
protectorMarco Bertschi21-Oct-13 21:50 
GeneralRe: How to access .chm file present in zip file in C# winform application ? Pin
Pratik_P21-Oct-13 22:00
Pratik_P21-Oct-13 22:00 
SuggestionRe: How to access .chm file present in zip file in C# winform application ? Pin
Eddy Vluggen21-Oct-13 22:30
professionalEddy Vluggen21-Oct-13 22:30 
AnswerRe: How to access .chm file present in zip file in C# winform application ? Pin
Marco Bertschi21-Oct-13 22:39
protectorMarco Bertschi21-Oct-13 22:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.