C # implement folder selection box

xiaoxiao2021-04-02  196

Everyone uses the files in the C # to open / save the dialog box? It is convenient and easy to play. But unfortunately, C # actually does not provide the same folder selection box, many people use the system's functions through P / I Invoke To implement the dialog, it is quite inconvenient ~~ In fact, the FoldernameEditor class in C # implements the function of the folder selection box to some extent, but not fully packaged. Oh, I will give readers today. Use C # yourself to implement the dialog box, use it as easy to use as OpenFileDialog.

Select the box class to the folder OpenFolderDialog, the class is designed as follows:

public

Class OpenFolderDialog: FoldernameEditor, IDisposable

{

FoldernameEditor.FolderBrowser fdialog = new foldernameEditor.folderBrowser ();

Public openfolderdialog ()

{

}

Public DialogResult showdialog ()

{

Return ShowDialog ("SELECT A Folder:");

}

Public DialogResult ShowDialog (String Description)

{

FDIALOG.DESCRIPTION = Description;

Return fdialog.showdialog ();

}

Public String Path

{

get

{

Return fdialog.directorypath;

}

}

Public void dispose ()

{

FDIALOG.DISPOSE ();

}

}

Use example:

Private

Void SelectFolderButton_Click (Object Sender, Eventargs E)

{

Using (OpenFolderDialog OpenFolderdlg = New OpenFolderDialog ())

{

IF (OpenFolderdlg.ShowDialog () == DialogResult.ok)

{

this.outfoldertextBox.text = OpenFolderdlg.path;

}

}

}

Below is the example of the use of the OpenFolderDialog, the control is not very similar?

Private

Void SelectFileButton_Click (Object Sender, Eventargs E)

{

Using (OpenFileDialog OpenDLG = New OpenFiledialog ())

{

OpenDLG.FILTER = @ "all text files (*. txt) | * .txt";

OpenDLG.Multiselectriselectr = FALSE;

OpenDLG.RESTOREDIRECTORY = true;

OpenDLG.FILTERINDEX = 1;

IF (OpenDLG.ShowDialog () == DialogResult.ok)

{

This.FilePathtextBox.Text = Opendlg.FileName;

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-131413.html

New Post(0)