c# - 自动关闭消息框

标签 c# timer messagebox auto-close

我有这个程序,其中我使用计时器重定向到另一个页面。它确实有效,但问题是当我点击取消按钮时,会出现一个消息框,当用户不点击它并且计时器滴答作响时,消息框没有关闭。如何自动关闭消息框??

这就是它的样子..

enter image description here

这是我用来重定向页面的代码

 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();


    }

如何在定时器计时跳转到主页面时关闭消息框?

最佳答案

我已使用此代码关闭消息框,而无需创建新表单。它对我来说很好用。也可以帮到大家。我从Close a MessageBox after several seconds得到的

 private void btnOK_Click(object sender, RoutedEventArgs e)
 {
     AutoClosingMessageBox.Show("Wrong Input.", "LMS", 5000);
 }

  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);
  }

关于c# - 自动关闭消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23692127/

相关文章:

c# - 从 C# 调用带有 char* 参数的 DLL 函数?

c# - 从另一个程序的文本字段中插入和检索文本的方法

C# 分隔 TCP 消息

c# - 声明类型列表

c# - 生成licenses.licx

TimerOne 和串行库

PHP 服务器端计时器?

c# - 了解多媒体计时器的奇怪行为

customization - nsis - 自定义 MessageBox 上的按钮文本

Python:如何在消息框中获取输入框?