WinForms FAQ - Common Dialogs

How do I specify the path for the FolderBrowser instance when it opens the first time?

In the 1.1 framework there is a SelectedPath property that will let you do this.

Permalink
How do I implement a Folder Browser class

Below is a technique that uses FolderNameEditor and FolderBrowser classes to implement a solution. You can also use iterop to get a solution.

Both the FolderNameEditor and FolderBrowser classes used in this solution are described in the Docs as ‘This type supports the .NET Framework infrastructure and is not intended to be used directly from your code.’

	// add a reference to System.Design.DLL

	using System.Windows.Forms.Design;
	.............

	public class DirBrowser : FolderNameEditor
	{
		FolderBrowser fb = new FolderBrowser();

		public string Description
		{
			set { _description = value; }
			get { return _description; }
		}

		public string ReturnPath
		{
			get { return _returnPath; }
		}

		public DirBrowser() { }

		public DialogResult ShowDialog()
		{
			fb.Description = _description;
			fb.StartLocation = FolderBrowserFolder.MyComputer;

			DialogResult r = fb.ShowDialog();
			if (r == DialogResult.OK)
				_returnPath = fb.DirectoryPath;
			else
				_returnPath = String.Empty;

			return r;
		}

		private string _description = 'Choose Directory';
		private string _returnPath = String.Empty;
	}

(Posted by Ryan Farley on the microsoft.public.dotnet.language.csharp newsgroup)

Permalink
How do I use the OpenFileDialog
	using System.Text;
	using System.IO;
	....
	private void button1_Click(object sender, System.EventArgs e)
	{
		OpenFileDialog dlg = new OpenFileDialog(); 
		dlg.Title = 'Open text file' ; 
		dlg.InitialDirectory = @'c:\' ; 
		dlg.Filter = 'txt files (*.txt)|*.txt|All files (*.*)|*.*' ; 
		 
		if(dlg.ShowDialog() == DialogResult.OK) 
		{ 
			StreamReader sr = File.OpenText(dlg.FileName);

			string s = sr.ReadLine();
			StringBuilder sb = new StringBuilder();
			while (s != null)
			{
				sb.Append(s);
				s = sr.ReadLine();
			}
			sr.Close();
			textBox1.Text = sb.ToString();
		} 
	}
Permalink
How can I get just the extension of a file from the complete path string?

Use FileInfo. Instantiate a FileInfo object with the full path as constructor arg. Then simply call FileInfo.Extension and you will get just the extension of the file.

FileInfo finfo = new FileInfo(strFileName);
Console.WriteLine(finfo.Extension);
Permalink
How can I get just the name of a file from the complete path string?

Use FileInfo. Instantiate a FileInfo object with the full path as constructor arg. Then simply call FileInfo.Name and you will get just the name of the file.

FileInfo finfo = new FileInfo(strFileName);
Console.WriteLine(finfo.Name);
Permalink
How do I use the ColorDialog to pick a color

It is straight-forward. Create an instance of the class and call its ShowDialog method.

	ColorDialog colorDialog1 = new ColorDialog(); 
	//fontDialog1.ShowColor = true; 
	if(colorDialog1.ShowDialog() != DialogResult.Cancel ) 
	{ 
		textBox1.ForeColor = colorDialog1.Color; 
	} 
	textBox1.Text = 'this is a test';
Permalink
How do I use the FontDialog class to set a control’s font

It is straight-forward. Create an instance of the class and call its ShowDialog method.

     FontDialog fontDialog1 = new FontDialog(); 
      fontDialog1.ShowColor = true; 
      if(fontDialog1.ShowDialog() != DialogResult.Cancel ) 
      { 
           textBox1.Font = fontDialog1.Font ; 
           textBox1.ForeColor = fontDialog1.Color; 
      } 
  
     textBox1.Text = 'this is a test';
Permalink