Here are some frequently asked questions about Windows Forms and their answers.
Browse by Tags
All Tags » General (RSS)
-
|
See the DesignModeDialog sample. This sample is a component, much like the common dialogs, that has a ShowDialog method. When invoked, ShowDialog takes a snapshot of the parent form, clones all the controls and creates a new designer form with the controls...
|
-
|
The new DesignSurface class drastically simplifies the code necessary to host a designer in your own application. The following lines of code show the most simple designer that can be created. Note: to run this code, a reference to System.Design.dll will...
|
-
|
No. In fact, there are no updateable cursors anywhere in .NET. All of the data access in .NET is based on this disconnected data model. In .NET you push data into a DataSet. The DataSet keeps track of the changes that are made to the data. Then the DataSet...
|
-
|
You call stored procedures in basically the same manner as executing other SQL commands. When creating the SqlCommand, set the query string to be the name of the stored procedure, and then set the CommandType to be CommandType.StoredProcedure. SqlCommand...
|
-
|
You can use MSDE, a SQL server compatible database engine that MS makes available for free. You can download and install MSDE from a whole variety of sources. Here is one source . MSDE also comes with Visual Studio .NET and the .NET Framework SDK. It...
|
-
|
The data source component of your connection string should contain the port right after the IP address (or name). It should be separated from the IP by a comma. data source=192.168.123.1, port_number; Take a look at the connectionstrings.com website for...
|
-
|
Yes, MySQL can be used with ADO.NET. For more information, see the article Exploring MySQL in the Microsoft .NET Environment at mysql.com. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
Here's a sample that loads a comma separated values (CSV) file into a DataSet. using System; using System.Data; using System.Data.OleDb; using System.IO; public class Csv { public static DataSet GetDataSet( string filename ) { string connString =...
|
-
|
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: <Reference Name = "MyAssembly...
|
-
|
Use the following syntax: devenv.exe {Solution Name} /command {Macro name} Example: devenv.exe "myproject.sln" /command "Macros.Syncfusion.Utils.FormatProjectAndClose" Visual Studio will start up, load the solution, run the macro and...
|
-
|
Type devenv.exe /? to get a full list of options. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
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. >>>...
|
-
|
Mastering Visual Studio .NET Ian Griffiths, Jon Flanders & Chris Sells, 2003, O'Reilly. ISBN: 0-596-00360-9. Every page of this book is packed with useful information on getting the most out of Visual Studio .NET 2003. Written by experienced developers...
|
-
|
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...
|
-
|
Yes, in Code View you can select and drag code to a Tab (General Tab) in the ToolBox and then you can drag the code from the Tab to the desired location. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
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...
|
-
|
In the project's General Properities, specify Class2 as the "Startup Object". The dropdown contains all classes with a Main method specified. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
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". Ryan LaNeve
|
-
|
In the ClassView window, expand the base class under your derived class. Then right-click the desired method, and select Add. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
VB.NET can perform decent auto-formatting for code. Here is a macro that can call this Auto Formatting command for all the files in a project. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
How do I get the default namespace of a C# project in Visual Studio? 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 |...
|
-
|
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. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
You cannot move it via drag-and-drop. But you change its position or dock order by selecting the "Bring To Front" or "Send To Back" verbs in its context menu. "Bring To Front" will move it to the top of the children list...
|
-
|
1) Cannot override methods like WndProc, ProcessCmdKey, etc in a derived Control. The class ref for a particular property will let you know about the permission requirements. 2) Cannot call Control.Parent. 3) Cannot call Control.Focus() method. Contributed...
|
-
|
The issues listed here are pertaining to code running in IE in the default Internet Zone. 1) You cannot view Controls inside IE using the 1.0 framework (when running with the permissions in the default Internet Zone). This is however possible with the...
|
-
|
You cannot use a WinForms Control directly in IE, you will have to instead derive from it and then use the derived instance from a custom assembly. These articles introduce the topic: Host Secure, Lightweight Client-Side Controls in Microsoft Internet...
|
-
|
You can do this in different ways explained below. In all the cases the bitmap or icon should follow these rules: The bitmap or icon dimension should be 16x16 with 16 colors. The bottom-left pixel color will be assumed to be the transparent color. Technique...
|
-
|
Provide ShouldSerialize and Reset Methods along with your property. Example: private bool ShouldSerializeFont() { return this.bFontSet; } private void ResetFont() { this.localFont = null; } Contributed from George Shepherd's Windows Forms FAQ
|
-
|
For example, if you have a custom type: public class CustomSize { public int Width { get { /*...*/ } set { /*...*/ } } public int Height { get { /*...*/ } set { /*...*/ } } } And you want to the Width and Height properties to appear in that order, then...
|
-
|
If you have a custom data type like this: public class CustomSize { public int Width { get { /*...*/ } set { /*...*/ } } public int Height { get { /*...*/ } set { /*...*/ } } } Then write this: public class CustomSizeConverter : ExpandableObjectConverter...
|
-
|
Check the Form.DesignMode property. But note that when in Visual Inheritance mode (designing a derived form), your control's DesignMode property will be true when the base form's constructor gets executed in the design-time. To workaround this...
|
-
|
Type converters let you convert one type to another type. Each type that you declare can optionally have a TypeConverter associated with it using the TypeConverterAttribute. If you do not specify one the class will inherit a TypeConverter from its base...
|
-
|
Provide the followoing attributes to your Property: [ Browsable( false ), EditorBrowsable( EditorBrowsableState.Always ) ] public bool CustomProperty{ get{} set{} } Contributed from George Shepherd's Windows Forms FAQ
|
-
|
Set the Browsable attribute on the new property that hides the existing one. Here is a code snippet that hides the WordWrap property in the derived TextBox. [C#] public class CustomTextBox : TextBox { [ Browsable( false ) ] public new bool WordWrap {...
|
-
|
Use the DesignTimeVisible attribute set to false. [ DesignTimeVisible( false ) ] You can still provide a TypeConverter to make it participate in designer Serialization. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
Use the ToolboxItem attribute set to false on your component or control class. This will prevent it from appearing in the Toolbox. [ ToolboxItem( false ) ] public class MyComponent : Component{..} Contributed from George Shepherd's Windows Forms FAQ
|
-
|
You can do this using the bitwise operators. In C# the bitwise operators are & , | and ^ ; in VB they are And (or &), Or and Xor (or ^). Here is code that will toggle whether label1 is anchored on the left. [C#] using System.Windows.Forms; private...
|
-
|
private void Form1_Load( object sender, EventArgs e ) { Bitmap newBmp = new Bitmap(100, 100); Graphics g = Graphics.FromImage(newBmp); g.FillRectangle(new SolidBrush(Color.Red), 0, 0, 33, 100); g.FillRectangle(new SolidBrush(Color.White), 34, 0, 33, 100...
|
-
|
When you call the Focus method on a control, the control must be visible; otherwise focus will not be set. For example, if you call Focus on a control in Form_Load then focus will not be set. Instead consider setting the control's TabIndex property...
|
-
|
The .NET Framework Class Library does not provide a method to determine which control has keyboard focus, but you can invoke a Win32 API function to do this. Here's a custom form class that adds a GetFocusControl method to retrieve the .NET control...
|
-
|
Override the control's WndProc method. This sample shows a custom control that is a ComboBox that discards the WM_KEYUP message. [C#] using System.Windows.Forms; public class ComboBoxWithNoKeyUp : ComboBox { private const int WM_KEYUP = 0x101; protected...
|
-
|
Where do I find an ImageMap-style control? There isn't an ImageMap-style control in the .NET Framework Class Library. However, the article C# Windows Forms ImageMap Control by Ryan LaNeve, September 2002, on The Code Project provides a control with...
|
-
|
See Developing a Simple Windows Forms Control from .NET Framework Developer's Guide for a walk-through about creating Windows Forms custom controls. Tom Krueger, Microsoft
|
-
|
button1.Cursor = new Cursor( @"C:\winnt\cursors\hnodrop.cur" ); Contributed from George Shepherd's Windows Forms FAQ
|
-
|
Set the Form's StartPosition property to CenterScreen . Contributed from George Shepherd's Windows Forms FAQ
|
-
|
The following code snippet (posted in the Windows Forms FAQ forums) shows how you can prevent a user from moving a form at run time: protected override void WndProc( ref Message m ) { const int WM_NCLBUTTONDOWN = 161; const int WM_SYSCOMMAND = 274; const...
|
-
|
Do as follows in your Form's constructor after setting the StartPosition to Manual: SetBounds( Screen.GetWorkingArea( this ).Width - Width, Screen.GetWorkingArea( this ).Height - Height, Width, Height ); Contributed from George Shepherd's Windows...
|
-
|
Set the System.Windows.Forms.Form.WindowState property to FormWindowState.Maximized or FormWindowState.Minimized. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
In addition to setting the Location property of the form, make sure you also set the StartPosition property of the form to FormStartPosition.Manual. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
I have a form with several controls on it. As I size the form, the controls are being resized continuously. Is it possible to postpone changing the controls position until the resizing is complete? The idea is to do the painting on Idle. This means you...
|
-
|
There is not currently a way to do this built into the framework, but WM_SETREDRAW will do what you're looking for. It can't be called recursively, so here's code for a property you can add to your form to handle it. A VB sample is also available...
|
-
|
Try this code: Dim g as Graphics g = frmMain.CreateGraphics() g.DrawLine(Pens.Black, new Point(0,0), _ new Point(frmMain.ClientRectangle.Width), frmMain.ClientRectangle.Height) g.Dispose() This should draw a line from the upper left to the bottom right...
|
-
|
Check out Painting techniques using Windows Forms for the Microsoft .NET Framework by Fred Balsiger on WindowsForms.net . It is a good basic discussion of how to get the best performance from Windows Forms drawing. His hints include leveraging the power...
|
-
|
Handle the form's Closing event. private void Form1_Closing( object sender, CancelEventArgs e ) { if ( NotOkToClose() ) e.Cancel = true; //don't close } Contributed from George Shepherd's Windows Forms FAQ
|
-
|
One way to do this is to override the form's WndProc method and check for WM_SYSCOMMAND and SC_CLOSE. Looking for WM_CLOSE in the override is not sufficient as WM_CLOSE is seen in both cases. A sample is available for download ( C# , VB ). public...
|
-
|
Your main form is an object of type System.Windows.Forms.Form. Use its Close method to exit the application. If you wish to Exit from a Form's constructor, this will not work. A workaround is to set a boolean flag that you can later check in the Form's...
|
-
|
The controls that I've try to add to my form at runtime don't show up. What's wrong? Make sure you implemented and executed code similar to the InitializeComponent method that VS adds to your Windows Forms project for controls added during...
|
-
|
You use System.Reflection to dynamically load a control. This sample loads the SpecControls.ColorControl from SpecControls.dll. [C#] using System.Reflection; Assembly assembly = Assembly.LoadFrom( "SpecControls.dll" ); Type t = assembly.GetType...
|
-
|
There are three steps to adding a control at runtime. Create the control. Set control properties. Add the control to the Form's Controls collection. In general, if you need help on exactly what code you need to add, just look at the code generated...
|
-
|
You can use the System.Reflection.Assembly.CreateInstance method to create a form from its name. Below is a code snippet. The name in the textbox has to be the full name including its namespace. So if there is a class named Form2 in namespace MyCompanyName...
|
-
|
In VB6, I used control arrays to have a common handler handle events from several controls. How can I do this in Windows Forms? You can specify events from different controls be handled by a single method. When you define your handler, you can list several...
|
-
|
You can do this by setting the child form's TopMost to False and setting its Owner property to the Main Form. Form1 f = new Form1(); f.TopMost = false; f.Owner = this; f.Show(); Contributed from George Shepherd's Windows Forms FAQ
|
-
|
For example the Find dialog in Visual Studio. Make your main form the "Owner" of the form in question. Refer to Form.Owner in class reference for more information. findReplaceDialog.Owner = this; // where 'this' is the main form findReplaceDialog...
|
-
|
To set or control the location of the form using desktop coordinates, you can use the SetDeskTopLocation property. You can do this by setting the child form's TopMost to False and setting its Owner property to the Main Form. this.SetDesktopLocation...
|
-
|
You need to set the form's ShowInTaskbar property to False to prevent it from being displayed in the Windows taskbar. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
See the Control.Handle property which returns the HWND. Be careful if you pass this handle to some Win32 API as Windows Forms controls do their own handle management so they may recreate the handle which would leave this HWND dangling. Shawn Burke, Microsoft
|
-
|
To do this, you can use use iterop to access the GetSystemMenu and AppendMenu Win32 APIs. You also need to override the form's WndProc method to catch and act on the menu message. This idea was posted in the Microsoft newsgroups by Lion Shi. Here...
|
-
|
Normally when you make a Form visible by setting the Visible property to true, it will show the form and set the focus too. In some cases however, you do not want it to take focus until the user clicks on it. To get this behavior, do the following utility...
|
-
|
One way to do this is to make the TextBox either a public property or a public field. Then you will be able to access it through the instance of its parent form. So, if TextBox1 is a public member of FormA and myFormA is an instance of FormA, then you...
|
-
|
You can P/Invoke the BitBlt function from gdi32.dll to handle this problem. using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Windows.Forms; class CustomForm : Form { [ DllImport( "gdi32...
|
-
|
Check out Working with Multiple Forms in Visual Basic .NET: Upgrading to .NET by Duncan Mackenzie in the MSDN Library. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
Add public properties to your form. Then these properties can be accessed by any object that creates an instance of your form. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
Set the form's AcceptButton property. You can do this either through the designer, or through code such as form1.AcceptButton = button1; Contributed from George Shepherd's Windows Forms FAQ
|
-
|
I am using a Windows Forms control in an Internet Explorer (IE) page. When IE downloads the control, it is distinguishing between upper- and lower-case letters in the web site URL. When I use the same site with different casing - such as http://111.1...
|
-
|
I would like to use the Region property to create a form with hidden non-client areas (not exactly a rectangle), but I can't determine the client area rectangle position in non-client coordinates. The code I usually use to get this offset is: Point...
|
-
|
For example, let's say you want to define an add-in model, or provide some base forms so my customers can extend to customize the look-and-feel of my app by just dropping in additional assemblies. How can you achieve this with "ClickOnce"...
|
-
-
|
Yes, although the top level exe must be built with Whidbey.
|
-
|
No. Addins generally require special registration and integrated activation within the hosting application. The “ClickOnce” installer does not support machine level changes such as this.
|
-
|
Yes. When a “ClickOnce” application is activated via a URL, the application can obtain the QueryString of the URL used to activate the application.
|
-
|
That is the actual “ClickOnce” engine that is handling the update checks & downloads. It is launched when a “ClickOnce” application needs updated. It shuts itself down if not used for 15 minutes.
|
-
|
You can use “ClickOnce” to deploy .NET Framework based applications built with any language, including C++.
|
-
|
You can, but wouldn’t you rather have a rich Windows Forms based UI? Also, console apps that require a command-line interface are beyond the scope of “ClickOnce” deployment in the .NET Framework 2.0. The primary reasons for this is that the install path...
|
-
|
Yes, “ClickOnce” applications that choose to be installed get an entry added in the Start Menu that can be used to launch the application when disconnected from the network. Note however, it’s still up to the application to make sure it can work offline...
|
-
|
No. “ClickOnce” only deals with deploying applications. The Windows Forms control in the browser technology is effectively unchanged.
|
-
|
For a number of security related considerations, “ClickOnce” requires that the main entry point assembly (the .exe file) be compiled with the 2.0 version of the .NET Framework. Other assemblies in the application can still be compiled with previous versions...
|
-
|
Most applications won’t have to know anything about “ClickOnce” to be deployed with “ClickOnce”. In most cases, using “ClickOnce” is as simple as creating the necessary manifest files & placing the application on a server… for which we provide tools...
|
-
|
“ClickOnce” is the successor to the V1 No-Touch deployment technology. “ClickOnce” builds upon V1 & addresses the key limitations & issues with the V1 technology. The V1 No-Touch deployment technology is still supported in V2 of the .NET Framework...
|
-
|
No. While Visual Studio makes deploying “ClickOnce” applications simple, there are several other ways to deploy “ClickOnce” applications. The .NET Framework SDK includes a tool named MAGE(Manifest Editor & Generator) that provides both a UI (mageui...
|
-
|
Yes and your app will still be auto-updateable via a network server you configured into the “ClickOnce” deployment when you created the CDs. This is a great solution for large applications or for applications that have pre-requisites installed with the...
|
-
|
If you need to do extensive install time custom actions, MSI would probably be a beter choice than ClickOnce.
|
-
|
Yes. One option is to use the VS Bootstrapper. The Bootstrapper will bundle all of your apps necessary pre-requisite’s, install them when run & then start the “ClickOnce” application install. The Bootstrapper must be run the 1st time each user uses...
|
-
-
|
No. All “ClickOnce” applications are installed per-user. Each user is totally isolated from one another and must install their own copy. If your application needs to be installed per-machine, you should use MSI.
|
-
|
No. All “ClickOnce” applications are installed per-user.
|
-
|
“ClickOnce” & MSI are separate but, complimentary application installer technologies. “ClickOnce” is targeted at web based per-user deployments. This has a lot of benefits, but also means you can’t do anything & everything at install time. If...
|
-
|
"ClickOnce" can be used to deploy Windows Forms based application based on the .NET Framework. Since "ClickOnce" applications are deployed via the web, they can't be allowed to impact the client machine to a high degree. So if...
|
-
|
You can check the useragent value in your server side code or in javascript on the html page (hint… see the Publish.htm page that VS generates). The useragent string will contain the version of the .NET Framework installed on that client.
|
-
|
Really whatever way works best for you. Ex. You can have your users install the .NET Framework before using “ClickOnce” or use management tools like SMS to push out the .NET Framework across your enterprise. To help with deployment, Visual Studio provides...
|
-
|
"ClickOnce" simply uses a MIME type for activation, which is configured by the .NET Framework 2.0 setup. This allows a "ClickOnce" app to be launched by any client that uses standard Windows url activation. This includes Internet Explorer...
|
-
|
No. "ClickOnce" does not use any special protocols to transfer files. So you can use a regular file share, IIS or any web server. If you are using a web server, there are a few additional considerations. First, you may need to configure the...
|
-
|
“ClickOnce” ships in the .NET Framework redist & supports all of the same operating systems & languages as the core .NET Framework. Specifically Windows 98 & up, except for NT4.
|
-
|
You need to have the .NET Framework 2.0 installed on the client. And you need a place to publish your app to where your end users can access, such as a web server, file share, or media such as CD/DVD.
|
-
|
"ClickOnce" is the codename for a technology that ships in the .NET Framework 2.0 that allows Windows Forms based client applications to be downloaded & run over the Network. Quite simply, "ClickOnce" makes running a client application...
|