Tag: Windows Form

When you use Windows Form Listview control, you will see a strange problem: you added columns, but the columns can not showing up in the designer panel in Visual Studio.

This is from Microsoft design: they set default Listview control view property to LargeIcon, maybe they think ListView control is not like regular DataGridView control, the view should be use Icon showing style?

There was a windows form application based on .NET 4.0 and Visual Studio 2010, it was working well on a PC which we developed it. when we move its debug folder which including generated executable files, it crashed (We use Windows 7):

We expanded “Show problem details” button and saw the following information:

After we added above code, we ran our application again, and got a popup window like the following:

So then we know the missed the file is about “Microsoft.VisualBasic.PowerPacks.Vs”, it is a library file. it missed on the new PC, so we just “include” the dll file to output folder in Visual Studio 2010, then our application can work on other PCs.

After you zoom in the Microsoft Chart control. you will see X scroll bar and Y scroll bar which you can scroll the view. Also, you can see two small icons beside the scroll arrow button on scroll bars, the one is on X scroll bar, another one is on Y scroll bar. They are zoom reset buttons.

By default, the zoom reset button only reset one Axis zoom and reset the zoom level to previous scale. When you try to click them, you can see the zoom level reset step by step.

In some cases, you do not want to the zoom reset works like its default way, but you want the zoom reset just “click one time” and reset the zoom rate to its original level. and also, you might just want one zoom reset button in your UI.

About how to hidden one of the tow zoom reset buttons is simple. it is only one line of code:

If you are using Windows 7 or Windows Vista, or Windows Server 2008, you should know the UAC (User Account Control), this is one of significant differences between Windows XP and the newer Windows System.

“User Account Control (UAC) determines the privileges of a user. If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. By default, you are in the standard user role. When you attempt to perform a task that requires administrative privileges, you can dynamically elevate your role by using the Consent dialog box. The code that executes the IsInRole method does not display the Consent dialog box. The code returns false if you are in the standard user role or in the Built-in Administrators group. You can elevate your privileges before you execute the code by right-clicking the application icon and indicating that you want to run as an administrator.” – From MSDN.

When you build a Windows Form application using C#, your application will not run as administrator by default. Then if your application need more permissions just like an Administrator, you might get problem since your application can not access enough resources;

The simplest solution is disabling the UAC on your Windows system. About how to disable UAC, we will discuss in another time, this time we do not want to talk about more about this since disabling UAC for a windows system is not recommended.

The normal and the best solution is using manifest file.

Open your application in Visual Studio 2010, Right click the project and add a new item, choose “Application Manifest File”:

Now you can see a new file named “app.manifest” is added under Properties folder:

When you open app.manifest file, you can see there are some sample settings have been already there, for example:

When we use Form.DialogResult in our Windows Form project, we should know it just return a DialogResult, it does NOT call a Close method to close the form automatically. Even you click the Close icon in the top-right corner of the form to close the form.

When a form is displayed as a modal dialog box, setting DialogResult enumeration value as the result for the form, it will hide the modal dialog box, and returns control to the calling form. But the form is just hidden, it is still there. The Close method is not called automatically. If you click the Close icon in the top-right corner of the form, it just causes the form to be hidden too. If you want to close the form, you have to load Dispose method in your code.

If a Form is displayed as a modeless window, the value returned by the DialogResult property might not return a value assigned to the form because the form’s resources are automatically released when the form is closed.

When you handle keyboard input in Windows Form, you have to control them in at least two level: Form level and Control level.

At Form level, you MUST set the KeyPreview property of the form to true so that keyboard messages are received by the form before they reach any controls on the form when Handle the KeyPress or KeyDown event of the form.

Firstly I show you our sample for the hotkey F4:

(Do not use KeyPress event because KeyPress event can not use e.KeyCode == Keys.F4 kinds of code)

The application includes a TextBox along with several other controls that allow you to move focus from the TextBox. The KeyPress event of the main Form consumes ‘1’, ‘4’, and ‘7’, and the KeyPress event of the TextBox consumes ‘2’, ‘5’, and ‘8’ while displaying the remaining keys. Compare the MessageBox output when you press a number key while the TextBox has focus with the MessageBox output when you press a number key while focus is on one of the other controls.

There is no direct alignment property for TableLayoutPanel. So lots
of programmer have similar question: How to align the controls in
TableLayoutPanel ?

Finally I found several FAQs about Windows Form TableLayoutPanel control (Content from Microsoft site):

Q: How to stretch and align elements within a TableLayoutPanel ?A: Use the anchor property of the control to align to a particular side (e.g. Left will stick it to the Left side). Using opposing anchors, the control can be stretched. E.g. Anchor=Left|Right will stretch to fill the width. The Dock property works in a similar manner.

Q: How to absolutely position elements within a TableLayoutPanel ?A: The margin property of control can be used to adjust the distance from the edge of the cell. If you change the anchor to be Top|Left and change the Margin to be (20,3,0,0) the control will be 20 pixels from the left edge and 3 pixels from the top.

Additionally the tableLayoutPanel provides several methods to change the position of controls that are already added to the table:tableLayoutPanel.SetCellPosition(new TableLayoutPanelCellPosition(4,2))NOTE: Via extender provider on the control itself the SetRow and SetColumn methods can be used.

To determine where a control was place programmatically use the following:public TableLayoutPanelCellPosition GetPositionFromControl(Control control);