Pages

Showing posts with label winforms. Show all posts
Showing posts with label winforms. Show all posts

Saturday 20 June 2015

Code to set ShortCut Keys in winforms

Introduction:
The shortcut keys are used to make your application user friendly and easy to use. But as you know there are some default short keys prepared by Microsoft Windows and some times you need to override these shortkeys.

Code:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.B))  //Key combination
            {
                 // your code here
                return true;    // indicate that you handled this keystroke
            }

            if (keyData == Keys.Control) //Single Key
            {
                txtamount.Focus();
                return true;
            }

            // Call the base class
            return base.ProcessCmdKey(ref msg, keyData);
        }


Sunday 29 June 2014

Export data to EXCEL form ListView in Windows Application

Overview :  While working on windows form application sometimes we need to copy all data of listview to an EXCEL sheet. This is very important and needy task in most of the application because EXCEL provides as a great and easy data management platform for those who are not very much expert in handling large database, I am talking about the end user of the application.

Example: Suppose I have a listview in my form having columns as described below -

             listView1.Columns.Add("Item Code", 120);
            listView1.Columns.Add("Party Name", 100);
            listView1.Columns.Add("Brand", 80);
            listView1.Columns.Add("Model No", 100);
            listView1.Columns.Add("Item Name", 100);
            listView1.Columns.Add("Category", 80);
            listView1.Columns.Add("Size", 50);
            listView1.Columns.Add("Color", 70);
            listView1.Columns.Add("Quantity", 50);
            listView1.Columns.Add("Purchase Rate", 90);
            listView1.Columns.Add("Sale Rate", 90);
            listView1.Columns.Add("Disc(%)", 50);
            listView1.Columns.Add("Final Price", 70);
            listView1.Columns.Add("Branch", 100);

 I have a button placed somewhere on the form, so code for the button is as follows -

            copyAlltoClipboard(listView1);
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlexcel = new Excel.Application();
            xlexcel.Visible = true;
            xlWorkBook = xlexcel.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
            CR.Select();
            xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);


function copyAlltoClipboard(ListView lv) used on buttins's click event as follows -

        public void copyAlltoClipboard(ListView lv)
        {
            StringBuilder buffer = new StringBuilder();

            for (int i = 0; i < lv.Columns.Count; i++)
            {
                buffer.Append(lv.Columns[i].Text);
                buffer.Append("\t");
            }

            buffer.Append("\n");

            for (int i = 0; i < lv.Items.Count; i++)
            {
                for (int j = 0; j < lv.Columns.Count; j++)
                {
                    buffer.Append(lv.Items[i].SubItems[j].Text);
                    buffer.Append("\t");
                }

                buffer.Append("\n");
            }

            Clipboard.SetText(buffer.ToString());
        }

NemaSpace used : 

using Excel = Microsoft.Office.Interop.Excel;
using fm = System.Windows.Forms;