Tải bản đầy đủ - 0 (trang)
6-16. Modify an XML Tree with LINQ

6-16. Modify an XML Tree with LINQ

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

CHAPTER 6 ■ XML PROCESSING



// Write out the XML.

Console.WriteLine(rootElem);

Console.WriteLine("Press enter to continue");

Console.ReadLine();

// Select all of the product elements.

IEnumerable prodElements

= from elem in rootElem.Element("products").Elements()

where (elem.Name == "product")

select elem;

// Run through the elements and change the ID attribute.

foreach(XElement elem in prodElements)

{

// Get the current product ID.

int current_id = Int32.Parse((string)elem.Attribute("id"));

// Perform the replace operation on the attribute.

elem.ReplaceAttributes(new XAttribute("id", current_id + 500));

}

Console.WriteLine(rootElem);

Console.WriteLine("Press enter to continue");

Console.ReadLine();

// Remove all elements that contain the word "tea" in the description.

IEnumerable teaElements = from elem in

rootElem.Element("products").Elements()

where (((string)elem.Element("description")).Contains("tea"))

select elem;

foreach (XElement elem in teaElements)

{

elem.Remove();

}

Console.WriteLine(rootElem);

Console.WriteLine("Press enter to continue");

Console.ReadLine();

// Define and add a new element.

XElement newElement = new XElement("product",

new XAttribute("id", 3000),

new XElement("productName", "Chrome French Press"),

new XElement("description",

"A simple and elegant way of making great coffee"),

new XElement("productPrice", 25.00),

new XElement("inStock", true));



304



www.it-ebooks.info



CHAPTER 6 ■ XML PROCESSING



rootElem.Element("products").Add(newElement);

Console.WriteLine(rootElem);

}

}

}



305



www.it-ebooks.info



www.it-ebooks.info



CHAPTER 7

■■■



Windows Forms

The Microsoft .NET Framework includes a rich set of classes for creating traditional Windows-based

applications in the System.Windows.Forms namespace. These range from basic controls such as the

TextBox, Button, and MainMenu classes to specialized controls such as TreeView, LinkLabel, and

NotifyIcon. In addition, you will find all the tools you need to manage multiple document interface

(MDI) applications, integrate context-sensitive help, and even create multilingual user interfaces—all

without needing to resort to the complexities of the Win32 API.

Most C# developers quickly find themselves at home with the Windows Forms programming model,

and despite the arrival of Windows Presentation Foundation (discussed in Chapter 17) as an alternative

thick-client development technology, Windows Forms is still the best choice for many types of

applications.



■ Note Most of the recipes in this chapter use control classes, which are defined in the System.Windows.Forms

namespace. When introducing these classes, the full namespace name is not indicated, and System.Windows.

Forms is assumed.



The recipes in this chapter describe how to do the following:





Add controls to a form programmatically at runtime so that you can build forms

dynamically instead of only building static forms in the Visual Studio forms

designer (recipe 7-1)







Link arbitrary data objects to controls to provide an easy way to associate data

with a control without the need to maintain additional data structures (recipe 7-2)







Process all the controls on a form in a generic way (recipe 7-3)







Track all the forms and MDI forms in an application (recipes 7-4 and 7-5)







Save user-based and computer-based configuration information for Windows

Forms applications using the mechanisms built into the .NET Framework and

Windows (recipe 7-6)



307



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS







Force a list box to always display the most recently added item, so that users do

not need to scroll up and down to find it (recipe 7-7)







Assist input validation by restricting what data a user can enter into a text box, and

implement a component-based mechanism for validating user input and

reporting errors (recipes 7-8 and 7-17)







Implement a custom autocomplete combo box so that you can make suggestions

for completing words as users type data (recipe 7-9)







Allow users to sort a list view based on the values in any column (recipe 7-10)







Avoid the need to explicitly lay out controls on a form by using the Windows

Forms layout controls (recipe 7-11)







Use part of a main menu in a context menu (recipe 7-12)







