Tải bản đầy đủ - 0 (trang)
7-6. Save Configuration Settings for a Form

7-6. Save Configuration Settings for a Form

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

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



CHAPTER 7 ■ WINDOWS FORMS



[STAThread]

public static void Main(string[] args)

{

Application.Run(new Recipe07_06());

}

}

}



Figure 7-4. Configuring Application Settings in Visual Studio



324



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



7-7. Force a List Box to Scroll to the Most Recently Added

Item

Problem

You need to scroll a list box programmatically so that the most recently added items are visible.



Solution

Set the ListBox.TopIndex property, which sets the first visible list item.



How It Works

In some cases, you might have a list box that stores a significant amount of information or one that you

add information to periodically. Often, the most recent information, which is added at the end of the list,

is more important than the information at the top of the list. One solution is to scroll the list box so that

recently added items are visible. The ListBox.TopIndex property enables you to do this by allowing you

to specify which item is visible at the top of the list.



The Code

The following sample form includes a list box and a button. Each time the button is clicked, 20 items are

added to the list box. Each time new items are added, the code sets the ListBox.TopIndex property and

forces the list box to display the most recently added items. To provide better feedback, the same line is

also selected.

The example uses an unsorted ListBox, which means that new items are added to the end of the

ListBox. If you set ListBox.Sorted to true, the ListBox will sort the items it contains alphabetically. In

this case, new items added to the ListBox will be inserted at the appropriate point in the list and the

ListBox.Add method returns an int containing the zero-based index of where the new item was inserted.

You can assign this value to the ListBox.TopIndex property and force a sorted list box to display the most

recently added item.

using System;

using System.Windows.Forms;

namespace Apress.VisualCSharpRecipes.Chapter07

{

public partial class Recipe07_07 : Form

{

// A counter to keep track of the number of items added

// to the ListBox.

private int counter = 0;



325



www.it-ebooks.info



CHAPTER 7 ■ WINDOWS FORMS



public Recipe07_07()

{

// Initialization code is designer generated and contained

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

InitializeComponent();

}

// Button click event handler adds 20 new items to the ListBox.

private void cmdTest_Click(object sender, EventArgs e)

{

// Add 20 items.

for (int i = 0; i < 20; i++)

{

counter++;

listBox1.Items.Add("Item " + counter.ToString());

}

// Set the TopIndex property of the ListBox to ensure the

// most recently added items are visible.

listBox1.TopIndex = listBox1.Items.Count - 1;

listBox1.SelectedIndex = listBox1.Items.Count - 1;

}

[STAThread]

public static void Main(string[] args)

{

Application.Run(new Recipe07_07());

}

}

}



7-8. Restrict a Text Box to Accept Only Specific Input

Problem

You need to create a text box that will reject all nonnumeric keystrokes.



Solution

Use the MaskedTextBox control and set the Mask property to configure the input that is acceptable.



How It Works

One way to ensure user input is valid is to prevent invalid data from being entered in the first place. The

MaskedTextBox control facilitates this approach. The MaskedTextBox.Mask property takes a string that

specifies the input mask for the control. This mask determines what type of input a user can enter at



326



www.it-ebooks.info



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

7-6. Save Configuration Settings for a Form

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

×