Solving development problems  |  About this blog

Archive for the ‘c#’ tag

Managing memory leaks in WinForms application

Few things that can create memory leaks are:

  1. If you have a UserControl which you add dynamically to your form and if you attach events dynamically to this UserControl using:< br />
    ucMyControl.btnSave.Click += new EventHandler(btnSave_Click);
    

    and if you are removing you UserControl from the form using Controls.Clear() method then you must first remove all attached events before removing your UserControl using

    ucMyControl.btnSave.Click -= new EventHandler(btnSave_Click);
    
  2. When removing dynamically added control from Form/Panel calling Controls.Clear() is not enough and instance stays in the heap after thar, so before this command call Dispose() on your dynamically added UserControl (that means that you should save a reference to your control in order to dispose it after)
  3. If you have modal forms do not forget to call Dispose() after ShowDialog() method and set their reference to null in order to be garbage collected!

    MyDialog dialog = new MyDialog();
    dialog.ShowModal();
    dialog.Dispose();
    dialog = null;
    

How to span columns or rows in TableLayoutPanel?

How you can span columns or rows in WinForms TableLayoutPanel?
Using designer you can not span columns and rows directly in TableLayoutPanel but you can, when add one control into TableLayoutPanel set ColumnSpan or RowSpan of this control instead.

  1. Drag a TableLayoutPanel control from the Toolbox onto your form.
  2. Drag a ListBox control from the Toolbox into the upper-left cell of the TableLayoutPanel control.
  3. Set the ListBox control’s ColumnSpan property to 2. Note that the ListBox control spans the first and second columns.

Written by Avivo

December 9th, 2010 at 12:55 am

Parsing URL query string into key-value pairs in C#

Imagine you have URL something like this:

http://www.yourdomain.com?param1=value1&param2=value2&param3=value3&param4=value4

and you want to get key-value pairs such as

  • {“param1″, “param2″, “param3″, “param4″}
  • {“value1″, “value2″, “value3″, “value4″}

It is easy to do using system function:

using System.Collections.Specialized;
NameValueCollection query = HttpUtility.ParseQueryString(queryString);
Response.Write(query["param1"]);

Written by Avivo

December 3rd, 2010 at 1:08 pm

Remove items from List using LINQ

Typical scenario: you got your list of items from database but you want to remove some items from it (items that match additional criterion).

It is simple as this (i.e. assuming that your list is named yourItems):

yourItems.RemoveAll(x => x.ItemPropertyId != null);

Written by Avivo

November 24th, 2010 at 7:13 pm

Force dot as decimal separator and ignore thousand separator when formatting decimal numbers

Sometimes, when you are doing data integrations, you can have a vendor that can accept numbers only in specific format.
So (for EN culture), our vendor said to us:
This is not valid: 10,000.44
This is valid for me: 10000.44

That means we had to remove thousand separator and needed to force dot as decimal separator (btw, we are SI culture and we use comma as decimal separator and dot is thousand separator).

This is the way how to do it:

CultureInfo ci = CultureInfo.GetCultureInfo("en-US");
NumberFormatInfo nfi = (NumberFormatInfo)ci.NumberFormat.Clone();
//Now force thousand separator to be empty string
nfi.NumberGroupSeparator = "";
//Format decimal number to 2 decimal places
string decimalFormatted = product.Price.ToString("n2", nfi);