Provide multilingual support in your Windows Forms application (recipe 7-13)







Create forms that cannot be moved and create borderless forms that can be

moved (recipes 7-14 and 7-15)







Create an animated system tray icon for your application (recipe 7-16)







Support drag-and-drop functionality in your Windows Forms application (recipe

7-18)







Correctly update the user interface in a multithreaded application (recipe 7-19)







Display web-based information within your Windows application and allow users

to browse the Web from within your application (recipe 7-20)







Display WPF windows in a Windows Forms application (recipe 7-21)







Display WPF controls in a Windows Forms application (recipe 7-22)



■ Note Visual Studio, with its advanced design and editing capabilities, provides the easiest and most productive

way to develop Windows Forms applications. Therefore, the sample code projects for the recipes in this chapter—

unlike those in most other chapters—rely heavily on the use of Visual Studio. Instead of focusing on the library

classes that provide the required functionality, or looking at the code generated by Visual Studio, these recipes

focus on how to achieve the recipe’s goal using the Visual Studio user interface and the code that you must write

manually to complete the required functionality. The separation of generated and manual code is particularly

elegant in Visual Studio 2005 and later versions due to the extensive use of partial types.



308



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



7-1. Add a Control Programmatically

Problem

You need to add a control to a form at runtime, not design time.



Solution

Create an instance of the appropriate control class. Then add the control object to a form or a container

control by calling Controls.Add on the container. (The container’s Controls property returns a

ControlCollection instance.)



How It Works

In a .NET form-based application, there is really no difference between creating a control at design time

and creating it at runtime. When you create controls at design time, Visual Studio generates code to

instantiate the desired control and places the code in a special method named InitializeComponent,

which is called from your form’s constructor. Visual Studio makes use of the partial class functionality of

C# to keep the bulk of the code it generates in a separate file with the extension Designer.cs.

If you want to create a control at runtime, just follow these steps:

1.



Create an instance of the appropriate control class.



2.



Configure the control properties accordingly (particularly the size and position

coordinates).



3.



Add the control to the form or another container. Every control implements a

read-only Controls property that references a ControlCollection containing

references to all of its child controls. To add a child control, invoke the

ControlCollection.Add method.



4.



If you need to handle the events for the new control, you can wire them up to

existing methods.



If you need to add multiple controls to a form or container, you should call SuspendLayout on the

parent control before dynamically adding the new controls, and then call ResumeLayout once you have

finished. This temporarily disables the layout logic used to position controls and will allow you to avoid

significant performance overheads and weird flickering if you are adding many controls at once.



The Code

The following example demonstrates the dynamic creation of a list of check boxes. One check box is

added for each item in a string array. All the check boxes are added to a panel that has its AutoScroll

property set to true, which gives basic scrolling support to the check box list (see Figure 7-1).



309



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



using System;

using System.Windows.Forms;

namespace Apress.VisualCSharpRecipes.Chapter07

{

public partial class Recipe07_01 : Form

{

public Recipe07_01()

{

// Initialization code is designer generated and contained

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

InitializeComponent();

}

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);

// Create an array of strings to use as the labels for

// the dynamic check boxes.

string[] foods = {"Grain", "Bread", "Beans", "Eggs",

"Chicken", "Milk", "Fruit", "Vegetables",

"Pasta", "Rice", "Fish", "Beef"};

// Suspend the form's layout logic while multiple controls

// are added.

this.SuspendLayout();

// Specify the Y coordinate of the topmost check box in the list.

int topPosition = 10;

// Create one new check box for each name in the list of

// food types.

foreach (string food in foods)

{

// Create a new check box.

CheckBox checkBox = new CheckBox();

// Configure the new check box.

checkBox.Top = topPosition;

checkBox.Left = 10;

checkBox.Text = food;

// Set the Y coordinate of the next check box.

topPosition += 30;

// Add the check box to the panel contained by the form.

panel1.Controls.Add(checkBox);

}



310



www.it-ebooks.info



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

6-16. Modify an XML Tree with LINQ

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

×