'Auto Close Message Box

I have this program wherein I use timer to redirect to another page. It do work but the problem is when I click cancel button a messagebox will appear and when the user will not click on it and the timer ticks, messagebox didn't close. How can I automatically close the message box??

this is what it looks like..

enter image description here

and here is the code I used to redirect the page

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


    }

How can I close the message box when I redirect to the main page when Timer ticks?



Solution 1:[1]

I've used this code to Close a messagebox without creating a new form. It do works fine for me. Might help you guys also. I got it from 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);
  }

Solution 2:[2]

Using AutoClosingMessageBox.Show(this, "Your Message..", 1500);. Code is as follows:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Rene Knop