Archive for March, 2013

CodeLyko v1.0 Released

Posted in Uncategorized on March 26, 2013 by Ruchira Hasaranga

untitled

I saw same coding pattern repeated again and again after building few database applications using c#. So I decided to create a tool to automate the process. This is it! You give system specification using xml file and this tool generates access database with tables+queries and connector code with storage classes.

Since generated code is a partial class, you can add new methods to that class using another source file without breaking auto generated code!

This tool was made to use with a gui tool. So there is no error checking operations occur while reading xml file and you may easily crash the app…  😉

Requirements:  DotNet 2.0 Runtime + Access

Download  (Mirror)

Advertisements

C# Hacks

Posted in Uncategorized on March 6, 2013 by Ruchira Hasaranga

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";
            }
        }