问题描述
- c#弹出一个消息框,3秒后自动消失
-
发现c#中MessageBox没有构造函数,无法生成对象,就无法销毁对象,请问如何实现这个功能?
解决方案
http://nxhujiee.blog.163.com/blog/static/298444220155238351302/
http://www.cnblogs.com/ap0606122/archive/2012/10/23/2735325.html
http://blog.csdn.net/huangshunle/article/details/8553283
解决方案二:
jQuery实现点击弹出层3秒后自动消失
winform C#屏幕右下角弹出消息框,自动消失
解决方案三:
这种问题狗无聊的。 。
解决方案四:
使用Form自定义一个MessageBox,使用定时器再
解决方案五:
当窗口显示的时候开始获取系统时间,然后开始计时,当后一次获取的系统时间减去第一次获取的系统时间得到的结果为3秒时此时执行关闭窗口动作
解决方案六:
像这种情况只能自己写一个类型,继承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));
}
}
}
解决方案七:
自己创建一个窗体,就把那个窗体当初弹出框,拖个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();
}