|
|
|
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
|
47. VS.NET Tips
|
| 47.1 Is there any easy way to convert VB.NET code to C#?
|
| 47.2 How can I word-wrap the Tooltip that is displayed?
|
| 47.3 How do I add a tooltip to my Button control?
|
| 47.4 How can I have a multiline tooltip?
|
| 47.5 How do I use VS to add an override for a virtual member of a baseclass?
|
| 47.6 How do I compile unsafe code in VS.NET?
|
| 47.7 How to convert an application project to class library project, and vice versa?
|
| 47.8 How do I start Visual Studio.NET from the command line?
|
| 47.9 What are the options with using Visual Studio.NET from the command line?
|
| 47.10 How do I create and use a component?
|
| 47.11 In Visual Studio, all my docking windows are out of whack. How can I reset them so things will be docked properly?
|
| 47.12 How do I move a docked control around (or change the dock order) in my Form?
|
| 47.13 How can I instruct the compiler to use the Main method in Class2 instead of Class1?
|
| 47.14 Whenever I share files between projects it appears that Visual Studio.NET copies the files over into the new project. I want to just share the file and not have it copied over. Is there any way to do this?
|
| 47.15 How can I graphically set the tab order for controls on a form?
|
| 47.16 How can I speed up my work in Visual Studio?
|
| 47.17 I have code snippets that I use often. How can I manage them so I can easily used them?
|
| 47.18 Can I drag and drop code snippets using the VS.NET IDE?
|
| 47.19 I get a 'This would cause two bindings in the collection to bind to the same property' error message. What might cause this?
|
| 47.20 In VS.NET, how can I change the build order of projects in my solution?
|
| 47.21 If I create an assembly, how can I see the assembly/components from with the VS.Net "Add..." dialogs?
|
| 47.22 How does VS.Net find the referenced assemblies when I build a project from the command line?
|
| 47.23 How can I get the default namespace of a C# project in VS.NET?
|
47.1 Is there any easy way to convert VB.NET code to C#?
|
|
If you add a BMP file to your solution
using the File|Add Existing Item... Menu Item, then change the
Build Action property of this BMP file to Embedded Resource,
you can then access this resource with code similar to:
|
// WindowsApplication6 corresponds to Default Namespace in your project settings.
// subfolders should be the folder names if any, inside which the bmp is added. If the bmp was added to the top level, you don't have to specify anything here.
string bmpName =
"WindowsApplication6.subfolders.sync.bmp";
System.IO.Stream strm = null;
try
{
strm =
this.GetType().Assembly.GetManifestResourceStream(bmpName);
pictureBox1.Image = new Bitmap(strm);
// Do the same for Icons
// Icon icon1 = new Icon(strm);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
if(strm !=
null)
strm.Close();
|
47.2 How can I word-wrap the Tooltip that is displayed?
|
|
Rhett Gong posted a work around using Reflection to achieve this in the microsoft.public.dotnet.framework.windowsforms.controls Newsgroup:
|
[DllImport("user32.dll")]
private extern static int SendMessage(IntPtr hwnd,uint msg, int wParam, int lParam);
.....
object o =
typeof(ToolTip).InvokeMember("Handle",BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.GetProperty,null,myToolTip,null);
IntPtr hwnd = (IntPtr) o;
SendMessage(hwnd,0x0418, 0, 300);
.....
|
47.3 How do I add a tooltip to my Button control?
|
|
From within the designer:
1) From the ToolBox, drop a ToolTip control to your form.
2) Check the properties of this toolTip1 object to make sure Active is set to true.
3) Click on your button, and in its Property page, set the ToolTip on toolTip1 entry.
From code:
|
private System.Windows.Forms.ToolTip toolTip1;
......
......
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.toolTip1.Active = true;
......
......
this.toolTip1.SetToolTip(this.button1, "Press for information...");
|
47.4 How can I have a multiline tooltip?
|
|
Include a \n as part of the tiptext.
|
|
this.toolTip1.SetToolTip(this.button1, "Press for\ninformation...");
|
47.5 How do I use VS to add an override for a virtual member of a baseclass?
|
|
In the ClassView window, expand the base class under your derived class. Then right-click the desired method, and select Add.
|
47.6How do I compile unsafe code in VS.NET?
|
|
Click "Project | Properties" from the menus.
Select "Configuration Properties" folder and the "Build" item under that.
Switch "Allow unsafe code blocks" from "False" to "True".
|
47.7 How to convert an application project to class library project, and vice versa?
|
|
In the Solutions Explorer window, right-click the exe project and then select Properties in the popup. Then change the Output type from Windows Application to Class Library. You may also want to comment out the Main method in your code as it is not needed in the library project.
To convert from a DLL to and
exe, reverse the process.
|
47.8 How do I start Visual Studio.NET from the command line?
|
|
Simply type devenv.exe from the command line. If you get a message like this, then you do not have devenv.exe in your path. >>> 'devenv.exe' is not recognized as an internal or external command, operable program or batch file. >>> To fix this simply run the batch file, vsvars32.bat that comes with Visual Studio.NET from the command line in the working folder. After you run this batch file devenv.exe will be available from the command line in that folder.
|
47.9 What are the options with using Visual Studio.NET from the command line?
|
|
The following HatchStyleEditor class draws a graphical representation for values in the HatchStyle enumeration. HatchStyleEditor is derived from UITypeEditor and overrides the GetPaintValueSupported method and returns true. GetPaintValueSupported indicates that this editor supports the painting of a representation of an object's value. PaintValue paints the representative value to the canvas.
|
public class HatchStyleEditor : UITypeEditor
{
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}
public override void PaintValue(PaintValueEventArgs e)
{
if (e.Value is HatchStyle)
{
HatchStyle hatch = (HatchStyle) e.Value;
Brush br = new HatchBrush(hatch, SystemColors.WindowText, SystemColors.Window);
e.Graphics.FillRectangle(br, e.Bounds);
br.Dispose();
}
}
}
|
47.10 How do I create and use a component?
|
|
Take a look at Mahash Chand's Creating C# Class Library (Dll) found on C# Corner.
|
47.11 In Visual Studio, all my docking windows are out of whack. How can I reset them so things will be docked properly?
|
|
You can use the menu item Tools|Options, and then select Reset Window Layout.
A second alternative is to close Visual Studio, and then delete the suo file in your project folder. (suo=Studio User Options)
|
47.12 How do I move a docked control around (or change the dock order) in my Form?
|
|
You cannot move it via drag-and-drop. But you change it's position or dock order by selecting the "Bring To Front" or "Send To Back" verbs in it's context menu. "Bring To Front" will move it to the top of the children list and "Send To Back" will move it to the last of the children list. This is possible because the docking logic will be applied on the children in the order in which they appear in the child controls list.
|
47.13 How can I instruct the compiler to use the Main method in Class2 instead of Class1?
|
|
In the project's General Properties, specify Class2 as the "Startup Object". The dropdown contains all classes with a Main method specified.
|
47.14 Whenever I share files between projects it appears that Visual Studio.NET copies the files over into the new project. I want to just share the file and not have it copied over. Is there any way to do this?
|
|
When you add an existing item from with the Visual Studio.NET IDE, click the small arrow on the 'Open' button in the dialog that is presented. You will see a list of options. One of these is to 'Link file'. If you select this option then you will not get a local copy and any changes that you make to the linked file will be in the original file. Another way to get the same effect is to share the files using Visual Source Safe. You can simply drag and drop the files between projects in VSS and they will also be in sync.
|
47.15 How can I graphically set the tab order for controls on a form?
|
|
While MFC programmer's have had this feature available in earlier versions of Visual Studio, VB programmers have not. To display tab orders on the active design mode Form, select the View | Tab Order menu item. Then click the numbers on each control to reset their tab order.
|
47.17 How can I speed up my work in Visual Studio?
|
|
Close the Dynamic Help window unless it is something you use. Keeping it open will slow things down.
|
47.17 I have code snippets that I use often. How can I manage them so I can easily used them?
|
|
In Visual Studio, you can store
commonly used code snippets on a tab (other than the Clipboard
ring) in your Toolbox. You use drag and drop techniques to
move the snippets to the Toolbox. To later use a snippet in
your code, you drag it from the Toolbox and drop it into your
code.
|
47.18 Can I drag and drop code snippets using the VS.NET IDE?
|
|
Yes, in Code View you can select and drag code to aTab (General Tab) in the ToolBox and then you can drag the code from the Tab to the desired location.
|
47.19 In VS.NET, how can I change the build order of projects in my solution?
|
|
You can set the build order of the projects by right clicking on the Solution and choosing Project Build Order and adjusting the dependencies on the Dependencies tab in the Project Dependencies window that pops up.
|
47.20 In VS.NET, how can I specify the start up project for my application?
|
|
In the Solution Explorer right click on the project that should be the start up project for your application and choose the Set As Start Up Project option.
|
47.21 If I create an assembly, how can I see the assembly/components from with the VS.Net "Add..." dialogs?
|
There are two ways in which this can be done. The first way is to use the provided Public assembly folder that is installed with VS.Net.
This folder is: "C:\Program Files\Microsoft Visual Studio .NET\Common7\IDE\PublicAssemblies"
All assemblies in this folder will be picked up by the "Add Reference" dialog under the ".NET" tab, and the "Customize Toolbox" under the ".NET Framework Components" tab.
Now, you are not limited to using this folder. You can specify your own public assembly folder by adding a key to the registry.
If you look at the following key:
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders", you will see a subkey named "PublicAssemblies". This is where the path above is specified as a public assembly folder. To add your own folder, create a key (e.g. MyAssemblies), and set the default value to be the desired path.
|
47.22 How does VS.Net find the referenced assemblies when I build a project from the command line?
|
|
There are a couple of ways to specify where VS.Net looks for the assemblies that you reference in your project when building from the command line.
One way is to specify the "HintPath" for the reference:
Name =
"MyAssembly.dll"
AssemblyName =
"MyAssembly.dll"
HintPath =
"..\work\samples\assemblies\MyAssembly.dll"
/>
However, this would require all of the systems that build the application to have the exact same directory structure locally. There are often times when this is not wanted (or needed), such as having a script on a build machine.
In this type of situation, you can use the DEVPATH environment variable, or make use of the Public Assembly folder functionality
To use the DEVPATH environment variable, you would simply add the folder that contains the assembly (e.g. "c:\work\samples\assemblies") to the list of directories. Then, you will need to specify the element in the machine configuration file:
This will instruct the CLR to locate the assemblies based on the DEVPATH environment variable.
A less restrictive way to specify the assembly locations is by making use of the Public Assembly Folder functionality. This is done by adding a path to the PublicAssemblies registry key.
Open up the registry, and locate the "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders" key (for VS.Net 2003, you would look under the "...\VisualStudio\7.1\AssembliesFolders" key). You will see a subkey named "PublicAssemblies". This is where you will want to add additional keys to create your own Public folders. To add your own folder, create a key (e.g. MyAssemblies), and set the default value to be the desired path (e.g. "c:\work\samples\assemblies").
The option you choose will simply depend on your needs.
|
47.23 How can I get the default namespace of a C# project in VS.NET?
|
|
To get the default namespace of a C# project developed using VS.NET, you need to right-click on the project in the Solution Explorer and then choose Properties->Common Properties->General->Default Namespace
|
|