Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

How to access the main form from another form

Hi,

I have a main form called MainForm with a SchedulerControl on it.
and I have a form called Settings that can be called by menuitem Settings
On the settingsform I have a checkbox, in the checkbox-event I have
a line of code that calls for the SchedulerControl on the mainform.
But I don't know how to access the SchedulerControl on the mainform
How can I do that?

Peter
Avatar of crysallus
crysallus
Flag of Australia image

Hope I don't lead you astray here, but if I recall correctly, you can just pass a reference to the main form, or the scheduler control, to the constructor of the Settings Form. In the Settings form constructor, assign the reference to the main form or the SchedulerControl to a variable within the Settings Form class, then you can access it at any point during the Settings Form's lifetime.
Avatar of Peter Kiers

ASKER

Can you show me how?

Peter
1) You can pass SchedulerControl via constructor of SettingsForm
     SettingsForm form = new SettingsForm(schedulerControl1);
     form.Show();

2) You can create event on SettingsForm (e.g. BlaBlaBlaChanged), subscribe to it on MainForm and raise it when you check checkbox on SettingsForm
     SettingsForm form = new SettingsForm();
     form.BlaBlaBlaChanged += SettingsForm_BlaBlaBlaChanged;
     form.Show();
     // do not forget to unsubscribe from event

      private void SettingsForm_BlaBlaBlaChanged(object sender, BlaBlaBlaChangedEventArgs e)
      {
          // call for the SchedulerControl
       }
ASKER CERTIFIED SOLUTION
Avatar of buraksarica
buraksarica
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have this in the Settings form:

    public partial class Settings : Form
    {
        MainForm frmMain = new MainForm();
        IniFile _if;
                 ....
    }

And this methode that calls for the SchedulerControl:

        private void chkShowMin_Click(object sender, EventArgs e)
        {
                if (chkShowMin.Checked)
                {
                    frmMain.schedulerControl.DayView.TimeRulers[0].ShowMinutes = false;
                    chkShowMin.Checked = false;
                }
                else
                {
                    frmMain.schedulerControl.DayView.TimeRulers[0].ShowMinutes = true;
                    chkShowMin.Checked = true;
            }
        }

But i get error:
MainForm.schedulerControl' is inaccessible due to its protection level

Peter
I go for the last post of buracsarica.

Can someone show me how?
I wonder how you accept the answer without verifying it.
Add to Settings a private variable

private SchedulerControl schedControl;

Open in new window


In Settings change the constructor to look like this
public Settings(SchedulerControl schedulerControl)
{
    InitializeComponent();
    schedControl = schedulerControl;
}

Open in new window


In your event handler for clicking on the menu item that opens the Settings Form add

Settings settingsForm = new Settings(this.schedulerControl);
settingsForm.Show();

Open in new window


Then in your checkbox click event handler in the Settings Form, just refer to schedControl, rather than refer to the main form. eg.

schedControl.DayView... etc