Wednesday, February 18, 2009

How to release IntPtr handle C#

If you are using IntPtr as handles in C# code and wish to avoid handle leaks you may use :

IntPtr handle = some handle;
Marshal.Release(handle);

Google search didn't give me correct answer instantly so I added it here :)

cherio!!
Vaibhav


Saturday, February 7, 2009

height and width of text with specific font

When you draw a string on a C# form if you want to figure out how many pixel wide and high the text will be use:

TextRenderer Class

To measure the size you may use

TextRenderer.MeasureText :

TextRenderer.MeasureText (String, Font, Size)
TextRenderer.MeasureText (IDeviceContext, String, Font, Size)
TextRenderer.MeasureText (String, Font, Size, TextFormatFlags)
TextRenderer.MeasureText (IDeviceContext, String, Font, Size, TextFormatFlags)

It returns back System.Drawing.Size which has height and width of the text you wanted.


To draw text you may use:
private void RenderText1(PaintEventArgs e)
{
TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font,
new Point(10, 10), SystemColors.ControlText);
}


for more details you may visit:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textrenderer(VS.80).aspx

Wednesday, February 4, 2009

Disable right click pop up on textbox in C# winform

It took me almost 3 hours to google the correct way to fix this one...

There are 2 ways of disabling right click on textbox in a C# winform.

1. create a new context menu and add it to the text box control :

TextBox textbox1 = new TextBox();
ContextMenu cm = new ContextMenu();
textbox1.controls.add(cm);

and you are done.

2. Just do
textbox1.ShortcutsEnabled = false;

second one is short and sweet....