簡體   English   中英

C#如何從另一個類更改變量

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

我在Windows窗體應用程序的主要窗體中有一個稱為“語言”的變量。

在子表單中,我有一個組合框,不需要更改主表單的新實例,就可以使用它來更改“語言”的值

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

那么我可以在其他形式的事件中更改主形式的變量嗎?

編輯:我更改此但在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();
        }

您需要將MainWindow的引用傳遞給另一個類,以訪問其成員。 為此,請在MENU類內創建一個構造函數,該構造函數需要MainWindow類的實例。 此外,您要更改的成員應該是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 = //
        }
    }
}

好吧,如果您的Menu -form沒有主窗體的實例(可以將其傳遞給構造函數或作為屬性),則仍可以使用以下OpenForms.OfType<MainWindow> -“ trick”來獲取它:

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

您需要將其設置為公共財產:

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

我准備了一個很小的示例,如何在不將主表單傳遞給子表單的情況下從子表單中檢索內容

運行主窗體

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

僅帶有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}");
        }
    }
}

子表格會要求用戶輸入數字。 如果用戶輸入數字,則表單將關閉,並顯示確定對話框結果。 對話結果將幫助我們了解用戶選擇了某些內容還是僅選擇了封閉形式。

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