且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从另一个类访问Windows窗体控件

更新时间:2023-12-06 11:40:52

您可能想使用DateTimePicker.Value,而不是Text.但是,那里的东西似乎还可以.问题在于您正在创建一个新的MainForm,而没有传递对您实际在其中编辑控件的引用!在某个地方,您需要将要使用的主窗体的一个实例传递给CreateExcelClass(如果响应用户交互而创建,则可能在其构造函数中).
You probably want to use DateTimePicker.Value, not Text. However, what you have there appears ok. The problem is that you are creating a new MainForm, not passing a reference to the one you are actually editing controls in! Somewhere, you need to pass an instance of the main form you are using to the CreateExcelClass (perhaps in its constructor if it is created in response to a user interaction).


您可以看到所有使用Application.OpenForms打开的表单,遍历所有表单

foreach(Application.OpenForms.OfType()中的var f)
{
if(f是MainForm)
{
(f为MainForm).GetWaterStartDate();
}
}
You can see all the opened forms with the Application.OpenForms, loop through all the forms

foreach(var f in Application.OpenForms.OfType())
{
if(f is MainForm)
{
(f as MainForm).GetWaterStartDate();
}
}