Click here to Skip to main content
15,898,798 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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:
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());
            }
        }


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.
Posted
Comments
Wayne Gaylard 1-Jun-11 8:24am    
What exactly are you trying to achieve ? Are the values not getting entered into the spreadsheet or are the date formats not correct or what is the error. It's not very clear
M Bester 1-Jun-11 8:51am    
Sorry, values weren't inputed. Got it fixed below though.

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).
 
Share this answer
 
Comments
M Bester 1-Jun-11 8:49am    
Alright 'public CreateExcelClass(string buildingName, Form tempForm)' and 'form = (ExcelTestTwo.MainForm)tempForm;' did it for me. Thought I got it earlier without passing the form but I got todays date. Thanks alot.
BobJanova 1-Jun-11 17:59pm    
You're welcome.

You can actually have the constructor parameter as a MainForm, if that's the only place this class can be run from. Since you're casting it anyway, you're making that assumption, so it's better to declare it as such.
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();
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900