Search code examples
devexpressprogress-barmessagebox

Devexpress MessageBox is closing automatically


I have a Devexpress form which is having Progress bar and MessageBox. When i click search button on my form,the progress bar displays percentage and then grid will be bind with records. and when it returns no records, message box will appear after progress bar like "No Records Found"

but after showing No records found,it is closing automatically with out clicking OK or CANCEL button on messagebox

Code for Progress Bar

namespace DMS
{
public partial class ProgressBar : WaitForm
{
    public ProgressBar()
    {
        InitializeComponent();
    }

    #region Overrides
    public override void SetCaption(string caption)
    {
        base.SetCaption(caption);
        this.progressPanel1.Caption = caption;
    }
    public override void SetDescription(string description)
    {
        base.SetDescription(description);
        this.progressPanel1.Description = description;
    }
    public override void ProcessCommand(Enum cmd, object arg)
    {
        base.ProcessCommand(cmd, arg);
    }
    #endregion

    private void progressBarControl1_EditValueChanged(object sender, EventArgs e)
    {
        try
        {
            int i = int.Parse(Math.Floor(double.Parse(this.progressBarControl1.EditValue.ToString())).ToString());                
            if (i.ToString().EndsWith("0"))
            {
                //satya
                //i += 5;
                i += 0;
            }
            this.Opacity = 100;
            SetCaption("Please Wait...");
            SetDescription(i.ToString() + "% Completed...");
            Application.DoEvents();
        }
        catch (Exception ex)
        {
            XtraMessageBox.Show(ex.Message.ToString() + " " + ex.Source.ToString());
        }
    }
}

}

Code for Showing Message Box

