Share via


Winforms Tips and Tricks:

Coding:

 

1) Use BeginUpdate( ) and EndUpdate( ) while performing any bulk operation that would cause the window to redraw itself.

a. Large number of items to ListView.

b. Large number of nodes to TreeView.

BeginUpdate() sends a WM_REDRAW message passing FALSE as redraw state to the window. This stops any paint messages being sent or processed. After the bulk operation is done EndUpdate( ) should be called which sends a WM_REDRAW message passing TRUE as redraw state. This is followed by calling Invalidate( ) which adds a rectangle (normally this is the clientRectangle) to the specified window's update region. The update region represents the portion of the window's client area that must be redrawn when the window receives the next WM_PAINT message.

We provide this public API on the controls that need it like ComboBox, ListBox, ListView, TreeView. Thus following a call to BeginUpdate(), any redrawing caused by operations performed on the above mention controls is deferred until the call to EndUpdate().

MSDN help here

2) When list box is bound to a datasource he SelectedIndexChanged event fires 3 times. If you set the datasource after the valuemember and displaymember then the selectedindex is only fired once.

3) How Should the SelectedIndex event handled?

You should set the DataSource last. You can’t prevent SelectedIndexChanged from firing because the SelectedIndex does change (from -1 to 0).

4) Is there any reason why Datasource should be set first?

This is more efficient. Setting the DisplayMember after the DataSource will cause the ComboBox to rebind (re-populate).

5) You might experience that if you set the datasource after displaymember and valuemember, the displaymember gets reset to string.Empty. Why does this happen?

This happens if you set the DataSource to null. So if you set the DataSource to a valid value, then clear it by setting it to null, then back to a valid value you’d wipe out your DisplayMember.


 

Performance:

1) Use ResumeLayout( ) and SuspendLayout( ) pair while performing operations that would cause the control to Layout. As an example think of a Form that hosts controls that are laid out depending on the size of the Form. In this case as the Form size changes, all the controls are laid out and this would cause a lot of flicker while changing the size of the form (say through mouse drag). But you can call SuspendLayout( ) on the Form’s ResizeBegin( ) and ResumeLayout( ) on the Form’s ResizeEnd( ) and thus the layout happens only once, that is at the end of the Form's size change.

MSDN help here

2) Always set the Sorted property of the TreeView after adding the nodes if your are using the ADD method instead of using the AddRange. We call BeginUpdate and EndUpdate APIs while sorting if we know the number of nodes in the TreeView and that makes this approach faster than setting the Sorted property first and then adding nodes to the TreeView. As suggested earlier always use AddRange (since this would use BeginUpdate/EndUpdate by default) or else if you have to use Add then guard it by BeginUpdate and EndUpdate callss

 


Security:

1) Always use Declarative security (whenever possible) in your code when you are using Code Access Security than Imperative security. Imperative security uses managed objects to specify permissions and security actions during code execution, as opposed to declarative security which uses attributes to store permissions and actions in metadata. Imperative security is extremely flexible since you can set the state of a permission object and select security actions using information that is not available until runtime. With that flexibility is the risk that the runtime information you use to determine permission’s state does not remain unchanged while the action is in effect. Use declarative security whenever possible. Declarative demands are easier to understand and can be detected by tools such as Permview.exe.

Comments