deep copying objects in C#

C# — Tags :, , , — admin @ 21:26

One of the fastest ways to deep copy an object is to serialize it to a binary stream and unserialize it to a new object, in that matter:

  1.        /// <summary>
  2.         /// Creates a deep copy of a serializable object
  3.         /// </summary>
  4.         /// <typeparam name="T">Any type</typeparam>
  5.         /// <param name="serializableObject">serializable object</param>
  6.         /// <returns>cloned object</returns>
  7.         public static T CloneOf<T>(T serializableObject)
  8.         {
  9.             object objCopy = null;
  10.             MemoryStream stream = new MemoryStream();
  11.             BinaryFormatter binFormatter = new BinaryFormatter();
  12.             binFormatter.Serialize(stream, serializableObject);
  13.             stream.Position = 0;
  14.             objCopy = (T) binFormatter.Deserialize(stream);
  15.             stream.Close();
  16.             return (T) objCopy;
  17.         }
  18.  

Playing with winforms textbox border color.

Non classé — Tags :, , , — admin @ 20:12

This is a small hack class that adds a set color for input area borders (textbox, richtextbox, combos, etc.) Here is the result:

Screenshot

Screenshot

It all holds up in a single file:

  1.  
  2.     /// <summary>
  3.     /// This class adds border to input elements similar to Chrome
  4.     /// </summary>
  5.     public class FormBorderizer
  6.     {
  7.         /** PRIVATE MEMBERS **/
  8.         private Label m_lblBorder;                      // label used for borders
  9.         private static Color BORDER_COLOR = Color.Gold; // Border color
  10.         private static int BORDER_WIDTH = 2;            // Border width
  11.         private static Color BACK_COLOR = Color.AliceBlue;       // Back color
  12.  
  13.         /** CONTRUCTORS **/
  14.         /// <summary>
  15.         /// Constructor, adds borders to input areas
  16.         /// </summary>
  17.         /// <param name="Control.ControlCollection">collection of controls to parse</param>
  18.         public FormBorderizer(Control.ControlCollection controlCollection)
  19.         {
  20.             InitializeBorder(controlCollection);
  21.             AddBorder(controlCollection);
  22.         }
  23.  
  24.         /** METHODS **/
  25.  
  26.         /// <summary>
  27.         /// Initializes the border control
  28.         /// </summary>
  29.         /// <param name="controlCollection">collection</param>
  30.         private void InitializeBorder(Control.ControlCollection controlCollection)
  31.         {
  32.             m_lblBorder = new Label();
  33.             m_lblBorder.BackColor = Color.Gold;
  34.             m_lblBorder.Visible = false;
  35.             controlCollection.Add(m_lblBorder);
  36.             m_lblBorder.SendToBack();
  37.         }
  38.  
  39.         /// <summary>
  40.         /// Adds a colored border to a collection of controls
  41.         /// </summary>
  42.         /// <param name="controlCollection">target collection</param>
  43.         private void AddBorder(Control.ControlCollection controlCollection)
  44.         {
  45.             foreach (Control c in controlCollection)
  46.             {
  47.                 // generate event handlers for input areas
  48.                 if (IsInputControl(c))
  49.                 {
  50.                     c.Enter += new EventHandler(c_Enter);
  51.                     c.Leave += new EventHandler(c_Leave);
  52.                 }
  53.                 else if (c is Panel)
  54.                 {
  55.                     new FormBorderizer(c.Controls);
  56.                 }
  57.                 else if (c is TabControl)
  58.                 {
  59.                     TabControl tc = (TabControl)c;
  60.                     foreach (TabPage tp in tc.TabPages)
  61.                     {
  62.                         new FormBorderizer(tp.Controls);
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.  
  68.         /// <summary>
  69.         /// Checks whether this control should hold a border or not
  70.         /// </summary>
  71.         /// <param name="c">target control</param>
  72.         /// <returns>true or false</returns>
  73.         private bool IsInputControl(Control c)
  74.         {
  75.             return (c is TextBox || c is ListBox || c is ComboBox || c is RichTextBox || c is MaskedTextBox);
  76.         }
  77.  
  78.         /** EVENTS **/
  79.         /// <summary>
  80.         /// Event occuring when the user leaves an input control
  81.         /// </summary>
  82.         private void c_Leave(object sender, EventArgs e)
  83.         {
  84.             m_lblBorder.Visible = false;
  85.             Control ctrl = (Control)sender;
  86.             ctrl.BackColor = Color.White;
  87.            
  88.         }
  89.  
  90.         /// <summary>
  91.         /// Event occuring when the user enters an input control
  92.         /// </summary>
  93.         private void c_Enter(object sender, EventArgs e)
  94.         {
  95.             Control ctrl = (Control)sender;
  96.             WrapControl(ctrl);
  97.             m_lblBorder.Visible = true;
  98.             m_lblBorder.Anchor = ctrl.Anchor;
  99.         }
  100.  
  101.         /// <summary>
  102.         /// Wraps the border around the control
  103.         /// </summary>
  104.         /// <param name="control"></param>
  105.         private void WrapControl(Control control)
  106.         {
  107.             m_lblBorder.Top = control.Top - BORDER_WIDTH;
  108.             m_lblBorder.Left = control.Left - BORDER_WIDTH;
  109.             m_lblBorder.Width = control.Width + 2 * BORDER_WIDTH;
  110.             m_lblBorder.Height = control.Height + 2 * BORDER_WIDTH;
  111.             control.BackColor = BACK_COLOR;
  112.         }
  113.  
  114.     }
  115.  

To call it, simply create an instance of FormBorderizer (such an awful name!!) and initialize it with a form / panel control collection.
Ex:

  1.    public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.             new FormBorderizer(this.Controls);
  7.         }
  8.  
  9.     }
  10.  
  11.  

Winforms flicker-free forms.

C/C++, Non classé — Tags :, , — admin @ 0:12

Ever had those flickers in your 50+ component forms?

Trying to play with SuspendLayout / ResumeLayout didn’t help? That’s because it only suspends the automatic layout, triggered by the Anchor and Dock properties.

Setting double-buffering to true didn’t help either? That’s because it only suppresses flicker on individual controls: a label, a button and so on…

Setting the OptimizedDoubleBuffer flag to true? Nope, no changes…

After struggling for many hours trying to eliminate a flicker that was occuring on my application form, I finally found a solution on msdn:

It’s called compositing double-buffering and it’s simply 7 lines of code put in somewhere in your form code:

  1.  
  2. protected override CreateParams CreateParams {
  3.   get {
  4.     CreateParams cp = base.CreateParams;
  5.     cp.ExStyle |= 0×02000000;
  6.     return cp;
  7.   }
  8. }

Explanation from nobugz:

I discovered a new Windows style in the SDK header files, available for Windows XP and (presumably) Vista: WS_EX_COMPOSITED. With that style turned on for your form, Windows XP does double-buffering on the form and all its child controls

nobugz is the man…

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2012 Random namespace | powered by WordPress with Barecity