C# 自动关闭消息框
我有一个程序,我使用定时器重定向到另一个页面。它确实可以工作,但问题是当我单击“取消”按钮时,会出现一个messagebox,当用户不单击它并且计时器滴答作响时,messagebox没有关闭。如何自动关闭消息框 这就是它看起来的样子 这是我用来重定向页面的代码C# 自动关闭消息框,c#,timer,messagebox,auto-close,C#,Timer,Messagebox,Auto Close,我有一个程序,我使用定时器重定向到另一个页面。它确实可以工作,但问题是当我单击“取消”按钮时,会出现一个messagebox,当用户不单击它并且计时器滴答作响时,messagebox没有关闭。如何自动关闭消息框 这就是它看起来的样子 这是我用来重定向页面的代码 DispatcherTimer sessionTimer = new DispatcherTimer(); public CashDepositAccount() { InitializeCompone
DispatcherTimer sessionTimer = new DispatcherTimer();
public CashDepositAccount()
{
InitializeComponent();
con = new SqlConnection(ConfigurationManager.ConnectionStrings["kiosk_dbConnectionString1"].ConnectionString);
con.Open();
SqlCommand cmd1 = new SqlCommand("Select idle From [dbo].[Idle]", con);
idle = Convert.ToInt32(cmd1.ExecuteScalar());
InputManager.Current.PreProcessInput += Activity;
activityTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMinutes(idle),
IsEnabled = true
};
activityTimer.Tick += Inactivity;
}
#region
void Inactivity(object sender, EventArgs e)
{
navigate = "Home";
Application.Current.Properties["navigate"] = navigate;
}
void Activity(object sender, PreProcessInputEventArgs e)
{
activityTimer.Stop();
activityTimer.Start();
}
当计时器滴答作响时,如何在重定向到主页面时关闭消息框?我已使用此代码关闭消息框,而无需创建新表单。这对我来说确实很好。也许对你们也有帮助。我是从你那儿得到的
使用AutoClosingMessageBox.Show(这是“您的消息…”,1500)代码>。代码如下:
class AutoClosingMessageBox
{
private static System.Threading.Timer Tmr;
public static void Show(Form Parent, string text, int timeout)
{
Form mbx = new Form();
Label LblMessage = new Label();
#region InitializeComponent
mbx.Size = new System.Drawing.Size(308, 185);
mbx.MaximizeBox = false;
mbx.MinimizeBox = false;
mbx.ShowIcon = false;
mbx.ShowInTaskbar = false;
mbx.ControlBox = false;
mbx.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
mbx.FormBorderStyle = FormBorderStyle.None;
mbx.StartPosition = FormStartPosition.CenterScreen;
#region Center on Parent StartPosition
if (Parent != null)
{
mbx.BackColor = Parent.BackColor;
mbx.StartPosition = FormStartPosition.Manual;
int X = Parent.Location.X + ((Parent.Width - mbx.Width) / 2);
int Y = Parent.Location.Y + ((Parent.Height - mbx.Height) / 2);
mbx.Location = new System.Drawing.Point(X, Y);
}
#endregion
//
//LblMessage
//
LblMessage.Location = new System.Drawing.Point(12, 23);
LblMessage.AutoSize = false;
LblMessage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
LblMessage.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
LblMessage.ForeColor = mbx.BackColor.ToInvert();
LblMessage.BorderStyle = BorderStyle.FixedSingle;
LblMessage.Text = text;
LblMessage.Dock = DockStyle.Fill;
mbx.Controls.Add(LblMessage);
#endregion
Tmr = new System.Threading.Timer(new System.Threading.TimerCallback(Tmr_Tick), mbx, timeout, 0);
mbx.ShowDialog();
}
private static void Tmr_Tick(object obj)
{
Tmr.Dispose();
if (obj is Form)
{
if (((Form)obj).InvokeRequired)
{
((Form)obj).Invoke(new System.Action<Form>(InvokeMbx), new object[] { ((Form)obj) });
}
else InvokeMbx((Form)obj);
}
}
private static void InvokeMbx(Form mbx)
{
mbx.Close();
}
}
类自动关闭MessageBox
{
专用静态System.Threading.Timer Tmr;
公共静态无效显示(窗体父级、字符串文本、int超时)
{
表单mbx=新表单();
Label LblMessage=新标签();
#区域初始化组件
mbx.尺寸=新系统图纸尺寸(308185);
mbx.ebox=假;
mbx.ebox=假;
mbx.ShowIcon=false;
mbx.ShowInTaskbar=false;
mbx.ControlBox=false;
mbx.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Dpi;
mbx.FormBorderStyle=FormBorderStyle.None;
mbx.StartPosition=FormStartPosition.CenterScreen;
#父起始位置上的区域中心
如果(父项!=null)
{
mbx.BackColor=Parent.BackColor;
mbx.StartPosition=FormStartPosition.Manual;
intx=Parent.Location.X+((Parent.Width-mbx.Width)/2);
int Y=Parent.Location.Y+((Parent.Height-mbx.Height)/2);
mbx.位置=新系统图纸点(X,Y);
}
#端区
//
//LBL消息
//
LBL消息位置=新系统图纸点(12,23);
LblMessage.AutoSize=false;
LblMessage.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
LblMessage.Font=new System.Drawing.Font(“Microsoft Sans Serif”,9.75F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((字节)(162));
LblMessage.ForeColor=mbx.BackColor.ToInvert();
LblMessage.BorderStyle=BorderStyle.FixedSingle;
LblMessage.Text=文本;
LblMessage.Dock=DockStyle.Fill;
mbx.Controls.Add(LblMessage);
#端区
Tmr=new System.Threading.Timer(new System.Threading.TimerCallback(Tmr_Tick),mbx,timeout,0);
mbx.ShowDialog();
}
专用静态无效Tmr_记号(对象对象对象)
{
Tmr.Dispose();
if(obj为表格)
{
if(((表格)obj.InvokeRequired)
{
((Form)obj.Invoke(newsystem.Action(InvokeMbx),新对象[]{((Form)obj)});
}
其他调用EMBX((表格)obj);
}
}
专用静态无效调用EMBX(表格mbx)
{
mbx.Close();
}
}
我假设您使用的是标准的MessageBox.Show()
。您可能希望滚动自己的带有公共方法的小对话框窗口,以便在计时器过期时将其关闭。我不能只使用标准MessageBox.Show()吗?您可以-通过一些WinAPI调用(向MessageBox实例发送WM\u close
)。看这里:好了,我不知道:)谢谢。我会试试这个复制别人的答案,粘贴在这里不会让你聪明。
class AutoClosingMessageBox
{
private static System.Threading.Timer Tmr;
public static void Show(Form Parent, string text, int timeout)
{
Form mbx = new Form();
Label LblMessage = new Label();
#region InitializeComponent
mbx.Size = new System.Drawing.Size(308, 185);
mbx.MaximizeBox = false;
mbx.MinimizeBox = false;
mbx.ShowIcon = false;
mbx.ShowInTaskbar = false;
mbx.ControlBox = false;
mbx.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
mbx.FormBorderStyle = FormBorderStyle.None;
mbx.StartPosition = FormStartPosition.CenterScreen;
#region Center on Parent StartPosition
if (Parent != null)
{
mbx.BackColor = Parent.BackColor;
mbx.StartPosition = FormStartPosition.Manual;
int X = Parent.Location.X + ((Parent.Width - mbx.Width) / 2);
int Y = Parent.Location.Y + ((Parent.Height - mbx.Height) / 2);
mbx.Location = new System.Drawing.Point(X, Y);
}
#endregion
//
//LblMessage
//
LblMessage.Location = new System.Drawing.Point(12, 23);
LblMessage.AutoSize = false;
LblMessage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
LblMessage.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
LblMessage.ForeColor = mbx.BackColor.ToInvert();
LblMessage.BorderStyle = BorderStyle.FixedSingle;
LblMessage.Text = text;
LblMessage.Dock = DockStyle.Fill;
mbx.Controls.Add(LblMessage);
#endregion
Tmr = new System.Threading.Timer(new System.Threading.TimerCallback(Tmr_Tick), mbx, timeout, 0);
mbx.ShowDialog();
}
private static void Tmr_Tick(object obj)
{
Tmr.Dispose();
if (obj is Form)
{
if (((Form)obj).InvokeRequired)
{
((Form)obj).Invoke(new System.Action<Form>(InvokeMbx), new object[] { ((Form)obj) });
}
else InvokeMbx((Form)obj);
}
}
private static void InvokeMbx(Form mbx)
{
mbx.Close();
}
}