0

I have a timer class that I created that monitors the license of the software. When an error occurs I call ShowDialog() to show my customized windows form. My problem is how can I disable the parent window? Here's a simple example of my problem. As you can see once the MessageBox pops up, you can still type from the MainForm window.

MainForm1.cs file

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  

namespace TestProject  
{  
    public partial class MainForm1 : Form  
    {  
        public MainForm1()  
        {  
            InitializeComponent();  
        }  

        private void MainForm1_Load(object sender, EventArgs e)  
        {  
            TimerClass1 timer = new TimerClass1();  
        }  
    }  
}  

MessageBox.cs file

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  

namespace TestProject  
{  
    public partial class MessageBox : Form  
    {  
        public MessageBox()  
        {  
            InitializeComponent();  
            this.label1.Text = "Hello There";  
            this.button1.Text = "OK";  
            this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;  
        }  
    }  
}  

TimerClass1.cs file

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Timers;  
using System.Windows;  

namespace TestProject  
{  
    class TimerClass1  
    {  
        Timer _timer;  
        public TimerClass1()  
        {  
            _timer = new Timer(1);  
            _timer.Elapsed +=new ElapsedEventHandler(_timer_Elapsed);  
            _timer.Enabled = true;  
        }  

        private void _timer_Elapsed(object sender, ElapsedEventArgs e)  
        {  
            _timer.Stop();  
            MessageBox msg = new MessageBox();  
            msg.ShowDialog();  
            _timer.Start();  
        }  
    }  
}  
H H
  • 263,252
  • 30
  • 330
  • 514
user1598995
  • 1
  • 1
  • 2
  • Possible duplicate?: http://stackoverflow.com/questions/1130208/disable-parent-form-when-child-form-is-active – Rivasa Aug 14 '12 at 20:27
  • 1
    Just from reading this code I would say the main window should be non-responsive. But making your own MessageBox class is confusing things. – H H Aug 14 '12 at 20:27
  • 2
    Also, why is timer calling messagebox? If you want showdialog to work, it should be called from the mainform. – Rivasa Aug 14 '12 at 20:28

4 Answers4

2

You're showing the MessageBox on a separate thread, so it doesn't show as a modal dialog of the main window. You need to show it on the main UI thread:

    private void _timer_Elapsed(object sender, ElapsedEventArgs e)  
    {  
        _timer.Stop();  
        Application.Current.Dispatcher.Invoke(new Action(
        () => {
            MessageBox msg = new MessageBox();  
            msg.ShowDialog();  
        }));
        _timer.Start();  
    }  
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
1

To fix it, just change the following:

TimerClass1.cs file

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
//using System.Timers;  
using System.Windows;  

and then fix the errors that result from switching to Windows.Forms.Timer (the one you need).

H H
  • 263,252
  • 30
  • 330
  • 514
0

You'll need some way to access the MainForm1 from TimerClass1. Once you've done that, you can create and call a method on MainForm1 that will disable the form itself or the controls on the form.

John Fisher
  • 22,355
  • 2
  • 39
  • 64
0

Sending the parent form as a parameter to your timer, and showing dialog as follow should make the trick:

MainForm1.cs file

TimerClass1 timer = new TimerClass1(this);

TimerClass1.cs file

..
private Form ParentForm {get; set;}
..
public TimerClass1(Form parentForm)           
{
..
this.ParentForm = parentForm;
..   
}
..
private void _timer_Elapsed(object sender, ElapsedEventArgs e)           
{
..
 msg.ShowDialog(this.ParentForm);
..
} 
.. 
danielQ
  • 2,016
  • 1
  • 15
  • 19