C# Custom control – TextBox accepting only numbers, decimal numbers, signed numbers

| | | | |

The creation of custom control using Visual Studio involves the following steps:

  1. Start Visual Studio;
  2. Create new “Windows Control Library” project;
  3. In the created project delete “User Control” file;
  4. Go to “Project” > “Add User Control” and give it a name;
  5. Change the inherited class to this that you want your control to be;
  6. Create you custom properties for the control;
  7. Compile the project: Go to “Build” > “Build Solution”;
The generated DLL file is located in you projects folder under:“bin/Debug/UserContolName.dll” or “bin/Release/UserContolName.dll”.That depends on how your solution is configured: “Debug mode” or “Release mode”.

C# - How to add reference to project

| |

To add a reference to CSharp project do the following:

1) Open “Solution Explorer”;
2) Right click “References”;
3) Choose “Add Reference”.

On the Reference Manager Window go to: “Assemblies” -> “Framework”. In the list find the reference you need, select it and checkup the checkbox in front it. Then select “OK”.

C# Textbox - enter only positive numbers on KeyPress event in Windows form application

| | |

To prevent users from entering incorrect data in Windows form application, set restriction to allow only specific characters to be entered in the text box.

This example uses KeyPress event to monitor the users input and to apply the restriction required.
To avoid code duplication in Windows multiform applications for example, a separate class file is created “CommonFunctions.cs”.

The function to allow only numbers in textbox control is named InputNumbers.

For each Windows form, create a single KeyPress event and call the InputNumbers function inside it.

C# - Get connection string from Web.config file


To get the connection string from the Web config file with CSharp code include the following using directive:

using System.Configuration;

Then create a string variable and assign the connection string like this:

string connStr = ConfigurationManager.ConnectionStrings["connectionString_Name"].ConnectionString;

If “ConfigurationManager” is not recognized you will need to add reference to: "System.Configuration".

c# combobox set selected item by item's text

| | |

C# Combobox defined with name my_combobox

private System.Windows.Forms.ComboBox my_combobox;

Define string variable with the text of the combobox item that has to be selected. For example, if the combobox items have text values "item_a" "item_b" "item_c" ... and the item which text is "item_b" has to be selected

string select_this_item = "item_b";string item_text = "";  int i = 0;for (i = 0; i < my_combobox.Items.Count; i++) {    item_text = my_combobox.GetItemText(my_combobox.Items[i]);    if (item_text == select_this_item)    {       my_combobox.SelectedItem = (object)my_combobox.Items[i];       break;    }}

c# passing data between forms

| |

In C# project with two forms

public partial class Form1 : Form <- this is the main form

public partial class Form2 : Form <- this is the second form

To pass variables (values, data) from Form1 to Form2

Case: public variables in Form2

public string input_variable; <- put this in Form2 as global variable

public string output_variable; <- put this in Form2 as global variable

in Form1 inside the function that run (call) Form2

Form2 form = new Form2();

form.input_variable= "some string data"; <- send the input_variable from Form1 to Form2

DialogResult result = form.ShowDialog();

if (result == DialogResult.OK)


public string local_variable = form.output_var

c# combobox fill from database

| | |

To get only the unique /distinct/ data table records from any database connected to DataSet in C# project and to put them inside combobox.

Fill the table adapter with the records from database.


Create a view containing the database table.

DataView view = new DataView(this.myDB_DataSet.myDBTable);

Copy only the distinct records to new data table.

DataTable distinctValues = view.ToTable(true, "column_name");

For the combobox created, set the data source and display member properties.

c# Get selected item text from combobox

| | |

C# Combobox defined with name my_combobox

private System.Windows.Forms.ComboBox my_combobox;

Read the text of the selected combobox item and assign it to string variable, then show it in message box.

string selected_item_text = my_combobox.GetItemText(my_combobox.SelectedItem);MessageBox.Show(selected_item_text);

DataGridView copy to clipboard

| |


To copy the content of DataGridView cells to clipboard using MouseClick function

private void DataGridView1_MouseClick(object sender, MouseEventArgs e)


DataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;




DataGridView add row - C#

| |


Considering DataGridView with 3 text columns defined.

string[] row = new string[] { "column1_content", "column2_content", "column3_content" };