2 u011475211 u011475211 于 2016.05.04 16:15 提问

c#弹出一个消息框,3秒后自动消失
c#

发现c#中MessageBox没有构造函数,无法生成对象,就无法销毁对象,请问如何实现这个功能?

7个回答

caozhy
caozhy   Ds   Rxr 2016.05.21 04:17
已采纳
EAison
EAison   2016.05.04 16:39

这种问题狗无聊的。 。

CSDNXIAON
CSDNXIAON   2016.05.04 16:22

jQuery实现点击弹出层3秒后自动消失
winform C#屏幕右下角弹出消息框,自动消失
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

chenqixia
chenqixia   2016.05.04 16:58

使用Form自定义一个MessageBox,使用定时器再

Chenrongsake
Chenrongsake   2016.05.06 12:02

当窗口显示的时候开始获取系统时间,然后开始计时,当后一次获取的系统时间减去第一次获取的系统时间得到的结果为3秒时此时执行关闭窗口动作

u012758945
u012758945   2016.05.04 18:22

像这种情况只能自己写一个类型,继承Form的,我简单写了一个,参考下。
需要注意的地方是要窗体创建句柄之后(调用Show或者ShowDialog方法后),才能访问某些窗体的方法。
using System;
using System.Threading;
using System.Windows.Forms;

namespace WindowsForm
{
public class MessageBoxTimer : Form
{
private void InitializeComponent()
{
this.button1 = new Button();
this.button2 = new Button();
this.label1 = new Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(51, 127);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "确定";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new EventHandler(button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(154, 127);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "取消";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new EventHandler(button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(49, 33);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 12);
this.label1.TabIndex = 2;
//
// MessageBoxTimer
//
this.ClientSize = new System.Drawing.Size(284, 162);
this.Controls.Add(label1);
this.Controls.Add(button2);
this.Controls.Add(button1);
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(300, 200);
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(300, 200);
this.Name = "MessageBoxTimer";
this.ResumeLayout(false);
this.PerformLayout();

    }

    public MessageBoxTimer()
    {
        InitializeComponent();
        Text = "提示";
    }
    private Button button1;
    private Label label1;
    private Button button2;

    private delegate void SetTimeoutInfo(int timeover);

    private delegate void NoParameterDelegate();

    private string Title;
    /// <summary>
    /// 显示指定时间
    /// </summary>
    /// <param name="timeout"></param>
    public void ShowDialog(int timeout)
    {
        Title = Text;
        Text = "(" + timeout + "秒后关闭)" + Title;
        SetTimeoutInfo showMethod = ShowTimeout;
        showMethod.BeginInvoke(timeout, TimeoutCallback, timeout);
        DialogResult = ShowDialog();
    }
    /// <summary>
    /// 计时方法
    /// </summary>
    /// <param name="timeout"></param>
    private void ShowTimeout(int timeout)
    {
        SetTimeoutInfo setText = TimeoutInfo;
        while (timeout-- > 0)
        {
            Thread.Sleep(1000);
            if (IsHandleCreated) { Invoke(setText, timeout); }
        }
    }
    /// <summary>
    /// 显示内容
    /// </summary>
    public string Content { get { return label1.Text; } set { label1.Text = value; } }
    /// <summary>
    /// 显示超时信息
    /// </summary>
    /// <param name="timeout"></param>
    private void TimeoutInfo(int timeout)
    {
        Text = "(" + timeout + "秒后关闭)" + Title;
    }
    /// <summary>
    /// 计时完成回调
    /// </summary>
    /// <param name="result"></param>
    private void TimeoutCallback(IAsyncResult result)
    {
        DialogResult = DialogResult.None;
        if (IsHandleCreated) Invoke(new NoParameterDelegate(Hide));
    }
    /// <summary>
    /// 确定按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.OK;
        Invoke(new NoParameterDelegate(Hide));
    }
    /// <summary>
    /// 取消按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button2_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.Cancel;
        Invoke(new NoParameterDelegate(Hide));
    }
}

}


u012758945
u012758945 回复caoxwei2: 我上面没有用任何计时的类型,只是通过异步执行计时方法,如果你不理解委托、异步、回调等这些操作,建议你使用Timer类来完成计时功能。
一年多之前 回复
u011475211
u011475211 我原本敲了一遍出现问题,shoeMethod.BeginInvoke(timeout,TimerCallback,timeout),报错,无法从System.Threading.TimerCallback转为System.AsyncCallback
一年多之前 回复
ActionSnail
ActionSnail   2016.05.16 17:37

自己创建一个窗体,就把那个窗体当初弹出框,拖个Timer控件
// 自动关闭的时间限制,如3为3秒后自动关闭
private int second=3;
// 计数器,用以判断当前窗口弹出后持续的时间
private int counter;
public TimingMessageBox()
{
InitializeComponent();
}

    public TimingMessageBox(string message, int second)
    {
        InitializeComponent();
        // 显示消息
        this.labelMessage.Text = message;
        this.second = second;
    }

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

    private void TimingMessageBox_Load(object sender, EventArgs e)
    {
       // this.labelMessage.Text = "请将衣服放置于门板上!";
        // 获得时间限制
        this.second = second;
        // 初始化计数器
        this.counter = 0;
        // 初始化按钮的文本
       // this.buttonOK.Text = string.Format("确定({0})", this.second - this.counter);
        this.buttonOK.Text = string.Format("确定");
        this.timer1.Enabled = true;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        this.timer1.Interval = 1000;
        this.timer1.Start();
    }

    private void buttonOK_Click_1(object sender, EventArgs e)
    {
        // 单击确定按钮,关闭对话框
        this.Close();
    }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!