Monday, December 22, 2008

Read AssemblyInfo file in runtime

Sometime we need to read the information which is there in the assemblyinfo.cs file for about formbox. I googled in internet, finally found an answer.

Assembly currentAssembly = Assembly.GetExecutingAssembly();

AssemblyName assemblyName = currentAssembly.GetName();

//If only need version information then use.
assemblyName.Version.ToString();

//Copyright information
AssemblyCopyrightAttribute copyright =
AssemblyCopyrightAttribute.GetCustomAttribute(currentAssembly,
typeof(AssemblyCopyrightAttribute)) as AssemblyCopyrightAttribute;

like this you can get all the attribute information from assemblyinfo.cs file.

AssemblyVersionAttribute
AssemblyProductAttribute
AssemblyDescriptionAttribute
AssemblyCompanyAttribute
AssemblyTitleAttribute

If you need any more information feel free contact me at any time.

Sunday, December 21, 2008

Checkable group box

Some days ago, i have requirement to design the UI in such way that it should contains group box and chack box on top it. so i thought it's very simple somebody might have done it. i searched on internet, i couldn't find a control. so i decided to create a control which will contains check box on the top corner of group box. It has features like ordinary checkbox if click on checkbox then automatically disable/enable the controls within the group box. if you want some action should be performed while clicking on groupbox then subscribe of for checkbox click event also.

Because of blog file upload constrains i am not able upload file here. but i am telling the steps which needs to be done and code.

1) Implement a Groupbox control
2)Override paint event for drawing of checkbox
3)Override mouse down event for changing the status of check box.

Here my control code.
------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.VisualStyles;
namespace WindowsApplication
{
public class CheckableGroupBox : GroupBox
{
#region Fields
private bool checkedStatus = false;
private string text = "CheckGroupBox";
private Color textColor = Color.Blue;
#endregion
#region Constructor
public CheckableGroupBox()
{
this.SetStyle(ControlStyles.UserPaint ControlStyles.AllPaintingInWmPaint, true);
}
#endregion
#region Properties
public bool Checked
{
get { return this.checkedStatus; }
set
{
this.checkedStatus = value;
foreach (Control ctrl in this.Controls)
{
ctrl.Enabled = value;
}
}
}
public new string Text
{
get { return this.text; }
set { this.text = value; }
}
public Color TextColor
{
get { return this.textColor; }
set { this.textColor = value; }
}
#endregion
#region Events
public event EventHandler CheckStateChanged;
#endregion
#region Protected Methods
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
float totalLength = e.ClipRectangle.Right - e.ClipRectangle.Left;
float quaterLength = ((totalLength / 2) / 4);
int startPoint = (int)(e.ClipRectangle.Left + quaterLength);
Rectangle rectAngle = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y - 1,
e.ClipRectangle.Width, e.ClipRectangle.Height);
rectAngle.X = startPoint + CheckBoxRenderer.GetGlyphSize(e.Graphics,
CheckBoxState.UncheckedNormal).Width;
SizeF sizeF = e.Graphics.MeasureString(Text, this.Font);
rectAngle.Width = (int)sizeF.Width + 2;
rectAngle.Height = this.Font.Height;
CheckBoxRenderer.RenderMatchingApplicationState = true;
Rectangle rectAngleBack = new Rectangle(startPoint - 3, rectAngle.Y,
(rectAngle.X - (startPoint - 3)) + rectAngle.Width, rectAngle.Height);
CheckBoxRenderer.DrawParentBackground(e.Graphics, rectAngleBack, this);
Font font = Font;
CheckBoxRenderer.DrawCheckBox(e.Graphics, new System.Drawing.Point(startPoint, e.ClipRectangle.Y),
rectAngle, Text, this.Font, TextFormatFlags.VerticalCenter, false,
checkedStatus ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
using (Graphics g = this.CreateGraphics())
{
Rectangle rectAngle = ClientRectangle;
rectAngle.X = ClientRectangle.Left + (((ClientRectangle.Right - ClientRectangle.Left) / 2) / 4);
rectAngle.Y -= 1;
rectAngle.Width = CheckBoxRenderer.GetGlyphSize(g, checkedStatus ?
CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal).Width;
rectAngle.Height = CheckBoxRenderer.GetGlyphSize(g, checkedStatus ?
CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal).Height;
if (rectAngle.Contains(e.Location))
{
Checked = !Checked;
if (CheckStateChanged != null)
{
CheckStateChanged(this, new EventArgs());
}
}
}
Invalidate();
}
}
#endregion
}
}
------------------------------------------------------------------------------------------------
Just create c# file and copy paste above code, compile and use it in your application.

Feel free contact me if you need any more information and also i am very glad accept your suggestion.

Thursday, August 28, 2008

Virtual Cancel Button

You can add a Virtual cancel button by using KeyPreview property and Key Down event of Form. Just add Key Down event method implementation for form and Set KeyPreview property as True.

Below is code Details

This.Form.KeyPreviw = true;

private void SettingsForm_KeyDown(object sender, KeyEventArgs e)

{

if (e.KeyCode == Keys.Escape)

{

this.DialogResult = DialogResult.Cancel;

}

}

Setting of KeyPreview Property of form, will receive all the keystroke events (KeyPress, KeyDown, KeyUp). After form event handler completed processing the keystroke, the keystroke assigned to the control with focus.By default is false.

Note : If the form doesn’t contains any visible and enable controls then it automatically receive all keyboard events.


For more information


Click Here


This is my first post, Please feel free to give comments if you can send me mail also.