天天@BLOG

脖子越来越疼,脑袋越来越钝
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在基类里添加按F1的帮助信息

Posted on 2005-12-22 10:23  天天在线  阅读(183)  评论(0编辑  收藏  举报

public void AddF1Event(Control ctl_Param) {
   ctl_Param.KeyDown += new KeyEventHandler(ctl_Param_KeyDown);
   foreach (Control ctl_Tmp in ctl_Param.Controls) {
    if(!(ctl_Tmp is TabPage))
    this.AddF1Event(ctl_Tmp);
   }
  }

  private void ctl_Param_KeyDown(object sender, KeyEventArgs e) {
   if (e.KeyCode == Keys.F1) {
    Control o_Tmp = (Control)sender;
//    string str_Location = o_Tmp.Name;
//    while (o_Tmp != null) {    
//     str_Location = str_Location + "\n on Control " + o_Tmp.Name;
//     if (o_Tmp is Form) {
//      MessageBox.Show(str_Location);
//      break;
//     }
//     else {
//      o_Tmp = o_Tmp.Parent;
//     }
//    }
    if (o_Tmp is Form)
    {
     //str_Location = o_Tmp.Name;
    }
    else
    {
     while (!(o_Tmp is Form))
     {
      o_Tmp = o_Tmp.Parent;
     }
    }
    string str_Location = o_Tmp.Text;//这里是获得窗体的text值。
    ShowChm(str_Location);
   }
  }

  private void ShowChm(string indexName)
  {
//   string helpfile = Directory.GetCurrentDirectory() + "\\Chm.chm";//这两个方法都是获得debug目录下的chm文件。
   string helpfile = Application.StartupPath + "\\Chm.chm";
   HelpNavigator navigator = HelpNavigator.KeywordIndex;//根据CHM文件的索引指到对应的页面。
   Help.ShowHelp(this, helpfile,navigator,indexName);
  }

在基类的form_load事件下添加AddF1Event(this);

@忙碌,不代表有效率;方法,远胜于苦干