开发者

wpf prevent second click button

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.S开发者_JAVA百科how();
  dic.Topmost = true;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