Windows Forms FAQ

George Shepherd's Windows Forms FAQ

Welcome to the Windows Forms FAQ.  Questions and answers in this FAQ have been collected from newsgroup posts, various mailing lists and the employees of Syncfusion.  I have tried to mention the source wherever possible.  If you think that you should be credited for an answer, please send me a note and I will be happy to add a credit mention.

If you have a FAQ, or any other good Windows Forms resource, please email me.  The Windows Forms FAQ forum is available if you need more information.

You can also access the ASP.NET FAQ Home WPF FAQ Home

Search Help
 

Windows Forms

  • Deployment
  • Controls
  • Books
  • Data Binding
  • Datagrid
  • Docking
  • Keyboard Handling
  • Layout
  • Licensing
  • Menus
  • Mouse Handling
  • from MFC
  • from VB6
  • Patterns
  • Printing
  • Resources
  • Scrolling
  • Tips
  • Common Dialogs
  • ListBox
  • ComboBox
  • RichTextBox
  • ListView
  • TreeView
  • Button
  • TabControl
  • TextBox
  • Cursors
  • WebBrowser
  • ProgressBars
  • PictureBox
  • Form
  • CheckedListBox
  • MDI
  • In IE
  • Smart Client
  • GDI+

  • Bitmaps & Images
  • Fonts
  • Colors
  • Books
  • Brushes
  • Drawing Tips
  • Hit Testing
  • from GDI
  • Paths & Regions
  • Pens
  • Rendering Primitives
  • Interoperability

  • Win32
  • Books
  • COM
  • Tools

  • Other FAQs
  • Resource Editor
  • Metadata Viewers
  • Design Time

  • Serialization
  • Custom Designers
  • Tips
  • Type Editors
  • UI
  • VS.NET

  • Tips
  • Debugging
  • Macros
  • Framework Tips

  • General
  • Events
  • Performance
  • CGI
  • General IO
  • Strings
  • 1.1
  • Threading
  • XML
  • Math
  • Network

  • Tips
  • 1. Windows Forms Deployment

      

       1.1How can I run an EXE from within my application?
       1.2What are the common issues in redirecting assemblies using the publisher policy files?
       1.3How can I find all programs with a GUI (not just arbitrary windows) that are running on my local machine?
       1.4How can I get a list of all processes running on my system?
       1.5How can I make sure there is only one instance of my application running?
       1.6How do I determine which operating system is running?
       1.7How can I get all IP addresses for my local machine?
       1.8My user does not have .NET installed. Will he be able to run my Windows Forms application?
       1.9How do I get the path to my running EXE?
       1.10How can I programmatically obtain the name of the assembly that the code is executing in ?
       1.11How can I see what is installed in the Global Assembly on a machine?
       1.12How do I set the company name that is returned by System.Windows.Forms.Application.CompanyName?

    2. Windows Forms Controls

      

       2.1How do you prevent serialization of certain child controls in your Composite Control?
       2.2How do I get hold of the currently focused Control?
       2.3Why do calling Focus() on a control not set focus on it?
       2.4How do I listen to windows messages in my Control?
       2.5How do I programatically change the color of a control?
       2.6How can I change the Border color of my control?
       2.7Why should I provide a Non-Client border to my Control derived class?
       2.8How do I provide a 2 pixel 3d border in the Non-Client area of my Control derived class?
       2.9How do I provide a 1 pixel border in the NonClient area of my Control?
       2.10How do I invalidate a control including it's NonClient area?
       2.11How can I implement a scrollable picture box?
       2.12How can I put Controls, a ProgressBar for example, into a StatusBar?
       2.13How do I implement an ownerdrawn statusbar so I can put a progressbar in it?
       2.14How would I change the icon that appears on the toolbox for a custom control?
       2.15A control's Validating event is hit even when the user clicks on the Close box. How can I avoid this behavior?
       2.16I would like to prevent validation in my textbox when the user clicks on my Cancel button, how do I do this?
       2.17Why does adding images to an ImageList in the designer cause them to lose their alpha channel?
       2.18Why do the XP Icons when drawn using Graphics.DrawImage not draw transparently?
       2.19Why do the XP Icons that have alpha channel not draw properly when associated with controls like ListView?
       2.20How do I prevent resizing of my Controls by the user, via Docking or anchoring or manual resizing during design-time?
       2.21What control do I use to insert Separator lines between Controls in my Dialog?
       2.22How can I make my controls transparent so the form's background image can show through them?
       2.23How do I create an editable listbox with an in-place TextBox and Button?
       2.24How do I determine the width/height of the Non-Client area (like the border in a textbox) of a Control?
       2.25How can I programmatically manipulate Anchor styles?
       2.26What is the best method to override in custom Controls to perform custom initialization during runtime?
       2.27I set a Control's Visible property to true and in the immediate next statement, it returns false. Why doesn't setting the Visible property 'take'?
       2.28I'm trying to make the background of my linklabel transparent so a picturebox will show through it. However, if I set the link label's BackColor property to Transparent the label still has a white background. Why?
       2.29How do I dynamically load a control from a DLL?
       2.30What is the (DynamicProperties) item listed on a control's property page in VS.NET?
       2.31How can I make a Panel or Label semi-transparent on a Windows Form?
       2.32How can I add a control to a Window Form at runtime?
       2.33How do I make the arrow keys be accepted by a control (such as a button) and not handled automatically by the framework's focus management?
       2.34In the property browser for a custom control, how do I disable a property initially, but enable it later based on some other property changing?
       2.35How can I have the control designer create the custom editor by calling the constructor with the additional parameters rather than the default constructor?
       2.36How do I listen to the screen resolution change in my control?
       2.37How do I determine which button in a Toolbar is clicked?

    3. Windows Forms Books

      

       3.1Windows Forms Programming in C#?
       3.2Programming Microsoft Windows with C#?
       3.3C# Design Patterns?

    4. Windows Forms Data Binding

      

       4.1Does MySql work with ADO.NET?
       4.2Where can I find a discussion of databinding and windows forms?
       4.3How do I test for a null value in DataView.RowFilter?
       4.4I don't have SQL Server. Is there any way to run the samples that need SQL server?
       4.5How can I programmatically move through a dataset that has bound controls?
       4.6When I try to update a dataset I get an error that the system is unable to find "Table"?
       4.7How can I bind an ArrayList to a DataGrid?
       4.8When I try to bind two comboboxes to the same datatable, both boxes show the same values, changing one changes the other. How do I make them have distinct values?
       4.9How do I bind a mdb file to a datagrid?
       4.10How do I bind a listbox or a combobox to a MDB file?
       4.11In my databound ComboBox, how do I set the SelectedItem property?
       4.12Are server side cursors supported in .NET?
       4.13I am populating a ListBox by binding it to an ArrayList. The initial display is OK, but later changes to my ArrayList are not shown in my ListBox. How can I get the changes to show properly?
       4.14My SQL server runs on a non-default port. How can I specify this port in my connection string?
       4.15What is a DataSet?
       4.16How do I add updating support to a dataset?
       4.17How can I import a CSV file into a DataTable?
       4.18How can I prevent a ComboBox and DataGrid bound to the same DataTable from sharing the same current position?
       4.19I get a 'This would cause two bindings in the collection to bind to the same property' error message. What might cause this?
       4.20How do I bind a TextBox to a nested relation?
       4.21How do I call a SQL stored procedure?
       4.22I programatically change a bound TextBox value, but the value does not get pushed back into the bound datasource. How can I make sure the DataSource is updated?
       4.23Why does the CheckedListBox lose checked state when placed in a tab page that gets hidden and shown?

    5. Windows Forms Datagrid

      

       5.1How can I programatically set the rowheight of a row in my DataGrid?
       5.2How can I autosize the rowheights in my datagrid?
       5.3How do I prevent sorting a single column in my DataGrid?
       5.4How do I programmatically select a row in DataGrid?
       5.5How can I translate a point to a cell in the datagrid?
       5.6I lost the settings in my DataGrid set during design-time?
       5.7How do I prevent a click into the grid highlighting a cell?
       5.8How do I prevent the datagrid from displaying its append row (the row at the end with an asterisk)?
       5.9How can I put a combobox in a column of a datagrid?
       5.10How can I catch a double-click into a column header cell?
       5.11How can I select the entire row when the user clicks on a cell in the row?
       5.12How can I get text from a column header in a MouseUp event?
       5.13How do I hide a column?
       5.14How do I color a individual cell depending upon its value or some external method?
       5.15How can I put a checkbox in a column of my DataGrid?
       5.16How can I restrict the keystrokes that will be accepted in a column of my datagrid?
       5.17How do I make a field auto increment as new rows are added to my datagrid?
       5.18How can I prevent a particular cell from being editable?
       5.19How do I move columns in a datagrid?
       5.20How can I do cell by cell validation in a datagrid?
       5.21How do I programmatically determine the selected rows in a datagrid?
       5.22How can I move rows by dragging the row header cell?
       5.23I want to do custom handling of special keys such as the Tab key or F2 in the TextBox of a column in the DataGrid. How do I subclass this TextBox to get at it virtual members?
       5.24How can I have a column of icons in my datagrid?
       5.25How can I tell if the current row has changed and whether I am on the AddNew row or not?
       5.26How do I hide the gridlines or set them to a particular color?
       5.27How can I get the selected text in an active gridcell?
       5.28How do I determine whether a checkbox in my datagrid is checked or not?
       5.29How can I bind the datagrid to a datasource without using any wizards?
       5.30How can I bind two datagrids in a Master-Detail relationship?
       5.31How do I get the row or column that has been clicked on?
       5.32How do I add an unbound column to my bound datagrid?
       5.33How do I get the row and column of the current cell in my datagrid?
       5.34How can I prevent the Enter key from moving to the next cell when the user is actively editing the cell and presses Enter?
       5.35How do I set the width of a column in my DataGrid?
       5.36How can I implement OLE Drag & Drop between a DataGrid and another OLE DnD object that supports the Text format?
       5.37How can I make my DataGrid support a single select mode, and not the default multiselect mode?
       5.38How can I get celltips or tooltips to vary from cell to cell in my DataGrid?
       5.39How can I get notification of the changing of a value in a column of comboboxes within my datagrid?
       5.40How can I make the datagrid have no currentcell?
       5.41How can I make my grid never have an active edit cell and always select whole rows (as in a browser-type grid)?
       5.42I have hidden (column width = 0) columns on the right side of my datagrid, but tabbing does not work properly. How can I get tabbing to work?
       5.43How can I get the number of rows in my DataGrid?
       5.44How do I format a date column in a datagrid?
       5.45How can I change the width of the row headers or hide them?
       5.46How do I catch a doubleclick in my datagrid?
       5.47How can I make my last column wide enough to exactly occupy all the client area of the datagrid?
       5.48How can I prevent my user from sizing columns in my datagrid?
       5.49How can I catch the bool values changing in a DataGridBoolColumn?
       5.50When I click on a row header, the row is selected and no cell is active. How can I do this programmatically?
       5.51How can I force the vertical scrollbar in my DataGrid to always be visible?
       5.52How can I autosize a column in my datagrid?
       5.53How can I get rid of the error icon that appears when there is an editing error?
       5.54How do I find the top-left visible cell in a datagrid?
       5.55I want to display negative values with a CR suffix, instead of a minus sign. How can I perform custom formatting on the cells in my datagrid?
       5.56I want to do sort of a database join of two tables. How can I use data from two DataTables in a single DataGrid?
       5.57How do I display a column of buttons such as pushbuttons or combobox buttons?
       5.58How can I put up a confirmation question when the user tries to delete a row in the datagrid by clicking on the row header and pressing the Delete key?
       5.59How can I enable column selections in my datagrid?
       5.60How do I programmatically scroll the datagrid to a particular row?
       5.61How can I place text in the rowheader column of my datagrid?
       5.62How do I set default values for new rows in my datagrid?
       5.63How do I iterate through all the rows and columns in my datagrid?
       5.64How can I specially color only the currentcell of my readonly datagrid?
       5.65How can I make the Enter Key behave like the Tab Key and move to the next cell?
       5.66How do I use the DataColumn.Expression property to add a computed/combined column to my datagrid?
       5.67How can I change the font used in a grid cell on a cell by cell or row by row basis?
       5.68How can I use a mouse to select cell ranges in my datagrid?
       5.69How can I control the cursor over my DataGrid?
       5.70How can I programatically add and remove columns in my DataGrid without modifying the DataTable datasource?
       5.71How can I have a column of bitmaps in a DataGrid?
       5.72How can I add my custom columnstyles to the designer so I can use them in my DataGrid at design time?
       5.73After scrolling with the mouse wheel on a selected row in a DataGrid I cannot get it back into view. Is there a work around?
       5.74How can I make the DataGrid column be blank and not display (null) as the default value?
       5.75How can I add a DateTimePicker column style to the DataGrid?
       5.76How do I determine the DataGridTableStyle MappingName that should used for a DataGrid to make sure the grid uses my tablestyle?
       5.77I have a derived DataGridColumnStyle. From within my Paint override, how can I get at other values in the DataGrid?
       5.78How do I retrieve the current row from a DataTable bound to a DataGrid after the grid has been sorted?
       5.79How can I catch when the user clicks off the grid, say to close the form?
       5.80How can I get a CheckBox column in a DataGrid to react to the first click?
       5.81How can I use events to restrict key input to grid cells?
       5.82How can I format columns in my DataGrid without explicitly adding DataGridColumnStyles?
       5.83How can I auto-adjust keyboard input? For example, make typing 12312002 be taken as a valid date, 12/31/2002.
       5.84Can I display the rows in my datagrid in a free-form layout using textboxes on a panel?
       5.85How can I tell whether a scrollbar is visible in my DataGrid is visible?
       5.86How do I autosize the columns in my DataGrid so they always fill the the grid's client area?
       5.87How can I prevent all the cells in my DataGrid from being edited without deriving GridColumnStyle?
       5.88How can I prevent the plus-minus icon that appears next to the row header when I have a datagrid displayed bound to a datasource that has a relation defined?
       5.89How can I display master-details-details in three separate grids?
       5.90In my datagrid, if I press tab to enter a column using a derived columnstyle, the column does not receive focus. Why?
       5.91How can I detect when a cell starts being edited, not when it becomes current?

    6. Windows Forms Docking

      

       6.1How can I make a control occupy all the client area of a form?
       6.2How can I make my control automatically grow when the parent form is sized?
       6.3What is the difference between a form's Anchor property and a form's Dock property?

    7. Windows Forms Keyboard Handling

      

       7.1How can I prevent a control from getting a particular keystroke?
       7.2How can I tell if an ALT, Shift or CTL key is pressed without catching an event?
       7.3How do I check the state of the virtual keys, Caps lock for example?
       7.4How can I simulate keyboard input in my application?
       7.5How can I catch keyboard messages on a application-wide basis?
       7.6How can I listen for certain keys at the Form level irrespective of which Control has the focus?

    8. Windows Forms Layout

      

       8.1How can I programmatically set the initial position of a Form so that it is displayed properly after calling ShowDialog()?
       8.2How do I create a custom layout engine?
       8.3I 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?

    9. Windows Forms Licensing

      

       9.1How does .NET support licensing?
       9.2How do I find the file version of an EXE?

    10. Windows Forms Menus

      

       10.1How do I clone menus, insert menus, use popup menus, etc.
       10.2How do I add a context menu to a control?
       10.3When using the ContextMenu on multiple Controls, how do I know on which Control the right click was performed?
       10.4How do menu shortcuts work?
       10.5How do I make the context menu appear only when clicked at certain portions of the Control?
       10.6How do I prevent the context menu from showing up on certain keyboard keys (like Keys.Apps)?
       10.7How do I know when a menu is closed/started so that I can refresh my status bar?
       10.8Is there an Idle event which will get fired from which I could update my menu item's enabled state?
       10.9How do I cancel a context menu programatically?
       10.10Is there an easy way to create the "Window" menu that shows the list of child forms open?
       10.11How can I work around my menu shortcuts from showing up incorrectly when I use Ctrl+Number?
       10.12How do I control the position of the context menu when it is invoked via the keyboard?
       10.13How do I disable the default context menu of a textbox?
       10.14How can I make the context menu to close after a set time interval?
       10.15How can I add an icon to my menu items?
       10.16How to add a Separator in a menu?
       10.17How can I enable the mnemonics (underline) to show when my application is launched?

    11. Windows Forms Mouse Handling

      

       11.1How can I resize a borderless form with a rubber-band effect?
       11.2How do I get a mouse cursor position in my control's client coordinates?
       11.3How can I catch the mouse being over a control?
       11.4How do I set the mouse cursor position?
       11.5How can I reset the OnMouseHover timer so that it will fire again without the mouse having to leave the client area of a control?
       11.6How can I drag a window if it doesn't have a title bar or border?
       11.7How can I determine if a mouse button is pressed and moving over my form?
       11.8Why am I not receiving MouseLeave messages in a Control in my Form?

    12. Windows Forms from MFC

      

       12.1As a VC++ programmer, what should I look out for when using C#?
       12.2Where can I find a succinct discussion of Windows Forms?
       12.3When I add a destructor to a class, why isn't it hit when my instantiated object goes out of scope?
       12.4How do I convert a string to a double or int? What plays the role of atof and atoi in C#?
       12.5What class or method do I use to retrieve system metrics like the width of a scrollbar?
       12.6In C++, the code "MyClass ht;" creates an object ht on the stack frame. But in C#, this same code compiles, but gives a runtime error. Why?
       12.7I need to encode the LParam argument of a mouse message. How do I do MakeLong , HiWord and LoWord type conversions?
       12.8How do you clear the contents of a list box?

    13. Windows Forms from VB6

      

       13.1How do I draw a line in VB7 as there is no Line command as there was in VB6?
       13.2What is the replacement for VB6's SendKeys statement?
       13.3In VB6, I used control arrays to have a common handler handle events from several controls. How can I do this in Windows Forms?
       13.4What is different about working with forms in VB.NET? For example, how can I reference one form from another form?

    14. Windows Forms Patterns

      

       14.1How to display a status dialog in a background thread during a long operation and allow the user to cancel?
       14.2How can I throw my events asynchronously?
       14.3Why are the Tooltips not being shown on a NumericUpDown control?
       14.4How to get the hyperlink and the hyperlink text dragged from IE in my Control's drag-drop event?

    15. Windows Forms Printing

      

       15.1How do I print a form?
       15.2How do I display the PrintPreview maximized and control its zooming?

    16. Windows Forms Resources

      

       16.1What are the steps to compiling and using multiple language resources?
       16.2How do I load a BMP file that has been added to my solution as an embedded resource?
       16.3Why doesn't a Form containing an ImageList not open in WinRes?
       16.4Where can I find information on globalization?
       16.5How do I read embedded resources?
       16.6How can I check if a certain resource is included in a assembly?
       16.7How can I save a temporary disk file from an embedded string resource?
       16.8How do I embed a manifest file into my exe?
       16.9How do I read or write resources from code?
       16.10How do I create resources to make culture-aware programs without re-compiling code?
       16.11How do I programmatically use resources to make my programs culturally aware?
       16.12How can I dynamically load a resource file?

    17. Windows Forms Scrolling

      

       17.1How do I add support for custom scrolling to my own user control?
       17.2Are there any events that get fired when the user scrolls?

    18. Windows Forms Tips

      

       18.1How do I write a screen saver?
       18.2How do I determine the screen resolution?
       18.3How do I determine the working area of a screen without the systemtray area?
       18.4How do I position my form to the bottom right of the screen when it opens up the first time above the system tray?
       18.5How do I change my screen's resolution programatically?
       18.6How do I determine the current date/time?
       18.7How do I determine the time taken for a long operation that I perform?
       18.8What are the implications of using the Timer class in System.Timers as opposed to the Timer class in System.Windows.Forms?
       18.9Is there an easy way to Create/Delete/Move files in the Windows File System?
       18.10How can I use XP Themes with Windows Forms using the .NET FrameWork 1.1?
       18.11How can I use XP Themes with Windows Forms using the .NET FrameWork 1.0?
       18.12How do I use Windows Forms Controls in Internet Explorer?
       18.13Why would I be getting a NullReferenceException in Windows Forms with no application code in the call stack?
       18.14How do I change a directory name?
       18.15How do I beep the computer's speaker in a Windows Form application?
       18.16How do I use the system clipboard?
       18.17How do I add an application to the Window's Tray?
       18.18When I try to catch the Resize event to handle sizing of MDI children, the event is called even before my form's constructor can create the children throwing an exception. How do I avoid this?
       18.19How can I tell if the user has changed some system preference such as the locale or display settings?
       18.20Its possible that some of my class members will not be initialized when one of my virtual members gets called even before the constructor. How can I avoid this?
       18.21In a Hashtable, why doesn't setting the value for an existing key to be null remove the key from the Hashtable?
       18.22What mechanisms does .Net provide to convert one type to another?
       18.23How can I display HTML in a Form?
       18.24How can I shut down/restart the OS from my Windows Forms Application?
       18.25How can I add a custom verb to the file and folder context menus in the explorer shell?
       18.26How do I create windows shortcuts on my desktop programatically in .Net?
       18.27I have a long loop. How can I make my application continue to process events while it is executing this loop?
       18.28The MessageBox always appears in the center of the screen. How can I change it's location?
       18.29If I have a button with &Next as the text, the N-accelerator key does not appear until I press Alt. Why?
       18.30Can working on WinForms and .Net in general get exciting?
       18.31I get a message 'DragDrop registration' failed. Why?
       18.32What is the purpose of the [STA Thread] attribute for the Main method of a C# program?
       18.33I add my application to the Window's Tray, set ShowInTaskBar to false, but the program still appears in the Alt+Tab list.

    19. Windows Forms Common Dialogs

      

       19.1How do I use the ColorDialog to pick a color?
       19.2How do I use the FontDialog class to set a control's font?
       19.3How do I implement a Folder Browser class?
       19.4How can I get just the name of a file from the complete path string?
       19.5How can I get just the extension of a file from the complete path string?
       19.6How do I use the OpenFileDialog?
       19.7How do I specify the path for the FolderBrowser instance when it opens the first time?

    20. Windows Forms ListBox

      

       20.1How do I add and delete items from a ListBox?
       20.2How do I implement Drag and Drop support between ListBoxes?
       20.3How can I drag file names from Windows Explorer and drop them into a listbox?
       20.4How do I implement an ownerdrawn listbox?
       20.5How can I set the width of a listbox to fit the text?

    21. Windows Forms ComboBox

      

       21.1How do I bind the values of an enum to a ComboBox?
       21.2How can I programmatically prevent the combobox from dropping?
       21.3How can I turn off editing in the textbox portion of a ComboBox, restricting the user to selecting only those options in the drop list?
       21.4How can I adjust the height of the of my ComboBox dropdown?
       21.5How do I implement an owner drawn combobox?
       21.6How do I add a ComboBox button to a toolbar?
       21.7How do I set the width of a combobox to fit the entries in its list?
       21.8How can I programmatically create a new list for my ComboBox dropdown?