locked
how to create a progressbar

    Question

  • Hi,

    I want to create a progressbar in a winform. My winform contains few btns..process,export are two main btns. When i click process its taking tym to load the page, i want to show that in a progressbar! can anybody show the code for that.!

     public partial class frmRead : Form
        {
            public string pathFolder;
           
            public frmRead()
            {
                InitializeComponent();
    
            }
    private void btnSelect_Click(object sender, EventArgs e)
            {
                try
                {
                    OpenFileDialog path = new OpenFileDialog();
                    //making an object for the OpenFileDialog
        path.Title = "Document Control Register : Inputs";
                    path.InitialDirectory = @"d:\";
                    path.RestoreDirectory = true;
    
                    if (path.ShowDialog() == DialogResult.OK)
                    {
                        textBox1.Text = path.FileName.ToString();
                    }
    
                    pathFolder = System.IO.Path.GetDirectoryName(path.FileName.ToString());
    
                }
                catch (Exception l)
                {
                    MessageBox.Show(l.Message);
                }
    
            }
            public string RemoveNonASCII(string unicode)
            {
                char[] unicodeArray = unicode.ToCharArray();
                string ascii = "";
                for (int i = 0; i < unicodeArray.Length; i++)
                {
                    if (((int)unicodeArray[i]) <= 255) ascii += unicodeArray[i].ToString();
                }
                return ascii;
            }
            private void btnShow_Click(object sender, EventArgs e)
            {
                try
                {
                  //Reading docx file
                    Microsoft.Office.Interop.Word.ApplicationClass wordObject = new Microsoft.Office.Interop.Word.ApplicationClass();
                    object file = textBox1.Text; //this is the path
                    object nullobject = System.Reflection.Missing.Value;
                    Microsoft.Office.Interop.Word._Document doc = wordObject.Documents.Open
                        (ref file, ref nullobject, ref nullobject, ref nullobject,
                        ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                        ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                        ref nullobject, ref nullobject, ref nullobject, ref nullobject
                                        );
                    doc.ActiveWindow.Selection.WholeStory();
                    doc.ActiveWindow.Selection.Copy();
                    IDataObject data = Clipboard.GetDataObject();
                    textBox2.Text = data.GetData(DataFormats.Text).ToString();
                    OleDbConnection oleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=Transmittal.mdb");
                    OleDbCommand oleDbCommand = new OleDbCommand();
                    oleDbCommand.Connection = oleDbConnection;
                    //oleDbCommand.CommandText = "INSERT INTO Register (DRAWINGNO, DRAWINGTITLE)VALUES ('" + table1.Cell(5, 2).Range.Text.ToString() + "', '" + table1.Cell(5, 4).Range.Text.ToString() + "')";
                    oleDbConnection.Open();
                    //oleDbCommand.ExecuteNonQuery();
                    //oleDbConnection.Close();
    
                    string strDRAWINGNO, strREVISION, strDRAWINGTITLE, strWeight, strSTATUS, strREMARKS;
                    Int32 intRows;
                    Table table1 = doc.Tables[1];
                    for (intRows = 1; intRows < table1.Rows.Count; intRows++)
                    {
                       progressBar1.Value = (intRows *table1.Rows.Count)/100;
     strDRAWINGNO = Regex.Replace(table1.Cell(intRows, 2).Range.Text.ToString(), "[^A-Za-z0-9 _-]", ""); //table1.Cell(intRows, 2).Range.Text.ToString();
                        strREVISION = Regex.Replace(table1.Cell(intRows, 3).Range.Text.ToString(), "[^A-Za-z0-9 _-]", ""); //table1.Cell(intRows, 3).Range.Text.ToString();
                        strDRAWINGTITLE = Regex.Replace(table1.Cell(intRows, 4).Range.Text.ToString(), "[^A-Za-z0-9 _-]", ""); //table1.Cell(intRows, 4).Range.Text.ToString();
                        strWeight = Regex.Replace(table1.Cell(intRows, 5).Range.Text.ToString(), "[^A-Za-z0-9.]", ""); //table1.Cell(intRows, 5).Range.Text.ToString();
                        //strSTATUS = table1.Cell(intRows, 6).Range.Text.ToString(); // table1.Cell(intRows, 6).Range.Text.ToString();
    
                        if (table1.Cell(intRows, 6).Range.Text.ToString() == "√\r\a")
                        {
                            strSTATUS = "IFR";
                        }
                        else if (table1.Cell(intRows, 7).Range.Text.ToString() == "√\r\a")
                        {
                            strSTATUS = "IFA";
                        }
                        else if (table1.Cell(intRows, 8).Range.Text.ToString() == "√\r\a")
                        {
                            strSTATUS = "IFC";
                        }
                        else
                        {
                            strSTATUS = "";
                        }
                        strREMARKS = Regex.Replace(table1.Cell(intRows, 9).Range.Text.ToString(), "[^A-Za-z0-9 _-]", ""); //table1.Cell(intRows, 9).Range.Text.ToString();
                        oleDbCommand.CommandText = "INSERT INTO Register (DRAWINGNO,REVISION, DRAWINGTITLE,Weight,STATUS,REMARKS)VALUES ('"
                            + strDRAWINGNO.ToString() + "', '" + strREVISION.ToString() + "', '" + strDRAWINGTITLE.ToString() + "', '" + strWeight.ToString() + "', '" + strSTATUS.ToString() + "','" + strREMARKS.ToString() + "')";
                        oleDbCommand.ExecuteNonQuery();
                        //MessageBox .Show (docs.Tables [1].Cell (intRows,2).Range.Tables[1].ToString ());
                    }
                    oleDbConnection.Close();
                    //wordObject.Documents.Close(ref nullobject, ref nullobject, ref nullobject);
    
                    doc.Close(ref nullobject, ref nullobject, ref nullobject);
                    wordObject.Quit(ref nullobject, ref nullobject, ref nullobject);
                    MessageBox.Show("Process completed successfully!", "Transmittal v1.0.0");
                }
                catch (Exception j)
                {
                    MessageBox.Show(j.Message);
                }
            }
            private void releaseObject(object obj)
            {
                try
                {     System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                    obj = null;
                }
                catch (Exception ex)
                {
                    obj = null;
                    MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
                }
                finally
                {
                    GC.Collect();
                }
            }
            private void btnExport_Click(object sender, EventArgs e)
            {
                Int32 cnt = 1;
                Int32 sno = 0;
    Microsoft.Office.Interop.Excel.Application xlApp;
                Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;
    
                xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                xlWorkBook = xlApp.Workbooks.Add(misValue);
    
                xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Transmittal.mdb");
                thisConnection.Open();
                OleDbCommand thisCommand = thisConnection.CreateCommand();
                thisCommand.CommandText = "SELECT DRAWINGNO,REVISION, DRAWINGTITLE,Weight,STATUS,REMARKS FROM Register ORDER BY ID";
                OleDbDataReader thisReader = thisCommand.ExecuteReader();
                xlWorkSheet.Cells[cnt, 1] = "S.No";
                xlWorkSheet.Cells[cnt, 2] = "DRAWINGNO";
                xlWorkSheet.Cells[cnt, 3] = "REVISION";
                xlWorkSheet.Cells[cnt, 4] = "DRAWINGTITLE";
                xlWorkSheet.Cells[cnt, 5] = "Weight(Kgs)";
                xlWorkSheet.Cells[cnt, 6] = "DocumentTranismittalNo.";
                xlWorkSheet.Cells[cnt, 7] = "SENTDATE";
                xlWorkSheet.Cells[cnt, 8] = "STATUS (IFR/IFA/IFC)";
                xlWorkSheet.Cells[cnt, 9] = "REMARKS";
                while (thisReader.Read())
                {
                    cnt++;
                    sno++;  //MessageBox.Show(thisReader["CodeNumber"].GetType().ToString());
                    xlWorkSheet.Cells[cnt, 1] = sno.ToString();
                    xlWorkSheet.Cells[cnt, 2] = thisReader["DRAWINGNO"].ToString();
                    xlWorkSheet.Cells[cnt, 3] = thisReader["REVISION"].ToString();
                    xlWorkSheet.Cells[cnt, 4] = thisReader["DRAWINGTITLE"].ToString();
                    xlWorkSheet.Cells[cnt, 5] = thisReader["Weight"].ToString();
                    xlWorkSheet.Cells[cnt, 8] = thisReader["STATUS"].ToString();
                    xlWorkSheet.Cells[cnt, 9] = thisReader["REMARKS"].ToString();
                }
                thisReader.Close();
                thisConnection.Close();
                xlWorkBook.SaveAs(pathFolder + "\\DC Register.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();
                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                //releaseObject(xlApp);
                xlApp.Quit();
                MessageBox.Show("Exported data successfully!", "Transmittal v1.0.0");
            }
            private void btnNew_Click(object sender, EventArgs e)
            {
                OleDbConnection thisConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Transmittal.mdb");
                thisConnection.Open();
                OleDbCommand thisCommand = thisConnection.CreateCommand();
                thisCommand.CommandText = "DELETE FROM Register";
                thisCommand.ExecuteNonQuery();
                btnNew.Enabled = false;
            }
            } 
        } 

    This is my winform complete code..Help me with this..

    My other req is here in code im selecting files in browser but now i need to select complete folder..and it should display only the doc file in the explorer and when file processing is done it should disappear. Please help with this


    Tuesday, March 06, 2012 6:53 AM

All replies