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

How to: Browse a Folder

You can use the built-in FolderBrowserDialog component to enable users to browse a folder. To display a dialog box, you use the ShowDialog method. You can then check whether the user clicked the OK button by using the DialogResult..::.OK field.

To display the folder browser 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. Add a Label control to the form, and use the default name, Label1.

  3. Add a Button control to the form, and change the following properties in the Properties window:

Property

Value

Name

folderPath

Text

Path

  1. Drag a FolderBrowserDialog component from the Dialogs tab of the Toolbox to the form.

folderBrowserDialog1 appears in the component tray.

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

  2. In the folderPath_Click event handler, add the following code to display the folder browser dialog box and display the selected path in the label.

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

    {

    this.label1.Text = folderBrowserDialog1.SelectedPath;

    }

  3. Press F5 to run the code.

  4. When the form appears, click Path, click a folder in the list, and then click OK.

  5. Verify that the selected path appears in the label.

  6. Close the application.

Просмотр папки

Чтобы пользователи могли выбирать папки, можно использовать встроенный компонент FolderBrowserDialog. Чтобы отобразить диалоговое окно, используйте метод ShowDialog. Затем с помощью поля DialogResult..::.OK можно проверить, нажал ли пользователь кнопку ОК.

Отображение диалогового окна обозревателя папок

    1. В меню Файл выберите команду Создать проект.

Откроется диалоговое окно Создание проекта.

    1. Выберите элемент Приложение Windows Forms и нажмите кнопку ОК.

    2. Добавьте в форму элемент управления Label и именем по умолчанию Label1.

    3. Добавьте элемент управления Button в форму и измените следующие свойства в окне Свойства.

Свойство

Значение

Имя

folderPath

Текст

Путь

  1. С вкладки Диалоговые окна панели элементов перетащите в форму компонент FolderBrowserDialog.

В области компонентов появится компонент folderBrowserDialog1.

  1. Дважды щелкните кнопку, чтобы создать в редакторе кода обработчик событий по умолчанию.

  2. В обработчик событий folderPath_Click добавьте следующий код для отображения диалогового окна обозревателя папок и отображения выбранного пути в надписи.

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

    {

    this.label1.Text = folderBrowserDialog1.SelectedPath;

    }

  3. Нажмите клавишу F5 для выполнения кода.

  4. В открывшейся форме нажмите кнопку Путь, выберите папку в списке и нажмите кнопку ОК.

  5. Убедитесь, что выбранный путь появился в надписи.

  6. Закройте приложение.

How to: Save a File to a Folder

You can use the built-in SaveFileDialog component to enable users to save a file to a folder. To display a dialog box, you use the ShowDialog method. You can then check whether the user clicked the OK button by using the DialogResult..::.OK field.

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