Search this site:

2008-02-21

Some notes on C#.NET


Just some minor points to notes on C#.

1. The effective width or height of a control becomes zero when the control is docked.


That is, the Control.Height and Control.Width properties may return zero even it does occupy screen pixels. From my observation, there are some factors to this:
  • How the control itself is docked (Left, Right, Top, Bottom, or Fill)
  • How other control is docked
  • The situation only exists when the control has already been added to a control collection (Control.Controls)

You should be extremely careful if you have some how working with the control size if you have it docked. Take my situation as example, my form dynamically loads controls and resizes accordingly. The Control.SuspendLayout() and Control.ResumeLayout() methods are extremely useful in this case to avoid immediate changes of the size property:
this.SuspendLayout();
this.Controls.Add(my_control);
my_control.Dock
= DockStyle.Fill;
this.Height += my_control.Height;
this.ResumeLayout();


2. Special keys like Arrows, ESC and TAB do not fire KeyPress, KeyUp or KeyDown events


As title, Control.KeyPress, Control.KeyUp and Control.KeyDown do not help. To catch these key you need to override the Control.ProcessDialogKey() method.

Example:
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Escape)
{
this.Close();
return true;
}
return base.ProcessDialogKey (keyData);
}


BTW. All the above based on .NET Framework 1.1 that I am using. Newer versions may have different behavior.


1 comment:

HTML Tags allowed (e.g. <b>, <i>, <a>)

Disclaimer

ALL CONTENTS AND INFORMATION IN THIS WEB SITE ARE PROVIDED "AS IT" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE ACCURACY AND AVAILABILITY OF THE CONTENTS, INFORMATION AND THE WEB SITE ITSELF ARE NOT GUARANTEED. THE AUTHOR TAKES NO RESPONSIBILITIES ON ANY COSTS OR DAMAGES (DIRECT OR INDIRECT) ARISING OUT OF OR IN CONNECTION WITH THE ACCESS, USAGE OR INABILITY OF USAGE OF THIS WEB SITE.