Динамическое отображение компонента 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.

2. Click Windows Forms Application and then click OK.

3. 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

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

colorDialog1 appears in the component tray.

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

6. 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; }

7. Press F5 to run the code.

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

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

10. Close the application.




Дата добавления: 2022-05-27; просмотров: 145;


Поиск по сайту:

Воспользовавшись поиском можно найти нужную информацию на сайте.

Поделитесь с друзьями:

Считаете данную информацию полезной, тогда расскажите друзьям в соц. сетях.
Poznayka.org - Познайка.Орг - 2016-2024 год. Материал предоставляется для ознакомительных и учебных целей.
Генерация страницы за: 0.007 сек.