Select a folder dialog control C #

xiaoxiao2021-03-05  29

We know that there is a dialog control for the C #, but there is no selection of the folder dialog control. Many people have to handle them with the TreeView class in order to select a folder. In fact, don't use it now in C #, we can The Sub-class FolderBrowser class for the FoldernameEditor class with C # implements the function of obtaining the browsing folder dialog. Let's take a look at how the specifically implemented. First create a new WinForm project, create a category file (file-> addnewItem-> Class). Because FoldernameEditor is under the System.Windows.Forms.Design namespace, this namespace is located in dynamic link library system.design.dll, so we must first add a new reference system.design in the solution. Then add: use system.windows.forms.design; then, we create a new FolderDialog class, inheriting from the FoldernameEditor class, newly created an instance of a FolderBrowser class in this class. The DisplayDialog (String Description) function is used to set the properties of instance fdialog and display the browsing folder dialog. In order to obtain the path of the folder, set a PATH property to return the path to the selected folder. Code is as follows: public class FolderDialog: FolderNameEditor ... {FolderNameEditor.FolderBrowser fDialog = newSystem.Windows.Forms.Design.FolderNameEditor.FolderBrowser (); public FolderDialog () ... {} public DialogResult DisplayDialog () ... {return DisplayDialog ("Please select a folder");} public DialogResult DisplayDialog (String Description) ... {fdialog.description = description; return fdialog.showdialog ();} public string path ... {get ... {Return FDIALOG.DIRECTORPATH;}} ~ folderdialog () ... {fdialog.dispose ();}} // Write the following code in the WinForm form, you can look at the running effect. Private Void Form1_Load (Object Sender, System.EventArgs E) ... {FolderDialog aa = new folderdialog (); aa.displaydialog (); messagebox.show (aa.path);}

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

New Post(0)