C#: Form Popup

Wat als ge een form hebt, met een button, die een nieuw form opent. Ge klikt erop, nieuw form opent, en ge hebt mooi 2 forms. Maar als ge dan op de knop duwt opent zich een nieuw form. Ongewenst gedrag. Hier een oplossing:

U popup:

public partial class Popup : Window
{
	static Popup instance;

	static Popup()
	{
	}

	public Popup()
	{
		InitializeComponent();
	}

	public static Popup Instance
	{
		get
		{
			if (instance == null)
				instance = new Popup();

			return instance;
		}
	}

	private void Popup_Closed(object sender, EventArgs e)
	{
		instance = null;
	}
}

En u mainform:

public partial class Main : Window
{
	public Main()
	{
		InitializeComponent();
	}

	private void clicky_Click(object sender, RoutedEventArgs e)
	{
		Popup popup = Popup.Instance;
		popup.Show();
		popup.Focus();
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>