简体   繁体   English

如何从 C# 中的另一个表单更改表单属性

[英]How to change a form properties from another form in C#

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..这是打开设置菜单的代码..

Rather than attempt to change properties in the calling form one option is to create a class with properties to change eg与其尝试在调用表单中更改属性,一种选择是创建一个 class,其中包含要更改的属性,例如

Class for settings Class 进行设置

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 .在要进行更改的表单中,这里是上面两个属性的一个颜色对话框和一个Settings class 的实例。进行更改时,在私有变量_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.对于上面的内容,当然需要一些地方来存储设置,它可能在 json 文件中,而下面的选项已经为您处理了。

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.例如,对于主窗体背景颜色,创建 MainFormBackColor 比通过绑定到窗体属性 window 中 ApplicationSettings 下的背景颜色来使用它,为每个控件添加一个绑定。

In SettingsMenu create three public variables to hold the Colors:SettingsMenu中创建三个公共变量来保存 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:现在您可以在ShowDialog()返回后检索这些值:

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;
    }
}

I guess Form1 is the form that contain setting btn right?我猜 Form1 是包含设置 btn 的表单吧? And then you try to change the theme of Form1 form setting Form.然后你尝试改变 Form1 窗体设置窗体的主题。

The problems is that you try to new Form1 and set the color to it because it is difference instance of Form1.问题是您尝试新建 Form1 并为其设置颜色,因为它是 Form1 的不同实例。

You rather be pass Form1 to SettingForm Constructor and set color to it.您宁愿将 Form1 传递给 SettingForm Constructor 并为其设置颜色。

// 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.对不起,如果我的猜测是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM