C# MessageBox.Show()怎么设置3秒后 自动关闭?

2025-02-24 04:03:00
推荐回答(3个)
回答1:

写好了,以下是截图和部分源码,完整的源码在附件中:

1.指定要弹出的消息以及定时的时间(单位秒)

2.弹出后,对话框上的确定按钮上会动态倒计时,当时间为0时自动关闭,也可以通过点击确定按钮关闭

核心代码:

 public partial class TimingMessageBox : Form
    {
        // 自动关闭的时间限制,如3为3秒后自动关闭
        private int second;
        // 计数器,用以判断当前窗口弹出后持续的时间
        private int counter;

        // 构造函数
        public TimingMessageBox(string message, int second)
        {
            InitializeComponent();
            // 显示消息
            this.labelMessage.Text = message;
            // 获得时间限制
            this.second = second;
            // 初始化计数器
            this.counter = 0;
            // 初始化按钮的文本
            this.buttonOK.Text = string.Format("确定({0})", this.second - this.counter);
            // 激活并启动timer,设置timer的触发间隔为1000毫秒(1秒)
            this.timer1.Enabled = true;
            this.timer1.Interval = 1000;
            this.timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // 如果没有到达指定的时间限制
            if (this.counter <= this.second)
            {
                // 刷新按钮的文本
                this.buttonOK.Text = string.Format("确定({0})", this.second - this.counter);
                this.Refresh();
                // 计数器自增
                this.counter++;
            }
            // 如果到达时间限制
            else
            {
                // 关闭timer
                this.timer1.Enabled = false;
                this.timer1.Stop();
                // 关闭对话框
                this.Close();
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            // 单击确定按钮,关闭对话框
            this.Close();
        }
    }

然后在主窗体中调用:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonShowMessageBox_Click(object sender, EventArgs e)
        {
            string message = this.textBoxMessage.Text.Trim();
            int second = Convert.ToInt32(this.textBoxSecond.Text.Trim());
            TimingMessageBox messageBox=new TimingMessageBox(message,second);
            messageBox.ShowDialog();
        }
    }


回答2:

MessageBox.Show不支持你说的功能,不过你为什么不用一个放置在最前端的Label来实现呢?我现在的项目就是这么做的,你用ShowMsg方法代替MessageBox.Show,代码参考:

void ShowMsg(string msg)
{
    new Thread(() =>
                   {
                       TimeSpan ts = new TimeSpan(0, 0, 0, 1);
                       for (int i = 3; i > 0; i--)
                       {
                           // 如果强制不显示,则终止循环显示
                           if (_forceVisible)
                           {
                               _forceVisible = false;
                               return;
                           }
                           OperationLabelMethod(labTitle, msg + "\r\n" + i + "秒后关闭");
                           Thread.Sleep(ts);
                       }
                       OperationLabelMethod(labTitle, null);
                   }).Start();
    //MessageBox.Show(msg);
}
delegate void OperationLabel(Label lab, string txt);
/// 
/// 通过委托方法设置或隐藏Label
/// 

/// 
/// 
void OperationLabelMethod(Label lab, string txt)
{
    if (lab.InvokeRequired)
    {
        OperationLabel method = OperationLabelMethod;
        if(!this.IsDisposed)// 点保存,然后马上关闭窗体时,会导致this变成null了,所以这里要判断
            Invoke(method, lab, txt);
    }
    else
    {
        if (string.IsNullOrEmpty(txt))
        {
            lab.Text = string.Empty;
            lab.Visible = false;
        }
        else
        {
            lab.Text = txt;
            lab.Visible = true;
        }
    }
}

回答3:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AutoClosingMessageBox.Show("Text", "Caption", 1000);
        }
    }

    public class AutoClosingMessageBox
    {
        System.Threading.Timer _timeoutTimer;
        string _caption;
        AutoClosingMessageBox(string text, string caption, int timeout)
        {
            _caption = caption;
            _timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
                null, timeout, System.Threading.Timeout.Infinite);
            MessageBox.Show(text, caption);
        }
        public static void Show(string text, string caption, int timeout)
        {
            new AutoClosingMessageBox(text, caption, timeout);
        }
        void OnTimerElapsed(object state)
        {
            IntPtr mbWnd = FindWindow(null, _caption);
            if (mbWnd != IntPtr.Zero)
                SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
            _timeoutTimer.Dispose();
        }
        const int WM_CLOSE = 0x0010;
        [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
    }