Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_for_Beginners.doc
Скачиваний:
35
Добавлен:
13.02.2015
Размер:
2.39 Mб
Скачать

How to: Display an OpenFileDialog Dynamically

You can use the OpenFileDialog component to enable users to browse to a text file and load the selected file to a RichTextBox control on a Windows Form. This example instantiates OpenFileDialog at run-time.

Example

// Create an OpenFileDialog object.

OpenFileDialog openFile1 = new OpenFileDialog();

// Initialize the OpenFileDialog to look for text files.

openFile1.Filter = "Text Files|*.txt";

// Check if the user selected a file from the OpenFileDialog.

if(openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)

// Load the contents of the file into a RichTextBox control.

richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);

Compiling the Code

  • Copy the code into the Load_Form1 event handler. When you run the program, you will be prompted to select a text file. The contents of the selected file will be displayed in a RichTextBox control.

Robust Programming

Use the CheckFileExists, CheckPathExists, DefaultExt, Filter, Multiselect, and ValidateNames

Динамическое отображение компонента OpenFileDialog

Чтобы пользовали могли выбрать текстовый файл и загрузить его в элемент управления RichTextBox в форме Windows Forms, можно использовать компонент OpenFileDialog. В этом примере компонент OpenFileDialog создается во время выполнения.

Пример

// Create an OpenFileDialog object.

OpenFileDialog openFile1 = new OpenFileDialog();

// Initialize the OpenFileDialog to look for text files.

openFile1.Filter = "Text Files|*.txt";

// Check if the user selected a file from the OpenFileDialog.

if(openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)

// Load the contents of the file into a RichTextBox control.

richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText);

Компиляция кода

  • Скопируйте код в обработчик событий Load_Form1. При выполнении программы будет выведен запрос на выбор текстового файла. Содержимое выбранного файла будет отображено в элементе управления RichTextBox.

Надежное программирование

Чтобы сократить число ошибок во время выполнения, используйте свойства CheckFileExists, CheckPathExists, DefaultExt, Filter, Multiselect и ValidateNames элемента управления OpenFileDialog.

How to: Display a Color Pallet

You can use the built-in ColorDialog component to display a color dialog box instead of creating your own color pallet. For example, you can enable users to select a color to apply to a Windows form when they click a button on the form.

To display the color dialog box

  1. On the File menu, click New Project.

The New Project dialog box appears.

  1. Click Windows Forms Application and then click OK.

  2. From the Toolbox, drag a Button control to the form, and change the following properties in the Properties window:

    Property

    Value

    Name

    formColor

    Text

    Color

  3. Drag a ColorDialog component from the Dialogs tab of the Toolbox to the form.

colorDialog1 appears in the component tray.

  1. Double-click the Color button to create the default event handler in the Code Editor.

  2. In the formColor_Click event handler, add the following code to display the color dialog box and change the background color of the form according to the user's choice.

    if (colorDialog1.ShowDialog() == DialogResult.OK)

    {

    this.BackColor = colorDialog1.Color;

    }

  3. Press F5 to run the code.

  4. When the form opens, click Color, click a color in the resulting dialog box, and then click OK.

  5. Verify that the chosen color is applied to the form.

  6. Close the application.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]