#1. How to set the default button for a form?
Default Button of a form means that button on form whose click event fires when Enter key is pressed. To make a button on form as default set the form's AcceptButton property. You can do this either through the designer, or through code such as
form1.AcceptButton = button1;
#2. How to set the Cancel button for a form?
Cancel Button of a form means that button on form whose click event fires when ESC key is pressed. To make a button on form as Cancel set the form's CancelButton property. You can do this either through the designer, or through code such as
form1.CancelButton = button1;
#3. How to trigger a button click event?
In VB 6.0 it was possible to call CommandButton click event from anywhere like any other method or function (Sub). But in .NET it is not possible in same way. But .NET provides a very simple way to do this. Just use the button's public method PerformClick.
button1.PerformClick();
Alternative: The tip below is provided by kaminm
You can trigger a button (Web and Win) by calling Buttonclick with null parameters
btnClear_Click(null,null)
Alternative: The tip below is provided by Paul Brower
You can use it this way, if you're planning on doing something with the sender object, you have a reference to it.
button1_click(button1,EventArgs.Empty)
Combo Box
#1. How to fill a ComboBox with the available fonts?
comboBox1.Items.AddRange (FontFamily.Families);
Text Box
#1. How to disable the default ContextMenu of a TextBox?
To prevent the default context menu of a TextBox from showing up, assign a empty context menu as shown below:
textBox1.ContextMenu = new ContextMenu ();
#2. How to enter multiline text in textbox through code?
Sometimes it is needed to show data on different lines. The first idea that comes is to set MULTILINE Property to true and use '\n' escape sequence for this. But this escape sequence is not supported in .NET textbox. Still it very easy to overcome this problem. To assign multiline text at design time, in the designer window use the LINES property of TextBox control. For achieving this at runtime, create an array of string and assign it to LINES property of Textbox as shown below.
string [] strAddress = {"Mukund Pujari","Global Transformation Technologies","Pune, India"};
textBox1.MultiLine=true;
textBox1.Lines=strAddress;
Alternative: The tip below is provided by joelycat
.NET text boxes don't recognize \n but they do recognize \r\n. Try:
textBox1.Text="Line 1\r\nLine2\r\nLine3.";
Alternative: The tip below is provided by Robert Rohde
Actually "System.Environment.NewLine" should be used instead. This way you are platform independant.
Alternative: The tip below is provided by Redgum
simply use a "RichTextBox" for those areas on your form that require multiple lines
of randomly output text, and use a simple text box for those that do not.
#3. Some useful TextBox Validations
Numeric TextBox
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( !( char.IsDigit( e.KeyChar ) || char.IsControl( e.KeyChar ) ) )
{
e.Handled = true;
}
}
Numeric TextBox with Decimals
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( !( char.IsDigit( e.KeyChar) || char.IsControl( e.KeyChar ) ||(e.KeyChar== (char )46)) )
{
e.Handled = true;
}
}
TextBox Allowing Characters Only
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( !( char.IsLetter( e.KeyChar ) || char.IsControl( e.KeyChar ) ) )
{
e.Handled = true;
}
}
TextBox Allowing Upper Case Characters Only
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( !( char.IsUpper( e.KeyChar ) || char.IsControl( e.KeyChar )) )
{
e.Handled = true;
}
}
TextBox Allowing Lower Case Characters Only
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( !( char.IsLower( e.KeyChar ) || char.IsControl( e.KeyChar )) )
{
e.Handled = true;
}
}
Check For Unfilled TextBox
// Call this function and pass the Textbox as parameter to this function
public static bool ChkEmpty(params System.Windows.Forms.TextBox[ ] tb)
{
int i;
for (i = 0; i < tb.Length; i++)
{
if (tb.Text.Trim() == "")
{
MessageBox.Show("Don't keep field empty");
tb.Focus();
return false;
}
}
return true;
}
Localizing Validations - Country Specific Decimal Character
The tip below is provided by curt
Here he tells us, how different characters can be used for decimals depending upon the countries. For e.g. people in France may use character other than dot (.) for decimal point.
string DecimalSeparator = Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( !( char.IsDigit( e.KeyChar) || char.IsControl( e.KeyChar ) || (DecimalSeparator.IndexOf(e.KeyChar) != -1 ) ) )
{
e.Handled = true;
}
}
DateTime Picker
#1. How to make the DateTimePicker show empty text if no date is selected?
Use following code in some Button Click event:
dateTimePicker1.CustomFormat=" ";
dateTimePicker1.Format=DateTimePickerFormat.Custom;
Data Grid
#1. How to remove the indeterminate status of checkbox in datagrid?
The checkbox in checkbox column of datagrid shows indeterminate status by default. To remove this behaviour set AllowNull property of the CheckBox column to false as below:
DataGridTableStyle ts1 = new DataGridTableStyle(); // Create New TableStyle
ts1.MappingName = "Items"; // Assign the name of Data Table to which Style is applied
DataGridColumnStyle boolCol = new DataGridBoolColumn(); // Create a CheckBox column
boolCol.MappingName = "ch"; // Assign the name of Data Column
boolCol.AllowNull=false; // This property actually removes the indeterminate status of checkboxes
#2. How to group columns in DataGrid?
Download source files - 9.57 Kb
Download demo project - 5.26 Kb
Hi friends, you may be knowing better ways of doing it, but I managed to find this solution in the time limit I had been given. The logic is that, while looping through datatable we save the values of current column and previous column and we compare it. If Current Value is same as Previous Value, we don't show it in grid and if it is not same then we show it.
/* The logic is that, while looping through datatable we save the
values of current column and previous column and we compare it.
If Current Value is same as Previous Value, we don't show it in grid
and if it is not same then we show it.
1. We save value of current column in variable 'strCurrentValue'.
2. After the loop we assign the value in 'strCurrentValue' to
variable 'strPreviousValue'.
3. And in next loop, we get new value in 'strCurrentValue'.
4. Now we can compare value in 'strCurrentValue' and 'strPreviousValue'
and accordingly show or hide values in the column.
*/
int m;
for(m=0;m<8;m++)
{
object cellValue = dt.Rows[m]["Category"]; // Here we catch the value form current column
strCurrentValue=cellValue.ToString().Trim(); // We assign the above value to 'strCurrentValue'
if(strCurrentValue!=strPreviousValue) // Now compare the current value with previous value
{
dt.Rows[m]["Category"]=strCurrentValue; // If current value is not equal to previous
// value the column will display current value
}
else
{
dt.Rows[m]["Category"]=string.Empty; // If current value is equal to previous value
// the column will be empty
}
strPreviousValue=strCurrentValue; // assign current value to previous value
}
strCurrentValue=string.Empty; // Reset Current and Previous Value
strPreviousValue=string.Empty;
Panel
#1. How to make a Panel or Label semi-transparent on a Windows Form?
You can make a panel or label transparent by specifying the alpha value for the Background color.
panel1.BackColor = Color.FromArgb(65, 204, 212, 230);
NOTE:In the designer you have to enter these values
manually in the edit box. Don't select the color using the ColorPicker.Buttons
Buttons