1

I just started with C# and a windows form application. I'm fairly new to OOP as well. I trying to create an application where the user can modify some settings from a meny that opens a second form. I found a way to move the user input data from the "settings form" to the main form using an additional "settings class". I am not however sure if I'm doing this in an good way and is seeking advice on best practice.

My code for the "Main form" looks like:

namespace Scheduler
{
    public partial class FormScheduler : Form
    {

        public FormScheduler()
        {
            InitializeComponent();
        }

        private PeriodSettings mPeriodSettings = new PeriodSettings();

        public string StartDate
        {
            get{ return mPeriodSettings.StartDate; }

            set{ mPeriodSettings.StartDate = value; }
        }

        private void settingsSchPer_Click(object sender, EventArgs e)
        {
            FormSchemaPeriodSettings formSchPer = new FormSchemaPeriodSettings();
            formSchPer.formScheduler = this;
            formSchPer.Show();
        }

    }
}

And the code for the "settings form" looks like:

namespace Scheduler
{
    public partial class FormSchemaPeriodSettings : Form
    {

        public FormSchemaPeriodSettings()
        {
            InitializeComponent();
        }

        public FormScheduler formScheduler = new FormScheduler();

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.formScheduler.StartDate = this.startDate.Value.ToShortDateString();
            this.Close();
        }
    }
}

For example, must I define the properity for SetDate in the "Main form", i.e. the part

public string StartDate
{
    get{ return mPeriodSettings.StartDate; }

    set{ mPeriodSettings.StartDate = value; }
}

in order to be able to access the StartDate of class PeriodSettings or is there a way to be able to directly access the already defined mPeriodSettings.StartDate?

My PeriodSettings class looks like:

namespace Scheduler
{
    class PeriodSettings
    {
        private string mStartDate;
        private string mEndDate;

        public PeriodSettings()
        {

        }

        public string StartDate
        {
            get{ return mStartDate; }

            set{ mStartDate = value; }
        }

        public string EndDate
        {
            get{ return mEndDate; }

            set{ mEndDate = value; }
        }
    }
}

Thankful for any advice!

3
  • 1
    Some time ago I answered a quite similar question. Have a look at it. That solution might come in handy for you, too. Commented Mar 5, 2016 at 8:10
  • 1
    There is no need to pass the whole form when the only things that you need to change are properties of the PeriodSettings instance created in the main form. Instead of passing the whole form pass the reference to the PeriodSettings instance. Commented Mar 5, 2016 at 8:29
  • Thanks! I chose to pass the PeriodSettings instance to the settings form. Works fine. Commented Mar 6, 2016 at 7:59

1 Answer 1

1

You can also open the settings form as an dialog and work with the DialogResult property, like this:

public partial class FormScheduler : Form
{
    public FormScheduler()
    {
        InitializeComponent();
    }

    public string StartDate
    {
        get { return mPeriodSettings.StartDate; }
        set { mPeriodSettings.StartDate = value; }
    }

    private void settingsSchPer_Click(object sender, EventArgs e)
    {
        FormSchemaPeriodSettings formSchPer = new FormSchemaPeriodSettings();
        if (formSchPer.ShowDialog() == DialogResult.OK)
            StartDate = formSchPer.ResultDate;
    }
}

public partial class FormSchemaPeriodSettings : Form
{
    public FormSchemaPeriodSettings()
    {
        InitializeComponent();
    }

    public string ResultDate;

    private void buttonCancel_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void buttonOK_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.OK;
        ResultDate = startDate.ValueToShortDateString();
        Close();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Currently I went with the suggetions to pass the PeriodSettings instance to the settings form, but this approach also seems practical since I then directly also get the abbility to update the Main form dependet on what settings the user chose upon OK pressed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.