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

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

本文介绍了从另一个类访问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.

推荐答案



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

09-03 13:55