private void btnshow_Click(object sender, EventArgs e)
    {
        try
        {
            if (dtstartdate.DateTime.Date > dtEnddate.DateTime.Date)
            {
                XtraMessageBox.Show("Start date should be less than End date", Program.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtstartdate.Focus();
                return;
            }
            string CommandText = "select * from DMS_View_Payments where v_Paymentdate between '" + General.DateToDatabase(this.dtstartdate.DateTime.ToShortDateString())
                + "' and '" + General.DateToDatabase(this.dtEnddate.DateTime.ToShortDateString()) + "'";
            if (ccbeForClient.EditValue.ToString() != string.Empty)
            {
                CommandText += " and v_clientid in (" + this.ccbeForClient.EditValue.ToString() + ")";
            }
            else if (Logs.UserLimitedClients != "0")
            {
                CommandText += " and v_clientid in (" + Logs.UserLimitedClients + ")";
            }
            if (ccbeForBatch.Text.Trim() != "" && ccbeForBatch.Text.Trim() != "--")
            {
                string selected_client_list = "";
                if (ccbeForBatch.Text.Trim() != "" && ccbeForBatch.Text.Trim() != "--")
                {
                    //selected_client_list = ccbe_client_list.Text.Replace(" ", "").Replace(",", "','");
                    string source = ccbeForBatch.Text;
                    string[] split = source.Split(',');
                    string changed = "";
                    for (int i = 0; i < split.Length; i++)
                    {
                        changed += "," + split[i].Trim();
                    }
                    //string output = changed.Trim(',').Replace(",", "','");
                    selected_client_list = changed.Trim(',').Replace(",", "','");
                }
                else
                {
                    selected_client_list = "";
                }
                CommandText += " and v_Batch in ('" + selected_client_list + "') ";
            }
            if (ccbeForBranch.EditValue.ToString() != string.Empty)
            {
                CommandText += " and v_BRANCH_ID in (" + ccbeForBranch.EditValue.ToString() + ")";
            }
            if (ccbeForCollector.EditValue.ToString() != string.Empty)
            {
                CommandText += " and V_CollectorId in (" + ccbeForCollector.EditValue.ToString() + ")";
            }
            else if (DevXCharts.EmpIdsForHierarchey != "")
            {
                CommandText += " and V_CollectorId in (" + DevXCharts.EmpIdsForHierarchey.ToString() + ")";
            }
            if (ccbeForAccountType.Text.Trim() != "" && ccbeForAccountType.Text.Trim() != "--")
            {
                string selected_client_list = "";
                if (ccbeForAccountType.Text.Trim() != "" && ccbeForAccountType.Text.Trim() != "--")
                {
                    //selected_client_list = ccbe_client_list.Text.Replace(" ", "").Replace(",", "','");
                    string source = ccbeForAccountType.Text;
                    string[] split = source.Split(',');
                    string changed = "";
                    for (int i = 0; i < split.Length; i++)
                    {
                        changed += "," + split[i].Trim();
                    }
                    //string output = changed.Trim(',').Replace(",", "','");
                    selected_client_list = changed.Trim(',').Replace(",", "','");
                }
                else
                {
                    selected_client_list = "";
                }
                CommandText += " and v_AccountType in ('" + selected_client_list + "') ";
            }
            if (radioPayment.SelectedIndex == 0)
            {
                CommandText += " and v_Confirmed='Y'";
            }
            else if (radioPayment.SelectedIndex == 1)
            {
                CommandText += " and v_Confirmed='N'";
            }
            if (radioCases.SelectedIndex == 0)
            {
                CommandText += " and v_flagabort='0'";
            }
            else if (radioCases.SelectedIndex == 1)
            {
                CommandText += " and v_flagabort='1'";
            }
            CommandText += " order by v_debtorname";
            Application.DoEvents();
            if (objPB != null)
            {
                objPB.Close();
                objPB = null;
            }
            objPB = new ProgressBar();
            objPB.StartPosition = FormStartPosition.CenterScreen;
            objPB.progressBarControl1.EditValue = 20;
            objPB.TopMost = true;
            objPB.ShowInTaskbar = false;
            Application.DoEvents();
            objPB.Show();
            Application.DoEvents();
            SqlDataAdapter sql_for_debtors = new SqlDataAdapter(CommandText, DBConString.ConnectionString());
            DataSet ds_for_rhb = new DataSet();
            sql_for_debtors.SelectCommand.CommandTimeout = 0;
            sql_for_debtors.Fill(ds_for_rhb, "DMS_View_Payments");
            sql_for_debtors.Dispose();
            Application.DoEvents();
            objPB.progressBarControl1.EditValue = 40;
            Application.DoEvents();
            if (ds_for_rhb.Tables["DMS_View_Payments"].Rows.Count > 0)
            {
                XrtPaymentsDevx obj_report = new XrtPaymentsDevx();
                obj_report.xrlReportHeader.Text = "Payments From " + this.dtstartdate.DateTime.ToString("dd/MM/yyyy") + " To " + this.dtEnddate.DateTime.ToString("dd/MM/yyyy");
                string SelectionCriteria = "";
                if (this.ccbeForClient.Text != "" && this.ccbeForClient.Text != "--")
                {
                    SelectionCriteria += " Client: " + this.ccbeForClient.Text + " , ";
                }
                if (this.ccbeForBatch.Text != "" && this.ccbeForBatch.Text != "--")
                {
                    SelectionCriteria += " Batch: " + this.ccbeForBatch.Text + " , ";
                }
                if (this.ccbeForBranch.Text != "" && this.ccbeForBranch.Text != "--")
                {
                    SelectionCriteria += " Branch: " + this.ccbeForBranch.Text + " , ";
                }
                if (this.ccbeForCollector.Text != "" && this.ccbeForCollector.Text != "--")
                {
                    SelectionCriteria += " Collector: " + this.ccbeForCollector.Text + " , ";
                }
                if (this.ccbeForAccountType.Text != "" && this.ccbeForAccountType.Text != "--")
                {
                    SelectionCriteria += " AccType: " + this.ccbeForAccountType.Text + " , ";
                }
                if (radioPayment.SelectedIndex == 0)
                {
                    SelectionCriteria += " Payment Confirmed='Y'" + " , ";
                }
                else if (radioPayment.SelectedIndex == 1)
                {
                    SelectionCriteria += " Payment Confirmed='N'" + " , ";
                }
                else if (radioPayment.SelectedIndex == 2)
                {
                    SelectionCriteria += " Payment Confirmed='Y' And 'N'" + " , ";
                }
                if (radioCases.SelectedIndex == 1)
                {
                    SelectionCriteria += " Abort Cases" + " , ";
                }
                else if (radioCases.SelectedIndex == 0)
                {
                    SelectionCriteria += " Active Cases" + " , ";
                }
                else if (radioCases.SelectedIndex == 2)
                {
                    SelectionCriteria += " Both Active And Abort Cases" + " , ";
                }
                obj_report.xrlSelectionCriteria.Text = SelectionCriteria;
                obj_report.DataSource = ds_for_rhb;
                obj_report.DataMember = "DMS_View_Payments";
                Application.DoEvents();
                objPB.progressBarControl1.EditValue = 60;
                Application.DoEvents();
                obj_report.CreateDataBindings();
                Application.DoEvents();
                objPB.progressBarControl1.EditValue = 80;
                Application.DoEvents();
                //obj_report.bind_data();
                obj_report.ShowPreview();
                Application.DoEvents();
                objPB.progressBarControl1.EditValue = 90;
                Application.DoEvents();
                Application.DoEvents();
                objPB.progressBarControl1.EditValue = 100;
                Application.DoEvents();
                objPB.Close();
                Application.DoEvents();
            }
            else
            {
                XtraMessageBox.Show("No Records(s) Found", Program.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                objPB.progressBarControl1.EditValue = 100;                    
                //objPB.progressBarControl1.EditValue = 100;
                Application.DoEvents();
                objPB.Close();

                //Application.DoEvents();




                //objPB.progressBarControl1.EditValue = 100;                    
                //Application.DoEvents();
                //objPB.Close();                    
                //XtraMessageBox.Show("No Records(s) Found", Program.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        catch (Exception ex)
        {
            XtraMessageBox.Show(ex.Message.ToString() + " " + ex.Source.ToString(), Program.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

Solution

  • The reason of the issue is incorrect usage of WaitForm instance.
    You should'not show/hide the WaitForm directly rather then use the SplashScreenManager API to show/hide and interact with WaitForm.
    Here i'll try to get you correct approach:

    At first, the code for running search operation with WaitForm showing should looks like this:

    void OnRunSearchOperation_ButtonClick(object sender, EventArgs e) {
        bool success = false;
        SplashScreenManager.ShowForm(this, typeof(ProgressWaitForm), false, false); 
        try {
            // here you can run some long time operations
            success = SearchOperation();
        }
        finally { SplashScreenManager.CloseForm(); }
        if(!success) {
            XtraMessageBox.Show(this, "No entries found");
        }
    }
    bool SearchOperation() {
        // THIS IS SEARCH OPERATION IMITATION 
        const int count = 70000000;
        int stepThreshold = count / 100;
        decimal dec = 1.41M;
        for(int i = 0; i < count; i++) {
            dec /= 2;
            if((i % stepThreshold) == 0) 
                SplashScreenManager.Default.SendCommand(WaitFormCommand.PerformStep, null);
        }
        return false; // result
    }
    

    Here are the WaitFormCommand and 'ProgressWaitForm' implementation:

    public enum WaitFormCommand {
        PerformStep
    }
    public partial class ProgressWaitForm : WaitForm {
        public ProgressWaitForm() {
            InitializeComponent();
        }
        #region Overrides
        public override void ProcessCommand(Enum cmd, object arg) {
            base.ProcessCommand(cmd, arg);
            WaitFormCommand wfCmd = (WaitFormCommand)cmd;
            if(wfCmd == WaitFormCommand.PerformStep) 
                progressBarControl1.PerformStep();
        }
        #endregion
    }