Tải bản đầy đủ - 0 (trang)
7-5. Find All MDI Child Forms

7-5. Find All MDI Child Forms

Tải bản đầy đủ - 0trang

CHAPTER 7 ■ WINDOWS FORMS



using System;

using System.Windows.Forms;

namespace Apress.VisualCSharpRecipes.Chapter07

{

// An MDI parent form.

public partial class Recipe07_05Parent : Form

{

public Recipe07_05Parent()

{

// Initialization code is designer generated and contained

// in a separate file named Recipe07-05Parent.Designer.cs.

InitializeComponent();

}

// When the New menu item is clicked, create a new MDI child.

private void mnuNew_Click(object sender, EventArgs e)

{

Recipe07_05Child frm = new Recipe07_05Child();

frm.MdiParent = this;

frm.Show();

}

[STAThread]

public static void Main(string[] args)

{

Application.Run(new Recipe07_05Parent());

}

}

}

The following is the Recipe07-05Child class:

using System;

using System.Windows.Forms;

namespace Apress.VisualCSharpRecipes.Chapter07

{

// An MDI child form.

public partial class Recipe07_05Child : Form

{

public Recipe07_05Child()

{

// Initialization code is designer generated and contained

// in a separate file named Recipe07-05Child.Designer.cs.

InitializeComponent();

}



320



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



// When a button on any of the MDI child forms is clicked, display the

// contents of each form by enumerating the MdiChildren collection.

private void cmdShowAllWindows_Click(object sender, EventArgs e)

{

foreach (Form frm in this.MdiParent.MdiChildren)

{

// Cast the generic Form to the Recipe07_05Child derived class

// type.

Recipe07_05Child child = (Recipe07_05Child)frm;

MessageBox.Show(child.LabelText, frm.Text);

}

}

// On load, set the MDI child form's label to the current date/time.

protected override void OnLoad(EventArgs e)

{

// Call the OnLoad method of the base class to ensure the Load

// event is raised correctly.

base.OnLoad(e);

label.Text = DateTime.Now.ToString();

}

// A property to provide easy access to the label data.

public string LabelText

{

get { return label.Text; }

}

}

}



Figure 7-3. Getting information from multiple MDI child windows



321



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



7-6. Save Configuration Settings for a Form

Problem

You need to store configuration settings for a form so that they are remembered the next time that the

form is shown.



Solution

Use the .NET Framework Application Settings functionality, which is configurable at design time in

Visual Studio.



How It Works

The Application Settings functionality included in the .NET Framework provides an easy-to-use

mechanism through which you can save application and user settings used to customize the appearance

and operation of a Windows Forms application. You configure Application Settings through the

Properties panel of each Windows control (including the main Windows Form) in your application (see

Figure 7-4 for an example). By expanding the ApplicationSettings property (item 1 in Figure 7-4) and

clicking the ellipsis (three dots) to the right of (PropertyBinding), you can review application settings for

each property of the active control (item 2 in Figure 7-4). When you configure a new application setting

for a control’s property, you must assign it a name, a default value, and a scope (item 3).





The name allows you to both access the setting programmatically and reuse the

application setting across multiple controls.







The default value is used if the application cannot obtain a value from a

configuration file at runtime.







The scope is either User or Application.



Settings with an Application scope are stored in the application’s configuration file (usually located

in the same folder as the application assembly) and are read-only. The benefit of an Application scope is

that you can change configuration settings by editing the configuration file without needing to

recompile the application. Settings with a User scope are read-write by default and are stored as part of

the user’s Windows profile in a file named after the executing assembly.

When you configure your application to use application settings, Visual Studio actually

autogenerates a wrapper class that provides access to the configuration file information, regardless of

whether it is scoped as Application or User. The class is named Settings and implements the singleton

pattern (discussed in recipe 13-10); the singleton instance is accessed through Settings.Default. This

class contains properties with names matching each of the application setting names you configured for

your controls’ properties. The controls will automatically read their configuration at startup, but you

should store configuration changes prior to terminating your application by calling the Settings.

Default.Save method.



322



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



The Code

The following example, shown in Figure 7-4, displays a simple Form containing a TextBox. Using Visual

Studio, the application is configured to store the location of the Form and the background color of the

TextBox. The sample also programmatically records the size of the Form.

using

using

using

using



System;

System.ComponentModel;

System.Windows.Forms;

Apress.VisualCSharpRecipes.Chapter07.Properties;



namespace Apress.VisualCSharpRecipes.Chapter07

{

public partial class Recipe07_06 : Form

{

public Recipe07_06()

{

// Initialization code is designer generated and contained

// in a separate file named Recipe07-06.Designer.cs.

InitializeComponent();

}

private void Button_Click(object sender, EventArgs e)

{

// Change the color of the text box depending on which button

// was pressed.

Button btn = sender as Button;

if (btn != null)

{

// Set the background color of the text box.

textBox1.BackColor = btn.ForeColor;

// Update the application settings with the new value.

Settings.Default.Color = textBox1.BackColor;

}

}

protected override void OnClosing(CancelEventArgs e)

{

// Call the OnClosing method of the base class to ensure the

// FormClosing event is raised correctly.

base.OnClosing(e);

// Update the application settings for Form.

Settings.Default.Size = this.Size;

// Store all application settings.

Settings.Default.Save();

}



323



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

7-5. Find All MDI Child Forms

Tải bản đầy đủ ngay(0 tr)

×