|
|
|
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
|
49. VS.NET Macros
|
| 49.1Can VB.NET code be
formatted using macros?
|
| 49.2 Is it possible to
call macros as part of a Visual Studio.NET command line build (or from a batch
file)?
|
49.1 Can VB.NET code be formatted using macros?
|
|
B.NET can perform decent autoformatting for code that may not be properly
formatted. Here is a macro that can call this Auto Formatting command for all
the files in a project.
|
49.2 Is it possible to call macros as part of a Visual Studio.NET command line
build (or from a batch file)?
|
|
You can do this by starting a new thread and executing Application.Run for the
status dialog form when the background thread starts running. To communicate
changes in percentage use BeginInvoke to executes a specific delegate
asynchronously on the thread that the form was created on.
In order to use BackgroundThreadStatusDialog from your code you have to update
the Progress inside your loop and check the IsCanceled state to detect if the
user pressed the Cancel button.
|
private void button1_Click(object sender, System.EventArgs e)
{
BackgroundThreadStatusDialog statusDialog = new BackgroundThreadStatusDialog();
try
{
for (int n = 0; n < 1000; n++)
{
statusDialog.Percent = n/10;
int ticks = System.Environment.TickCount;
while (System.Environment.TickCount - ticks < 10)
;
if (statusDialog.IsCanceled)
return;
}
statusDialog.Close();
MessageBox.Show(statusDialog.IsCanceled ? "Canceled" : "Success");
}
finally
{
statusDialog.Close();
}
}
|
|
Check out this sample project at gotnetdot.com. It derives from Form and
implements the owner drawing by handling the DrawItem event and MeasureItem
event.
|
// 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;
}
|
19.4 How can I set the width of a listbox to fit the text?
|
|
You can subclass ComboBox. In your derived class, make sure you set
|
this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.DropDownStyle = ComboBoxStyle.DropDownList;
|