PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSShellCommonFileDialog/CFDInCodePackForm.cs

#
C# | 275 lines | 139 code | 35 blank | 101 comment | 17 complexity | e7ba0712a97b014f2282e249cf6c974a MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: CFDInCodePackForm.cs
  3. * Project: CSShellCommonFileDialog
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. *
  7. *
  8. * This source is subject to the Microsoft Public License.
  9. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  10. * All other rights reserved.
  11. *
  12. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  13. * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  15. \***************************************************************************/
  16. #region Using directives
  17. using System;
  18. using System.Collections.Generic;
  19. using System.ComponentModel;
  20. using System.Data;
  21. using System.Drawing;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Windows.Forms;
  25. using Microsoft.WindowsAPICodePack.Dialogs;
  26. using Microsoft.WindowsAPICodePack.Shell;
  27. using Microsoft.WindowsAPICodePack.Dialogs.Controls;
  28. using System.IO;
  29. #endregion
  30. namespace CSShellCommonFileDialog
  31. {
  32. public partial class CFDInCodePackForm : Form
  33. {
  34. public CFDInCodePackForm()
  35. {
  36. InitializeComponent();
  37. }
  38. #region Common Open File Dialogs
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. /// <param name="sender"></param>
  43. /// <param name="e"></param>
  44. private void btnOpenAFile_Click(object sender, EventArgs e)
  45. {
  46. // Create a new common open file dialog
  47. CommonOpenFileDialog openFileDialog = new CommonOpenFileDialog();
  48. // (Optional) Set the title of the dialog
  49. openFileDialog.Title = "Select a File";
  50. // (Optional) Control the default folder of the file dialog
  51. // Here we set it as the Music library knownfolder
  52. openFileDialog.InitialDirectoryShellContainer =
  53. KnownFolders.MusicLibrary as ShellContainer;
  54. // (Optional) Specify file types for the file dialog
  55. openFileDialog.Filters.Add(new CommonFileDialogFilter(
  56. "Word Files", "*.docx"));
  57. openFileDialog.Filters.Add(new CommonFileDialogFilter(
  58. "Text Files", "*.txt"));
  59. openFileDialog.Filters.Add(new CommonFileDialogFilter(
  60. "All Files", "*.*"));
  61. // (Optional) Set the default extension to be added as ".docx"
  62. openFileDialog.DefaultExtension = "docx";
  63. // Show the dialog
  64. if (openFileDialog.ShowDialog() == CommonFileDialogResult.OK)
  65. {
  66. // Get the selection from the user
  67. ShellObject shellObj = openFileDialog.FileAsShellObject;
  68. MessageBox.Show(shellObj.ParsingName, "The selected file is");
  69. }
  70. }
  71. /// <summary>
  72. ///
  73. /// </summary>
  74. /// <param name="sender"></param>
  75. /// <param name="e"></param>
  76. private void btnOpenAFolder_Click(object sender, EventArgs e)
  77. {
  78. // Create a new common open file dialog
  79. CommonOpenFileDialog openFileDialog = new CommonOpenFileDialog();
  80. // Set the dialog as a folder picker
  81. openFileDialog.IsFolderPicker = true;
  82. // (Optional) Set the title of the dialog
  83. openFileDialog.Title = "Select a Folder";
  84. // Show the dialog
  85. if (openFileDialog.ShowDialog() == CommonFileDialogResult.OK)
  86. {
  87. // Get the selection from the user
  88. ShellObject shellObj = openFileDialog.FileAsShellObject;
  89. MessageBox.Show(shellObj.ParsingName, "The selected folder is");
  90. }
  91. }
  92. /// <summary>
  93. ///
  94. /// </summary>
  95. /// <param name="sender"></param>
  96. /// <param name="e"></param>
  97. private void btnOpenFiles_Click(object sender, EventArgs e)
  98. {
  99. // Create a new common open file dialog
  100. CommonOpenFileDialog openFileDialog = new CommonOpenFileDialog();
  101. // Allow mult-selection
  102. openFileDialog.Multiselect = true;
  103. // (Optional) Set the title of the dialog
  104. openFileDialog.Title = "Select Files";
  105. // Show the dialog
  106. if (openFileDialog.ShowDialog() == CommonFileDialogResult.OK)
  107. {
  108. // Get the selections from the user
  109. StringBuilder selectedFiles = new StringBuilder();
  110. foreach (ShellObject shellObj in openFileDialog.FilesAsShellObject)
  111. {
  112. selectedFiles.AppendLine(shellObj.ParsingName);
  113. }
  114. MessageBox.Show(selectedFiles.ToString(), "The selected files are");
  115. }
  116. }
  117. /// <summary>
  118. /// This code snippet demonstrates how to add custom controls in the Common File
  119. /// Dialog.
  120. /// </summary>
  121. /// <param name="sender"></param>
  122. /// <param name="e"></param>
  123. private void btnAddCustomControls_Click(object sender, EventArgs e)
  124. {
  125. // Create a new common open file dialog
  126. CommonOpenFileDialog openFileDialog = new CommonOpenFileDialog();
  127. // Prepare the controls to be added and the relationship among
  128. // the controls
  129. CommonFileDialogGroupBox grp = new CommonFileDialogGroupBox(
  130. "grp", "Change Title to ");
  131. CommonFileDialogRadioButtonList radl = new
  132. CommonFileDialogRadioButtonList("radl");
  133. grp.Items.Add(radl);
  134. radl.Items.Add(new CommonFileDialogRadioButtonListItem("Windows Vista"));
  135. radl.Items.Add(new CommonFileDialogRadioButtonListItem("Windows 7"));
  136. radl.SelectedIndexChanged += new EventHandler(radl_SelectedIndexChanged);
  137. // Add the firt-level control to the CommonFileDialog.Controls
  138. // collection to apply the changes of controls.
  139. openFileDialog.Controls.Add(grp);
  140. // Show the dialog
  141. if (openFileDialog.ShowDialog() == CommonFileDialogResult.OK)
  142. {
  143. // Get the value of the controls added to the dialog
  144. if (radl.SelectedIndex >= 0)
  145. {
  146. MessageBox.Show(radl.Items[radl.SelectedIndex].Text,
  147. "The selected Title is");
  148. }
  149. else
  150. {
  151. MessageBox.Show("No Title was selected");
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// This method gets called when selection happens to an dialog
  157. /// control item, radio-button. For sample sake, we change the dialog
  158. /// title according to the current selection in the readio button
  159. /// list.
  160. /// </summary>
  161. /// <param name="sender"></param>
  162. /// <param name="e"></param>
  163. private void radl_SelectedIndexChanged(object sender, EventArgs e)
  164. {
  165. // Get the radio button list control that triggers the selection-
  166. // changed event
  167. CommonFileDialogRadioButtonList radl = sender as CommonFileDialogRadioButtonList;
  168. if (radl != null)
  169. {
  170. // Get the parent common file dialog object
  171. CommonOpenFileDialog openFileDialog = radl.HostingDialog
  172. as CommonOpenFileDialog;
  173. if (openFileDialog != null)
  174. {
  175. // Change the title of the dialog based on the radio
  176. // button selection
  177. openFileDialog.Title = radl.Items[radl.SelectedIndex].Text;
  178. }
  179. }
  180. }
  181. /// <summary>
  182. /// The Common Places area in the Common File Dialog is extensible.
  183. /// This code snippet demonstrates how to extend the Common Places
  184. /// area.
  185. /// </summary>
  186. /// <param name="sender"></param>
  187. /// <param name="e"></param>
  188. private void btnAddCommonPlaces_Click(object sender, EventArgs e)
  189. {
  190. // Create a new common open file dialog
  191. CommonOpenFileDialog openFileDialog = new CommonOpenFileDialog();
  192. // Add the place (the PublicMusic knownfolder) to the bottom of
  193. // default list in Common File Dialog.
  194. openFileDialog.AddPlace(KnownFolders.PublicMusic as ShellContainer,
  195. FileDialogAddPlaceLocation.Bottom);
  196. // Show the dialog
  197. if (openFileDialog.ShowDialog() == CommonFileDialogResult.OK)
  198. {
  199. // You can add your own code here to handle the results.
  200. }
  201. }
  202. #endregion
  203. #region Common Save File Dialogs
  204. /// <summary>
  205. ///
  206. /// </summary>
  207. /// <param name="sender"></param>
  208. /// <param name="e"></param>
  209. private void btnSaveAFile_Click(object sender, EventArgs e)
  210. {
  211. // Create a new common save file dialog
  212. CommonSaveFileDialog saveFileDialog = new CommonSaveFileDialog();
  213. // (Optional) Set the title of the dialog
  214. saveFileDialog.Title = "Save a File";
  215. // (Optional) Specify file types for the file dialog
  216. saveFileDialog.Filters.Add(new CommonFileDialogFilter(
  217. "Word Files", "*.docx"));
  218. saveFileDialog.Filters.Add(new CommonFileDialogFilter(
  219. "Text Files", "*.txt"));
  220. // (Optional) Set the default extension to be added as ".docx"
  221. saveFileDialog.DefaultExtension = "docx";
  222. // (Optional) Display a warning if the user specifies a file name
  223. // that aleady exists. This is a default value for the Save dialog.
  224. saveFileDialog.OverwritePrompt = true;
  225. // Show the dialog
  226. if (saveFileDialog.ShowDialog() == CommonFileDialogResult.OK)
  227. {
  228. // Open and save to the file
  229. using (FileStream fs = new FileStream(saveFileDialog.FileName,
  230. FileMode.Create))
  231. {
  232. // Write to the file stream.
  233. // ...
  234. }
  235. MessageBox.Show(saveFileDialog.FileName, "The saved file is");
  236. }
  237. }
  238. #endregion
  239. }
  240. }