Wednesday, April 27, 2011

Helper Methods

Helper - Format Friendly DataGridView (C#)

public static void FormatFriendlyUIGrid(DataGridView DGV)
    {
        int i = 0;
        int LastVisibleColumn = 0;
        foreach (DataGridViewColumn column in DGV.Columns)
        {
            if (column.HeaderText.StartsWith("_"))
                column.Visible = false;
            else
                column.HeaderText = column.HeaderText.Replace("_", " ");

            if (c.Visible)
            {
                i += column.Width;
                LastVisibleColumn = column.Index;
            }

        }

        DGV.Columns[LastVisibleColumn].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        DGV.Width = i + DGV.RowHeadersWidth + 2;
        //if (DGV.Rows.Count > 0)
        //    DGV.Height = DGV.GetRowDisplayRectangle(DGV.NewRowIndex, true).Bottom + DGV.GetRowDisplayRectangle(DGV.NewRowIndex, false).Height;
    }

Helper - Convert Time Remove Milliseconds (C#)

public static DateTime ConvertTimeNoMilliseconds(DateTime TimeValue)
    {
        string myDate = TimeValue.ToString("M/d/yyyy HH:mm:ss");
        return DateTime.Parse(myDate);
    }
Helper - Extend DateTimePicker To Include BackColor Property (C#)

Kudos to Vincenzo Rossi (on CodeProject) who posted this original code on 3 Nov 2008. This code below is a re-post of Vincenzo Rossi's DevNugget that I found recently.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms.VisualStyles;


namespace CheckedListBoxText
{
    /// 
    ///     A derivation of DateTimePicker allowing to change background color
    /// 
    class ExtendedDateTimePicker : System.Windows.Forms.DateTimePicker
    {
        private Color _backDisabledColor;

        public ExtendedDateTimePicker()
            : base()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            _backDisabledColor = Color.FromKnownColor(KnownColor.Control);
        }

        /// 
        ///     Gets or sets the background color of the control
        /// 
        [Browsable(true)]
        public override Color BackColor
        {
            get { return base.BackColor; }
            set { base.BackColor = value; }
        }

        /// 
        ///     Gets or sets the background color of the control when disabled
        /// 
        [Category("Appearance"), Description("The background color of the component when disabled")]
        [Browsable(true)]
        public Color BackDisabledColor
        {
            get { return _backDisabledColor; }
            set { _backDisabledColor = value; }
        }


        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            //Graphics g = e.Graphics;
            
            //The dropDownRectangle defines position and size of dropdownbutton block, 
            //the width is fixed to 17 and height to 16. The dropdownbutton is aligned to right
            Rectangle dropDownRectangle = new Rectangle(ClientRectangle.Width - 17, 0, 17, 16);
            Brush bkgBrush;
            ComboBoxState visualState;

            //When the control is enabled the brush is set to Backcolor, 
            //otherwise to color stored in _backDisabledColor
            if (this.Enabled) {
                 bkgBrush = new SolidBrush(this.BackColor);
                 visualState = ComboBoxState.Normal;
            }
            else {
                bkgBrush = new SolidBrush(this._backDisabledColor);
                visualState = ComboBoxState.Disabled;
            }

            // Painting...in action

            //Filling the background
            g.FillRectangle(bkgBrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);
            
            //Drawing the datetime text
            g.DrawString(this.Text, this.Font, Brushes.Black, 0, 2);

            //Drawing the dropdownbutton using ComboBoxRenderer
            ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, visualState);

            g.Dispose();
            bkgBrush.Dispose();
        }
    }
}

3 comments:

  1. SSN Pattern completed or last 4

    //^(?:\d{3})-(?:\d{2})-(?:\d{4})|(?:-{2})(?:\d{4})$
    //Textual meaning of regular expression
    //Match a string which starts with any digit 0-9 exactly 3 times
    //followed by the character -
    //followed by any digit 0-9 exactly 2 times
    //followed by the character -
    //ending in any digit 0-9 exactly 4 times
    //or the character - exactly 2 times
    //ending in any digit 0-9 exactly 4 times


    public const string SocialSecurityNumberPattern = @"(^\d{3}-\d{2}-\d{4}$)|(^-{2}\d{4}$)";

    ReplyDelete
  2. private void FillComboBoxFromEnums()
    {
    comboBoxDateRangeType.Items.AddRange(Enum.GetNames(typeof(Enums.Enums.DateRangeOperators)));
    }

    ReplyDelete
  3. A method to get all the Method Names in a specified class:

    foreach (var method in type.GetMethods())
    {
    var parameters = method.GetParameters();
    var parameterDescriptions = string.Join
    (", ", method.GetParameters()
    .Select(x => x.ParameterType + " " + x.Name)
    .ToArray());

    Console.WriteLine("{0} {1} ({2})",
    method.ReturnType,
    method.Name,
    parameterDescriptions);
    }

    ReplyDelete