闂佽 鍋撻柟顖滃椤ワ拷 (0) +1 闁荤姍宥嗗 (0) +1 闂傚⿴浜烽幏锟� (0) +1
闂佽 鍋撻柟顖滃椤ユ垿鏌熺€涙ê濮囧┑顕嗘嫹闂佸搫琚崕鍐诧耿閸涙潙缁╅柟顖滃椤ワ拷>>

正在阅读:MDI应用中的单实例(singleton)窗口MDI应用中的单实例(singleton)窗口

2004-06-25 09:53 出处:CSDN 作者:turnmissile [翻译] 责任编辑:linjixiong

  下面的代码片断假设你的多文档应用程序中包含一个toolbar。当你点击toolBarButton1 按钮时,将会创建并显示一个 Patients 子窗口。下面的代码在单机事件时发生。



private void toolBar1_ButtonClick(object sender,
     System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
    // a flag to store if the child form is opened or not
    bool found = false;
 
    if (e.Button == toolBarButton1)
    {
       // get all of the MDI children in an array
       Form[] charr = this.MdiChildren;
 
       if (charr.Length == 0)      // no child form is opened
       {
          Patients myPatients = new Patients();
          myPatients.MdiParent = this;
          // The StartPosition property is essential
          // for the location property to work
          myPatients.StartPosition = FormStartPosition.Manual;
          myPatients.Location = new Point(0,0);
          myPatients.Show();
        }
        else      // child forms are opened
        {

 


察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:



         foreach (Form chform in charr)
          {
            if (chform.Name == "Patients")
            // one instance of the form is already opened
            {
              chform.Activate();
              found = true;
              break;   // exit loop
            }
            else
              found = false;      // make sure flag is set to
                                  // false if the form is not found
          }
 
          if (found == false)   
          {
            Patients myPatients = new Patients();
            myPatients.MdiParent = this;
            // The StartPosition property is essential
            // for the location property to work
            myPatients.StartPosition = FormStartPosition.Manual;
            myPatients.Location = new Point(0,0);
            myPatients.Show();
          }
        }
    }
}

   这样,就实现了一个单窗口实例的解决方案。


察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:

相关文章

关注我们

最新资讯离线随时看 聊天吐槽赢奖品
闂佸綊娼ч鍡椻攦閳ь剟鎮规担鍛婂仴婵☆偓鎷�闂佹悶鍎抽崑娑㈠春鐏炰勘浜滈柛顐f礀閸旓拷