Force to close MessageBox programmatically

后端 未结 11 2125
清酒与你
清酒与你 2020-12-03 07:21

Let me give you the background.

We have an Application(medium sized) that is using MessageBox.Show (....) at various places (in hundreds).

These message bo

11条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 08:13

    The easiest solution is to create a form that will close on timer_tick

    private int interval = 0;
    private string message = "";
    
    public msgBox(string msg = "", int i = 0)
    {
        InitializeComponent();
        interval = i;
        message = msg;
    }
    
    private void MsgBox_Load(object sender, EventArgs e)
    {
        if (interval > 0)
            timer1.Interval = interval;
    
        lblMessage.Text = message;
        lblMessage.Width = panel1.Width - 20;
        lblMessage.Left = 10;
    }
    
    private void Timer1_Tick(object sender, EventArgs e)
    {
        this.Close();
    }
    
    private void Panel1_Paint(object sender, PaintEventArgs e)
    {
        ControlPaint.DrawBorder(e.Graphics, this.panel1.ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Solid);
    }
    

    Method to use in main form

       private void showMessage(string msg, int interval = 0)
        {
            msgBox mb = new msgBox(msg, interval);
            mb.ShowDialog(this);
        }
    

    Call it

      showMessage("File saved");
    

提交回复
热议问题