Archive for the ‘WinForms’ Category

h1

Numeric only text box in .net Winforms

July 18, 2011

We can create Numeric only text boxes in .Net Windows forms application with adding below code in Key Press Event.

Numeric only text boxes

private void txtBox_KeyPress(object sender, KeyPressEventArgs e)

{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != ‘.’)
{
e.Handled = true;
}

// only allow one decimal point
if (e.KeyChar == ‘.’ && (sender as TextBox).Text.IndexOf(‘.’) > -1)
{
e.Handled = true;
}
}

 

Integer only text boxes

private void txtBox_KeyPress(object sender, KeyPressEventArgs e)

{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) )
{
e.Handled = true;
}

}

Advertisements
h1

Confirm Dialog in .Net WinForms

April 26, 2011

We can use MessageBox as Confirm Dialog box

Coding

if(MessageBox.Show(“Confirm this?”, “Test”, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
/// Code for ‘Yes’
}
else
{
/// Code for ‘No’
}
h1

AutoGenerateColumn property – Winforms

September 13, 2010

In datagridview properties can not find AutoGenerateColumn property, but can find pragmatically.

Here can set false to hide columns, need to do before set the datasource.

Datagridview1.AutoGenerateColumn = false; 
Datagridview1.DataSource = dt;
h1

Non-Editable ComboBox

July 21, 2010

For non-Editable comboBox Set DropDownStyle as DropDownList.

Coding
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;