Lets Have a fun with Technology.

BTemplates.com

Follow by Email

Wednesday, October 12, 2011

How to Add Progress bar in windows application ?




Hello Friend’s
In this article I am going to explain how to add progress bar in windows application.
In web application, we have much option and having third party controls too. Like
RadControls , NetAdvantages controls etc..  But in windows application, there is no straight way to add progress bar as per my thinking.  In this article I am going to give one small example so you can implement into your application.

First Make one application and in one Form put below controls.
1.)    Button (on clicking button we display progress Bar)
2.)    Picture box  (option to display Loading Image)
3.)    Label (give appropriate message to user like Please wait….)


public partial class KetanProgressBar : Form
    {
        private BackgroundWorker bgworker;
        delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
        public KetanProgressBar()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            bgworker = new BackgroundWorker();
            bgworker.WorkerSupportsCancellation = true;
            bgworker.WorkerReportsProgress = true;
            bgworker.DoWork += bwMC_DoWork;
            bgworker.ProgressChanged += bwMC_ProgressChanged;
            bgworker.RunWorkerCompleted += bwMC_RunWorkerCompleted;
        }
        private void bwMC_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            SetControlPropertyValue(pictureBox1, "Visible", true);
            SetControlPropertyValue(lblWait, "Visible", true);
        }
        private void bwMC_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if(!e.Cancelled)
            {
                SetControlPropertyValue(lblWait, "Visible", false);
                SetControlPropertyValue(pictureBox1, "Visible", false);
                MessageBox.Show("Action Completed", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void SetControlPropertyValue(Control oControl, string propName, object propValue)
        {
            if (oControl.InvokeRequired)
            {
                SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
                oControl.Invoke(d, new object[] { oControl, propName, propValue });
            }
            else
            {
                Type t = oControl.GetType();
                PropertyInfo[] props = t.GetProperties();
                foreach (PropertyInfo p in props)
                {
                    if (p.Name.ToUpper() == propName.ToUpper())
                    {
                        p.SetValue(oControl, propValue, null);
                    }
                }
            }
        }
        private void bwMC_DoWork(object sender, DoWorkEventArgs e)
        {
            bgworker.ReportProgress(1);
//just for test we make looping so progressbar will display.
            for (int i = 0; i <= 100000000; i++)
            { }
           
        }
        private void button1_Click(object sender, EventArgs e)
        {
            bgworker.RunWorkerAsync();
            Application.DoEvents();
        }



I think all statements are self explanatory. There is no need to explain. If you have any doubt in any statement then let me know.

Enjoy !!!!!!

Excel header print setting (repeat header content’s on every page during printing) using C#.Net


Excel header print setting (repeat header content’s on every page during printing) using C#.Net





Hello friend's


When we create excel file from C# there may be need to repeat some part’s when we printing.

See below code bloack to set it from C#.



  Microsoft.Office.Interop.Excel.Application xlApp;
                Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Excel.Range chartRange;
                xlApp = new app.Application();

                xlWorkBook = xlApp.Workbooks.Add(misValue);
                xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                xlWorkSheet.PageSetup.PrintTitleRows = "R1:R11";

// Repeat Rows from R1 to R11 for every page

                xlWorkSheet.PageSetup.PaperSize = app.XlPaperSize.xlPaperLegal;

// Printer Page Size.


                xlWorkSheet.PageSetup.Zoom = false;
                xlWorkSheet.PageSetup.FitToPagesWide = 1;
                xlWorkSheet.PageSetup.FitToPagesTall = 4;

//Set Content according to Page Width and Height. This is important factor if you want to Print Proper documents.

                xlWorkSheet.PageSetup.LeftMargin = 0;
                xlWorkSheet.PageSetup.RightMargin = 0;
                xlWorkSheet.PageSetup.TopMargin = 0.25;
                xlWorkSheet.PageSetup.BottomMargin = 0.25;
                xlWorkSheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;


that's sit.

Let  me know if have any problem.

Enjoy !!!!!

Create/Format Excel File From C#.Net

Create/Format Excel File From C#.Net


Create/Format Excel File From C#.Net
Hello Friend’s
In this article I am going to share “How to create excel file from C# and How to format that Excel file”

1.)    Microsoft.Office.InterPro.Excel namespace use to create excel file.

2.)    If this name space does not exists in your solution then add it from add reference.
3.)    Create Excel Application, Worksheet and Workbook Object as given in below example.


  Microsoft.Office.Interop.Excel.Application xlApp;
  Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
  Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
         Microsoft.Office.Interop.Excel.Range chartRange;
  object misValue = System.Reflection.Missing.Value;
  xlApp = new app.Application();
  xlWorkBook = xlApp.Workbooks.Add(misValue);
  xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


Purpose of misValue object :: if we do not pass the parameter then we can use misValue object.

Now let’s play with excel.
4.)    Below statement write my name to Excel file and also Border around it so its look cool.

xlWorkSheet.Cells[6, 3] = "My Name";
xlWorkSheet.Cells[6, 4] = “Ketan
xlWorkSheet.get_Range("d6", "g6").Merge(true);
xlWorkSheet.get_Range("d6", "g6").BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic);


5.)    Now suppose I want to give some header in excel file. For that Look at below statement.

xlWorkSheet.get_Range("a14", "l14").Merge(false);
chartRange = xlWorkSheet.get_Range("a14", "l14");
chartRange.FormulaR1C1 = "I am Heading";
chartRange.HorizontalAlignment = 3;
chartRange.BorderAround(true);

Above statements merge Excel cells and write statement in it.


6.)    Continue in this Way.  Decide your content position by taking excel positioning in mind. When we write from C# to excel one main point to remember is  Column consider from A-Z  (For more column repeat double letter like aa,ab etc..) and Rows is consider from 1-n.

7.)    You can play with this. You can also create dynamic content in excel by using looping  concept and some logic.

8.)    Below statements set font name and size of excel file.

xlWorkSheet.Cells.Font.Size = 8;
             xlWorkSheet.Cells.Font.Name = "Arial";
     
This two property comes with all cells so if you want to set different font and size for separate cell then you can achive using this properties.

9.)    If you want to format excel cell content position, Alignment Property is there. Let’s look at below example.
                               
xlWorkSheet.Cells[6, 4].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;

xlWorkSheet.Row[9].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;

xlWorkSheet.Cells.VerticalAlignment =
                Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;



       First statement make cell content to Left alignment, second statement make full row to left alignment and third statement make Center vertical Alignment.


10.)         You can also specify Width of the Excel Column.
xlWorkSheet.Columns[1].ColumnWidth = 4.0;

11.)         Below statement set wrap property of excel cell.
       xlWorkSheet.Cells[startingrow, 2].WrapText = true;

12.)         Most of property which applicable to Cell we can also apply to Row(s) of excel.

13.)         Now suppose you want to color one cell of excel.

        xlWorkSheet.Cells[1, 1].Interior.Color = Color.Red;
      
         This will fill red color in cell.

14.)         Now save excel file to physical directory.
    
xlWorkBook.SaveAs(strFilepath, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbookMacroEnabled);

String strFilePath is full path of excel which are going to create.

15.)         Close the excel operation and release all the objects which are used.
  xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();      

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                GC.Collect();
                GC.WaitForPendingFinalizers();