Home > Back-end >  How to change a form properties from another form in C#
How to change a form properties from another form in C#

Time:05-03

I am trying to make a settings menu and from that menu I am trying to change the properties of the other form but it doesn't work...

private void lightThemeBtn_Click(object sender, EventArgs e)
    {
        Form1 mainForm = new Form1();
        mainForm.chart1.BackColor = Color.White;
        mainForm.panel1.BackColor = Color.White;
        mainForm.BackColor = Color.White;   
    }

this is the code from the settings menu and when I clicked that button nothing happens

  private void settingBtn_Click(object sender, EventArgs e)
    {
        SettingsMenu sm = new SettingsMenu();
        sm.ShowDialog();
        sm.Activate();  
    }

this is the code that open settings menu..

CodePudding user response:

Rather than attempt to change properties in the calling form one option is to create a class with properties to change e.g.

Class for settings

public class Settings
{
    public Color? MainFormBackColor { get; set; }
    public Color? Panel1BackColor { get; set; }
}

In the form to make changes, here one color dialog for the two properties above and an instance of Settings class. When a change is made, set the value in the private variable _settings.

Create an event which the main form subscribes to and when invoked checks if there is a setting value for each property, if so set them.

Settings form

public partial class SettingsForm : Form
{
    public delegate void OnChangeColors(Settings settings);
    public event OnChangeColors ColorsChanged;
    private readonly Settings _settings = new Settings();
    public SettingsForm()
    {
        InitializeComponent();
    }

    private void SaveButton_Click(object sender, EventArgs e)
    {
        ColorsChanged?.Invoke(_settings);
    }

    private void FormBackColorButton_Click(object sender, EventArgs e)
    {
        if (colorDialog1.ShowDialog() == DialogResult.OK)
        {
            _settings.MainFormBackColor = colorDialog1.Color;
        }
    }

    private void PanelBackColor_Click(object sender, EventArgs e)
    {
        if (colorDialog1.ShowDialog() == DialogResult.OK)
        {
            _settings.Panel1BackColor = colorDialog1.Color;
        }
    }
}

Main form

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

    private void SettingsButton_Click(object sender, EventArgs e)
    {
        var settingsForm = new SettingsForm();
        settingsForm.ColorsChanged  = SettingsFormOnColorsChanged;
        settingsForm.ShowDialog();
        settingsForm.Dispose();
    }

    private void SettingsFormOnColorsChanged(Settings settings)
    {
        if (settings.Panel1BackColor.HasValue)
        {
            panel1.BackColor = settings.Panel1BackColor.Value;
        }

        if (settings.MainFormBackColor.HasValue)
        {
            BackColor = settings.MainFormBackColor.Value;
        }
    }
}

For the above there of course needs to be some place to store setting which may be in a json file while the option below would be already handled for you.

Another option is to create a setting for each property you want to set than data bind a property to the setting.

For instance, for the main form back color create MainFormBackColor than use this by binding to back color under ApplicationSettings in the property window for the form, add a binding for each control.

CodePudding user response:

In SettingsMenucreate three public variables to hold the Colors:

public class SettingsMenu 
{

    public Color clrChart;
    public Color clrPanel;
    public Color clrForm;

    private void lightThemeBtn_Click(object sender, EventArgs e)
    {
        clrChart = Color.White; // don't all have to be the same!
        clrPanel = Color.White; // don't all have to be the same!
        clrForm = Color.White; // don't all have to be the same!
        this.DialogResult = DialogResult.OK;
    }

}

Now you can retrieve those values after ShowDialog() returns:

private void settingBtn_Click(object sender, EventArgs e)
{
    SettingsMenu sm = new SettingsMenu();
    if (sm.ShowDialog() == DialogResult.OK)
    {
        this.chart1.BackColor = sm.clrChart;
        this.panel1.BackColor = sm.clrPanel;
        this.BackColor = sm.clrForm;
    }
}

CodePudding user response:

I guess Form1 is the form that contain setting btn right? And then you try to change the theme of Form1 form setting Form.

The problems is that you try to new Form1 and set the color to it because it is difference instance of Form1.

You rather be pass Form1 to SettingForm Constructor and set color to it.

// in SettingsMenu
Form1 mainForm;
public SettingsMenu(Form1 form1_) {
   mainForm = form1_;
}

private void lightThemeBtn_Click(object sender, EventArgs e)
{
    mainForm.chart1.BackColor = Color.White;
    mainForm.panel1.BackColor = Color.White;
    mainForm.BackColor = Color.White;   
}


// in Form1.cs
private void settingBtn_Click(object sender, EventArgs e)
{
    SettingsMenu sm = new SettingsMenu(this);
    sm.ShowDialog();
    sm.Activate();  
}

Sorry if my guess is wrong.

  • Related