Wednesday, September 11, 2013

How to make textbox accept only numbers?

  try
            {
               float number;
               number = float.Parse(textBox1.Text.ToString());
             }

            catch (Exception h)
            {
                MessageBox.Show("Please provide number only");
                textBox1.Text = "";
                textBox1.Focus();
            }

How to use open dialog box in C#

First of all, we need to place a opendialogbox control on our form.


string filename;
OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.Filter = "*.xls | *.xlsx";
openFileDialog1.ShowDialog();
filename = openFileDialog1.FileName.ToString();


How to use Messagebox in C#

We can display any alert message using following code.


MessageBox.Show("Type your message here");

How to copy the content of a cell of an excel file to another excel file using C#









Following is the part of code to copy the content of a cell of an excel file to a cell of another excel file using C# as programming language



using Excel = Microsoft.Office.Interop.Excel;
using ApplicatonClass = Microsoft.Office.Interop.Excel.ApplicationClass;
using Microsoft.Office.Core;
  
Excel.ApplicationClass excelapplicationclass = new Microsoft.Office.Interop.Excel.ApplicationClass();
Excel.Workbook destinationworkbook = null;
Excel.Workbook sourceworkbook_LOI = null;
Excel.Range sourcerange = null;
Excel.Range destinationrange = null;

sourceworkbook = excelapplicationclass.Workbooks.Open("sourcefilename with path", 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
worksheet = (Excel.Worksheet)sourceworkbook.Sheets[1];

destinationworkbook = excelapplicationclass.Workbooks.Open("destinationfilename with path", 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

destinationworksheet = (Excel.Worksheet)destinationworkbook.Sheets[1];
sourcerange = worksheet.get_Range("B17", "I19");
destinationrange = ((Excel.Worksheet)destinationworkbook.Sheets[1]).get_Range("B17", "I19");

sourcerange.Copy(Missing.Value);
            destinationrange.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValuesAndNumberFormats, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);


destinationworkbook.Save();
sourceworkbook_LOI.Save();