C# Hacks

Flicker Free MDI Parent Window

You will see a flickering if you setup a background image for MDI parent window. This is because for some reason Microsoft guys didn’t add a way to enable double buffering property of MdiClient control. So you will need to enable that property manually to avoid flickering. Here is the code that does the trick!

private void MainWindow_Load(object sender, EventArgs e)
{
   foreach (Control control in this.Controls)
   {
      // find it
      if (control is MdiClient)
      {
         // repaint on resize
         control.SizeChanged += new EventHandler(SizeClientBG);

         // enable double buffering
         MethodInfo method = ((MdiClient)control).GetType().GetMethod("SetStyle",
BindingFlags.Instance |BindingFlags.NonPublic);
         method.Invoke((MdiClient)control, new Object[] { ControlStyles.OptimizedDoubleBuffer, true });

         break;
      }
   }
}

protected void SizeClientBG(object sender, EventArgs e)
{
   ((MdiClient)sender).Invalidate();
}

Focus To Next Control On Enter Key Press

Add following method into the form. It also avoids beep sound when pressing enter key.

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            // ignore buttons
            if ((keyData == Keys.Enter) && (!(this.ActiveControl is Button)))
            {
                SelectNextControl(ActiveControl, true, true, true, true);
                return true; //Stops the beeping
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

Validate TextBox On Loosing Focus

Following code validates content of a textbox when loosing the keyboard focus. Add it to the focus leave event of a textbox!

        private void textBox2_Leave(object sender, EventArgs e)
        {
            // amount validator code

            string txt = ((TextBox)sender).Text.Trim();
            if (txt.Length != 0) // textbox is not empty
            {
                float val;
                if (float.TryParse(txt, out val)) // textbox contains number, lets format it to 2 decimal points
                {
                    ((TextBox)sender).Text = val.ToString("n2");
                }
                else // textbox contains invalid characters, show msgbox and refocus
                {
                    MessageBox.Show("Invalid value entered!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    ((TextBox)sender).Focus();
                    ((TextBox)sender).SelectAll();
                }
            }
            else // textbox is empty, lets fill 0.00
            {
                ((TextBox)sender).Text = "0.00";
            }
        }
Advertisements

2 Responses to “C# Hacks”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: