简体   繁体   English

C#如何从另一个类更改变量

[英]c# how change a variable from another class

I have a variabele called 'language' in the main form of my windows form application. 我在Windows窗体应用程序的主要窗体中有一个称为“语言”的变量。

In a sub-form I have a combo box with which I need to change the value of 'language' without making a new instance of the main form 在子表单中,我有一个组合框,不需要更改主表单的新实例,就可以使用它来更改“语言”的值

namespace MMI_WFA
{
    public partial class MainWindow : Form
    {
        int language;
            ...
            ...
    }
}


namespace MMI_WFA
{
    public partial class MENU : Form
    {
    ...

        private void cbo_Language_SelectedIndexChanged(object sender, EventArgs e)
        {
            function_to_Change_language_in_mainForm();
        }
    }
}

So can I change a variable in the main form, with an event of an other form? 那么我可以在其他形式的事件中更改主形式的变量吗?

EDIT: I change this but window in this.window has red underlines. 编辑:我更改此但在this.window中的窗口有红色下划线。

public MENU(MainWindow window) // main constructor
        {
            this.window = window;
            InitializeComponent();
            cbo_SerialPort.Items.AddRange(ports);
            //cbo_SerialPort.SelectedIndex = 2;           

            cbo_Baudrate.Items.AddRange(baudrates);
            //cbo_Baudrate.SelectedIndex = 1;

            cbo_ParityBits.Items.AddRange(partitybits);
            //cbo_ParityBits.SelectedIndex = 0;

            hideExtraSettings();
            fillLanguageBox();
            indexComboboxes();
        }

You need to pass a reference of your MainWindow to the other class in order to access its members. 您需要将MainWindow的引用传递给另一个类,以访问其成员。 To achieve this create a constructor within your MENU -class that expects an instance of your MainWindow -class. 为此,请在MENU类内创建一个构造函数,该构造函数需要MainWindow类的实例。 Furthermore the member you want to change should be public , and preferably a property instead of a field: 此外,您要更改的成员应该是public ,最好是一个属性而不是一个字段:

namespace MMI_WFA
{
    public partial class MainWindow : Form
    {
        public int Language { get; set; }
        ...
        ...
    }

    public partial class MENU : Form
    {
        private readonly MainWindow window;

        public MENU(MainWindow window) { this.window = window; }

        private void cbo_Language_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.window.Language = //
        }
    }
}

Well, if your Menu -form doesn't have the instance of the main form(which you could pass in the constructor or as property) you could still get it with this OpenForms.OfType<MainWindow> -"trick": 好吧,如果您的Menu -form没有主窗体的实例(可以将其传递给构造函数或作为属性),则仍可以使用以下OpenForms.OfType<MainWindow> -“ trick”来获取它:

var main = Application.OpenForms.OfType<MainWindow>().First();
main.Language = 1;

You need to make it a public property: 您需要将其设置为公共财产:

public partial class MainWindow : Form
{
    public int Language { get; set; }
}

I prepared very tiny example, how you can retrieve something from child form without passing Main form to child 我准备了一个很小的示例,如何在不将主表单传递给子表单的情况下从子表单中检索内容

Running main form 运行主窗体

void Main()
{
    var main = new MainForm();
    main.ShowDialog();
}

Main form with 1 button only - to ask child form for a number 仅带有1个按钮的主表单-向子表单询问数字

class MainForm : Form
{
    public MainForm()
    {
        var button = new Button();
        button.Click += button_pressed;
        button.Text = "Ask number";     
        this.Controls.Add(button);      
    }

    void button_pressed(object sender, EventArgs e)
    {
        var child = new SubForm();
        var result = child.ShowDialog();

        if (result == DialogResult.OK)
        {
            MessageBox.Show($"Entered number is {child.SelectedNumber}");
        }
    }
}

Child form will ask user to enter number. 子表格会要求用户输入数字。 If user will enter number, then form will be closed with OK dialog result. 如果用户输入数字,则表单将关闭,并显示确定对话框结果。 Dialog result will help us to understand whether user selected something or just closed form. 对话结果将帮助我们了解用户选择了某些内容还是仅选择了封闭形式。

class SubForm : Form
{
    public int SelectedNumber { get; set;}

    public SubForm()
    {
        var button = new Button();      
        var textBox = new TextBox();

        button.Click += (s, e) => {
            int i;
            if (int.TryParse(textBox.Text, out i))
            {
                this.SelectedNumber = i;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("pls, enter number", "Warning",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        };  

        textBox.SetBounds(0, 0, 100, 20);
        button.SetBounds(100, 0, 30, 20);

        button.Text = "OK";     
        this.Controls.Add(textBox);
        this.Controls.Add(button);
    }   
}

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

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