如何访问动态添加到ToolStrip的ToolStrip项的值 [英] How to access a value of a ToolStrip Item that was added dynamically to ToolStrip

查看:68
本文介绍了如何访问动态添加到ToolStrip的ToolStrip项的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为只有一些标准项目(textBox等)可以添加到
ToolStrip
在Winform应用程序的设计时控制,我添加了
DateTimePicker
动态控制如下,但我不知道如何访问控件中所选日期的日期时间值

Since only some standard items (textBox etc.) can be added to a ToolStrip control at design time in a Winform app, I've added a DateTimePicker control dynamically as follows but I'm not sure how to access the datetime value of the selected date in the control

var datePicker = new ToolStripControlHost(new DateTimePicker());
toolStrip1.Items.Add(datePicker);


我尝试了以下仍然如上所述但无法访问添加的dateTimePicker的选定日期值:
$

I've tried the following that still works as above but can't get access to the selected date value of the dateTimePicker added:

DateTimePicker myDateTimePicker = new DateTimePicker();
var datePicker = new ToolStripControlHost(myDateTimePicker);
toolStrip1.Items.Add(datePicker);



MSDN教程
l描述了如何动态地向ToolStrip添加项目,但没有说明如何使用这些添加项目的示例你的代码。

This MSDN tutoriall describes how to add items dynamically to ToolStrip but no example of how to work with those added item in your code.

推荐答案

也许移动变量 myDateTimePicker 和把它放在课堂(表格)上:

Maybe move the variable myDateTimePicker and put it on class (form) level:

class MyForm

{

   DateTimePicker myDateTimePicker;

   。 。 。

}

class MyForm
{
   DateTimePicker myDateTimePicker;
   . . .
}

然后您将拥有:

   myDateTimePicker = new DateTimePicker();

   toolStrip1.Items.Add(new ToolStripControlHost(myDateTimePicker));

   myDateTimePicker = new DateTimePicker();
   toolStrip1.Items.Add(new ToolStripControlHost(myDateTimePicker));

成员 myDateTimePicker 可以从不同的地方访问。

The member myDateTimePicker can be accessed from different places.


这篇关于如何访问动态添加到ToolStrip的ToolStrip项的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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