从另一个类访问Windows窗体控件 [英] Accessing Windows Form Control from Another Class

查看:130
本文介绍了从另一个类访问Windows窗体控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


忙着创建一个小程序,该程序可以从sql数据库创建excel电子表格,但遇到了一些麻烦.

尝试从CreateExcelClass(单独的.cs文件)从MainForm类中的datetimepicker访问文本.尝试了一些我在网络上发现的示例,但效果不佳.

因为我很不擅长,所以不做所有解释,这将占用很多空间.

这是我尝试过的.
主表单:

Hi
Busy creating with a little program that creates a excel spreadsheet from a sql database but hit a little snag.

Trying to access the text from a datetimepicker in my MainForm class from my CreateExcelClass (seperate .cs file). Tried a few examples I found around the web, didn''t work so well.

Not going to explain them all since I''m bad at it and it''s going to take alot of space.

Here''s what I tried.
MAINFORM:

public string getWaterStartDate()
{
    return dateTimePicker3.Text;
}

public string getWaterEndDate()
{
    return dateTimePicker4.Text;
}


CREATEEXCELCLASS:


CREATEEXCELCLASS:

Mainform form = new MainForm();
public void addBetweenDates()
        {
            try
            {
                if(strSupply == "W")
                {
                    ws.Cells[5, "B"] = form.getWaterStartDate();
                    //test output
                    ThrowError("addBetweenDates: " + form.getWaterStartDate());
                    ws.Cells[6, "B"] = form.getWaterEndDate();
                    //test output
                    ThrowError("addBetweenDates: " + form.getWaterEndDate());
                }
                else if (strSupply == "E")
                {
                    // 
                }
            }
            catch (Exception ex)
            {
                ThrowError(ex.Message.ToString());
            }
        }



这是我在日志文件中得到的输出:

2011年1月6日02:09:02 PM:addBetweenDates:
2011年1月6日02:09:02 PM:addBetweenDates:
01/06/2011 02:09:02 PM:OpenSheetAfterSave:打开文件:Tyger Valley(01-06-2011).xls

可能在做一些可怕的错误.有什么建议吗?

如果需要更多代码,请询问.



And here is the output I get in my log file:

01/06/2011 02:09:02 PM: addBetweenDates:
01/06/2011 02:09:02 PM: addBetweenDates:
01/06/2011 02:09:02 PM: OpenSheetAfterSave: Opening File: Tyger Valley (01-06-2011).xls

Probably doing something hideously wrong. Any suggestions?

If you need more code just ask.

推荐答案

您可能想使用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();
}
}


这篇关于从另一个类访问Windows窗体控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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