How to control the MessageBox in C #

1

My question is how to know if a MessageBox is already open so that they do not open more of the same message ??

private void timer1_Tick(object sender, EventArgs e)
    {
        //panelimgop.Location = new Point(panelimgop.Location.X + -5, panelimgop.Location.Y);
        //if (panelimgop.Location.X > this.Width)
        //{
        //    panelimgop.Location = new Point(0 - panelimgop.Width, panelimgop.Location.Y);
        //}
        string optr = cboperator.Text;
        if (optr != "")
        {
            DialogResult resul = MessageBox.Show("Operator Name : " + formcs.selcopert() + " ", "Operator Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (resul == DialogResult.Yes)
            {
            }
            if (resul == DialogResult.No)
            {
                cboperator.Text = "";
                optr = "";
                lboperator.Text = "";
            }
        }
    }

This is my message to open many times the same.

    
asked by use2105 20.12.2016 в 16:26
source

2 answers

2

The best thing you can do is stop the clock while the MessageBox is open. And start it when the message is closed, if necessary.

private void timer1_Tick(object sender, EventArgs e)
{
    //panelimgop.Location = new Point(panelimgop.Location.X + -5, panelimgop.Location.Y);
    //if (panelimgop.Location.X > this.Width)
    //{
    //    panelimgop.Location = new Point(0 - panelimgop.Width, panelimgop.Location.Y);
    //}
    string optr = cboperator.Text;
    if (optr != "")
    {
        timer1.Enabled = false;
        DialogResult resul = MessageBox.Show("Operator Name : " + formcs.selcopert() + " ", "Operator Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
        if (resul == DialogResult.Yes)
        {
        }
        if (resul == DialogResult.No)
        {
            cboperator.Text = "";
            optr = "";
            lboperator.Text = "";
        }
        timer1.Enabled = true;
    }
}

Another option would be to declare a variable:

private bool MsgBoxAbierto = false;

And change the value at the time of opening the dialogue.

private void timer1_Tick(object sender, EventArgs e)
{
    //panelimgop.Location = new Point(panelimgop.Location.X + -5, panelimgop.Location.Y);
    //if (panelimgop.Location.X > this.Width)
    //{
    //    panelimgop.Location = new Point(0 - panelimgop.Width, panelimgop.Location.Y);
    //}
    string optr = cboperator.Text;
    if (optr != "" and this.MsgBoxAbierto == false)
    {
        this.MsgBoxAbierto = true;
        DialogResult resul = MessageBox.Show("Operator Name : " + formcs.selcopert() + " ", "Operator Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
        if (resul == DialogResult.Yes)
        {
        }
        if (resul == DialogResult.No)
        {
            cboperator.Text = "";
            optr = "";
            lboperator.Text = "";
        }
        this.MsgBoxAbierto = false;
    }
}
    
answered by 20.12.2016 / 16:33
source
1

Why do not you control it with a Boolean variable?

private bool isMessageBoxShown;

private void timer1_Tick(object sender, EventArgs e)
{
    string optr = cboperator.Text;
    if (optr != "" && !isMessageBoxShown)
    {
        isMessageBoxShown = true;
        timer1.Enabled = false;
        DialogResult resul = MessageBox.Show("Operator Name : " + formcs.selcopert() + " ", "Operator Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
        if (resul == DialogResult.Yes)
        {
        }
        if (resul == DialogResult.No)
        {
            cboperator.Text = "";
            optr = "";
            lboperator.Text = "";
        }
        timer1.Enabled = true;
        isMessageBoxShown = false;
    }
}
    
answered by 20.12.2016 в 16:38