|
|
|
The General Winforms Interview Questions consists the most frequently
asked questions in Winforms. This list of 100+ questions guage your familiarity
with the Winforms platform. The q&a have been collected over a period
of time from various blogs, forums and other similar Winforms sites
|
18.Windows Forms Common Dialogs
|
| 18.1 How do I
use the ColorDialog to pick a color?
|
| 18.2 How do I
use the FontDialog class to set a control's font?
|
| 18.3 How do I
implement a Folder Browser class?
|
| 18.4 How can I
get just the name of a file from the complete path string?
|
| 18.5 How can I
get just the extension of a file from the complete path string?
|
| 18.6 How do I
use the OpenFileDialog?
|
| 18.7How do I
specify the path for the FolderBrowser instance when it opens the first time?
|
18.1 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";
|
18.2 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";
|
18.3 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;
}
|
18.4 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);
|
18.5 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);
|
18.6 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();
}
}
|
18.7 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.
|