关于在课外调用控件 [英] about calling control outside of the class

查看:84
本文介绍了关于在课外调用控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows应用程序.主界面上有一个菜单栏,主界面上有一个面板.在查看日历"的单击事件中,面板上有一个onother表单.在该表格上有一个月历.我想在跳转到今天"菜单项的点击事件中以粗体显示当前日期.请帮我.提前感谢.

该代码是在查看日历"按钮的单击事件上执行的,该事件将在主界面面板上加载表单.

I am working on a windows app. There is a menubar on main interface and on the main interface there is a panel. On click event of "View Calender" there is a onother form on the panel. On that form there is a monthcalender. I want to show current date bolded on click event of "Jump to Today" menu item. Please help me. Thanx in advance.

This code is on click event of "View Calender" button, which load the form on panel of main interface.

Calender obj = new Calender();
obj.TopLevel = false;

  if (panel2.Controls.Count > 0)
      {
          panel2.Controls.Clear();
          panel2.Controls.Add(obj);
          obj.TopLevel = false;
          obj.Show();
      }
 else
      {
           obj.TopLevel = false;
           panel2.Controls.Add(obj);
           obj.Show();
      }

推荐答案

这些表格是否在MDI容器中?

无论哪种方式,您都需要在需要引用的上下文中使对Calendar对象的引用(请注意拼写:))可用.在这种情况下,这意味着顶层表单,即

Are these forms within an MDI container?

Either way you need to make the reference to the Calendar object (note the spelling :)) available within the context that it needs to be referenced. In this case that means the top level form, i.e.

class MyForm {
 ...

 Calendar calendar = new Calendar();

 void viewCalendar_click(object sender, EventArgs e){
   panel2.Controls.Clear();
   panel2.Controls.Add(obj);
   calendar.TopLevel = false;
   calendar.Show();
 }
}



然后可以在跳到今天"的事件处理程序中引用它:



Then you can refer to it in the event handler of ''Jump to Today'':

void jumpToToday_click(object sender, EventArgs e){
  calendar.HighlightDate(DateTime.Today);
}



……显然,您还需要编写HighlightDate,这取决于您的日历的工作方式,但这应该很容易.



... obviously you need to write HighlightDate as well and that depends on how your Calendar works, but that should be pretty easy.


这篇关于关于在课外调用控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