Posts Tagged ‘ C# ’

Number Only TextBox C# Windows Application

Put this code snippet your TextBox KeyPress Event, you will have your Number Only TextBox Working.


private void numberOnlyTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
 //allow digits only
 if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
 {
   e.Handled = true;
 }

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


Thanks
A Rahim Khan

Advertisements

How to delete files from a specific folder using datetime constraint (C# code)

To delete files from a specific folder you can use following code snippet written in c#.


foreach (FileInfo fileInformation in new DirectoryInfo(@”I:/TrialTesting/”).GetFiles())
{
  File.Delete(fileInformation.FullName);                        
}

Continue reading

C# date validation

There is no direct built-in function or method to validate DateTime Data Type in C#. This following self-explanatory code snippet shows a method that takes string as argument and validates it using ParseExact Method of DateTime. Moreover, you can choose any Date Format / Style for validation. Continue reading

Form Closing Event in C# (Windows Application)

Sometimes, Developers don’t want Users to close or leave a Form right away as Application still has some back ground process going on or it requires a confirmation before closing. In C# Windows Application, you can code at FormClosing Event. Have a look at this code snippet. Continue reading

Modal From in C#

Almost all Desktop Application Developers are familiar with Modal Form. It restricts user from opening any other Forms without closing the opened one. Do you know how to get this done in C# ? If your answer is “NO”, you are at the right place. It’s very simple. Continue reading

Sending Image Embedded HTML Mail with C#

If you are not familiar with .Net Mail, have a look here.

First of all, create LinkedResource Object for the image or images; you want to embed in your Mail. Assign a unique name to ContentId Property of LinkedResource Object.


//Creating LinkedSource for embedded picture
LinkedResource myPhoto = new LinkedResource("c:\\27_n.jpg");
myPhoto.ContentId = "me";

Continue reading

BackgroundWorker example in C# Windows Application

C# Component BackgroundWorker is used to accomplish time consuming task like mailing, taking Database Backup in a separate thread. First of all, you have to create an instance of BackgroundWorker to use it. Simply, drag and drop BackgroundWorker from ToolBox on your Form to have an instance or you may have your instance programmatically in the following way.


BackgroundWorker backgroundWorkerBackup = new BackgroundWorker();

Continue reading