views:

51

answers:

1

hi! I have a problem again. when I click button, window appears. when I click button again same window appears again. I want when I click button first time, page appears. but I want to prevent second click. can anyone help me with this problem? thanks in advance.

private void Dictionary_Click(object sender, RoutedEventArgs e)
{
  Dictionary dic = new Dictionary();
  dic.Show();
  dic.Topmost = true;
}
+2  A: 

set a simple boolean value to check if the window is already open?

private bool isWindowAlreadyOpen = false;
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
   if (!isWindowAlreadyOpen)
   {
       Dictionary dic = new Dictionary();
       dic.Show();
       dic.Topmost = true;
       isWindowAlreadyOpen = true;
   }
}

Should do the trick.

EDIT
You'll have to register the closed event of the window to unset the boolean:

private bool isWindowAlreadyOpen = false;
private void Dictionary_Click(object sender, RoutedEventArgs e) 
{
    if (!isWindowAlreadyOpen) 
    {
        Dictionary dic = new Dictionary();
        dic.Show();
        dic.Topmost = true;
        dic.Closed += Dictionary_Closed;
        isWindowAlreadyOpen = true;
    }
}

private void Dictionary_Closed(object sender, EventArgs e)
{
    isWindowAlreadyOpen = false;
}

EDIT2
Alternatively, you can use dic.ShowDialog() if you want this window to be topmost and only one instance.

Roel
i tried it. it works. but when i close the window and wanna open again, i doesnt open. can you have any idea about it? thanks all.
neki
thank you very much. it worked. i an new in wpf and .net. thank you again. good working.
neki