locked
Form load with mulitple combox fill????? RRS feed

  • Question

  • Hi,
        In my few form a hav lots of combox and i have to load them in form_load().

    But it take time to to execute all the methods so form opens after few sec.

    So can any one suggest me what would be best way to handle this situation??

    Thanks
    Abhishek
    Tuesday, September 22, 2009 5:20 AM

Answers

  • Hi,
    Sample For You...
    i hope it will help you
        public Form1()
            {
                InitializeComponent();
                InitWorker();
            }
            BackgroundWorker backgroundWorker;
            public delegate void AddComboItem();
            public AddComboItem myDelegate;
           
            private void Form1_Load(object sender, EventArgs e)
            {
                backgroundWorker.RunWorkerAsync();
                backgroundWorker.WorkerReportsProgress = true;
                myDelegate = new AddComboItem(LoadCombo); 
                //LoadCombo();
            }
            private void InitWorker()
            {
                backgroundWorker = new BackgroundWorker();
                backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
                backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
                backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
            }
    
            void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                progressBar1.Value = e.ProgressPercentage;
            }
           
    
            void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    MessageBox.Show(e.Error.Message);
                }          
                else
                {                
                    MessageBox.Show("Successfully Loaded");
                }
    
            }
    
            void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                BackgroundWorker worker = sender as BackgroundWorker;
                e.Result = LoadAllCombo(worker, e);
    
            }
            private int LoadAllCombo(BackgroundWorker worker, DoWorkEventArgs e)
            {
    
                worker.ReportProgress(1, "Loading");
                DataSet ds = new DataSet();
                ds = CreateDataSet();
                worker.ReportProgress(20, "Loading");
                comboBox1.Invoke(this.myDelegate);
                worker.ReportProgress(100, "Loaded");
                int result=0;
               
               
                  
                  
              
                return result;
            }
            
            private void LoadCombo()
            {
                DataSet ds = new DataSet();
                ds = CreateDataSet();                        
                comboBox1.DataSource = ds.Tables[0];
                comboBox1.DisplayMember = "CustName";
                comboBox1.ValueMember = "CustID";
    
                comboBox2.DataSource = ds.Tables[0];
                comboBox2.DisplayMember = "CustName";
                comboBox2.ValueMember = "CustID";
    
                comboBox3.DataSource = ds.Tables[0];
                comboBox3.DisplayMember = "CustName";
                comboBox3.ValueMember = "CustID";
    
                comboBox4.DataSource = ds.Tables[0];
                comboBox4.DisplayMember = "CustName";
                comboBox4.ValueMember = "CustID";
               
            }
            private DataSet CreateDataSet()
            {
                DataTable table = new DataTable("childTable");
                DataColumn column;
                DataRow row;
                DataSet dataset = new DataSet();        
              
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.String");
                column.ColumnName = "CustName";         
                column.Caption = "Name";
                table.Columns.Add(column);
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int32");
                column.ColumnName = "CustID";           
                column.Caption = "ID";           
                table.Columns.Add(column);        
              
              
                dataset.Tables.Add(table);
    
             
                for (int i = 0; i <= 10000; i++)
                {
                    row = table.NewRow();               
                    row["CustName"] = "Item " + i;
                    row["CustID"] = i;
                    table.Rows.Add(row);
                }
                return dataset;
    
            }

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    • Proposed as answer by Gnanadurai Wednesday, September 23, 2009 11:12 AM
    • Marked as answer by Jing0 Saturday, September 26, 2009 4:18 AM
    Wednesday, September 23, 2009 9:19 AM

All replies

  • Hi,
    use background worker to load all combobox asynchronously. i hope it will help you.
    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    • Proposed as answer by Prasanta Swain Tuesday, September 22, 2009 5:40 AM
    • Unproposed as answer by abhi0410 Tuesday, September 22, 2009 6:02 AM
    Tuesday, September 22, 2009 5:25 AM
  • Hi,
    Sample For You...
    i hope it will help you
        public Form1()
            {
                InitializeComponent();
                InitWorker();
            }
            BackgroundWorker backgroundWorker;
            public delegate void AddComboItem();
            public AddComboItem myDelegate;
           
            private void Form1_Load(object sender, EventArgs e)
            {
                backgroundWorker.RunWorkerAsync();
                backgroundWorker.WorkerReportsProgress = true;
                myDelegate = new AddComboItem(LoadCombo); 
                //LoadCombo();
            }
            private void InitWorker()
            {
                backgroundWorker = new BackgroundWorker();
                backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
                backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
                backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
            }
    
            void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                progressBar1.Value = e.ProgressPercentage;
            }
           
    
            void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    MessageBox.Show(e.Error.Message);
                }          
                else
                {                
                    MessageBox.Show("Successfully Loaded");
                }
    
            }
    
            void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                BackgroundWorker worker = sender as BackgroundWorker;
                e.Result = LoadAllCombo(worker, e);
    
            }
            private int LoadAllCombo(BackgroundWorker worker, DoWorkEventArgs e)
            {
    
                worker.ReportProgress(1, "Loading");
                DataSet ds = new DataSet();
                ds = CreateDataSet();
                worker.ReportProgress(20, "Loading");
                comboBox1.Invoke(this.myDelegate);
                worker.ReportProgress(100, "Loaded");
                int result=0;
               
               
                  
                  
              
                return result;
            }
            
            private void LoadCombo()
            {
                DataSet ds = new DataSet();
                ds = CreateDataSet();                        
                comboBox1.DataSource = ds.Tables[0];
                comboBox1.DisplayMember = "CustName";
                comboBox1.ValueMember = "CustID";
    
                comboBox2.DataSource = ds.Tables[0];
                comboBox2.DisplayMember = "CustName";
                comboBox2.ValueMember = "CustID";
    
                comboBox3.DataSource = ds.Tables[0];
                comboBox3.DisplayMember = "CustName";
                comboBox3.ValueMember = "CustID";
    
                comboBox4.DataSource = ds.Tables[0];
                comboBox4.DisplayMember = "CustName";
                comboBox4.ValueMember = "CustID";
               
            }
            private DataSet CreateDataSet()
            {
                DataTable table = new DataTable("childTable");
                DataColumn column;
                DataRow row;
                DataSet dataset = new DataSet();        
              
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.String");
                column.ColumnName = "CustName";         
                column.Caption = "Name";
                table.Columns.Add(column);
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int32");
                column.ColumnName = "CustID";           
                column.Caption = "ID";           
                table.Columns.Add(column);        
              
              
                dataset.Tables.Add(table);
    
             
                for (int i = 0; i <= 10000; i++)
                {
                    row = table.NewRow();               
                    row["CustName"] = "Item " + i;
                    row["CustID"] = i;
                    table.Rows.Add(row);
                }
                return dataset;
    
            }

    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    • Proposed as answer by Gnanadurai Wednesday, September 23, 2009 11:12 AM
    • Marked as answer by Jing0 Saturday, September 26, 2009 4:18 AM
    Wednesday, September 23, 2009 9:19 AM