Monday, November 2, 2009

Editable ListBox User Control

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

using System.Drawing;

using System.Data;

namespace ExByTejas

{

public class EditableListBox : ListBox

{

TextBox editBox = new TextBox();

public EditableListBox()

{

editBox.Location = new System.Drawing.Point(0, 0);

editBox.Size = new System.Drawing.Size(0, 0);

editBox.Hide();

this.Controls.AddRange(new Control[] {this.editBox});

editBox.Text = "";

editBox.BackColor = Color.Beige;

editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);

editBox.LostFocus += new System.EventHandler(this.FocusOver);

}

protected override void OnKeyPress(KeyPressEventArgs e)

{

if (e.KeyChar == 13)

CreateEditBox();

base.OnKeyPress(e);

}

protected override void OnDoubleClick(EventArgs e)

{

CreateEditBox();

base.OnDoubleClick(e);

}

protected override void OnKeyDown(KeyEventArgs e)

{

if (e.KeyData == Keys.F2)

CreateEditBox();

base.OnKeyDown(e);

}

protected void CreateEditBox()

{

Rectangle r = this.GetItemRectangle(this.SelectedIndex);

string itemText = string.Empty;

if(this.DataSource == null)

itemText = (string)this.Items[SelectedIndex];

else

itemText = this.Text;

editBox.Location = new System.Drawing.Point(r.X, r.Y);

editBox.Size = new System.Drawing.Size(r.Width - 10, r.Height);

editBox.Show();

this.Controls.AddRange(new System.Windows.Forms.Control[] {this.editBox});

editBox.Text = itemText;

editBox.Focus();

editBox.SelectAll();

editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);

editBox.LostFocus += new System.EventHandler(this.FocusOver);

}

protected void FocusOver(object sender, System.EventArgs e)

{

UpdateEditedValue();

}

protected void EditOver(object sender, System.Windows.Forms.KeyPressEventArgs e)

{

if (e.KeyChar == 13)

UpdateEditedValue();

}

private void UpdateEditedValue()

{

if (this.DataSource == null)

{

this.Items[SelectedIndex] = editBox.Text;

editBox.Hide();

}

else

{

DataRowView rowView = (DataRowView)this.SelectedItem;

rowView.Row[this.DisplayMember] = editBox.Text;

editBox.Hide();

}

}

}

}

Monday, September 7, 2009

Put HTML page break for printing

you can use any of the following style for HTML page break (specially for printing)
1) style="page-break-after:always"
2) style="page-break-before:always"

Friday, September 4, 2009

Single border around HTML Table cell

style="border-collapse:collapse"

Borders for the cells are generally displayed independently from each other. so each cell has its own border and that's what border looks like double for the whole table. we can collapsed this double border into single border with the border-collapse style property, check the above example.

Select Distinct Column Values From DataTable

DataView dv= new DataView(SourceTable);
DataTable dt = dv.ToTable(true, "Col1", "Col2","Col3" ...);

Wednesday, September 2, 2009

get mouse position in c#

I might be wrong but you can try following two

Point p = Cursor.Position;
Point p2 = Control.MousePosition;

Tuesday, August 25, 2009

Add Custom Property into DataGridViewTextBoxColumn

public class CustomDataGridViewTextBoxColumn : DataGridViewTextBoxColumn
{
private bool _displayContextMenu = false;
public bool DisplayContextMenu
{
set { _displayContextMenu = value; }
get { return _displayContextMenu ; }
}

public override object Clone()
{
CustomDataGridViewTextBoxColumn myCol = (CustomDataGridViewTextBoxColumn)base.Clone();
myCol.DisplayContextMenu = _displayContextMenu ;
return myCol;
}
}

Note: When you derive from DataGridViewTextBoxColumn and add new properties to the derived class, be sure to override the clone method to copy the new properties during cloning operations. You should also call the base class's clone method so that the properties of the base class are copied to the new cell.

Thursday, July 2, 2009

Create fixed size string for CSV file in C#

public static string GetFixedLengthString(string input, int length)
{
string result = string.Empty;
if (string.IsNullOrEmpty(input))
{
result = new string(' ', length);
}
else if (input.Length > length)
{
result = input.Substring(0, length);
}
else
{
result = input.PadRight(length);
}
return result;
}