bask185
bask185

Reputation: 397

c# how change a variable from another class

I have a variabele called 'language' in the main form of my windows form application.

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.windowhas red underlines.

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

Upvotes: 0

Views: 13764

Answers (3)

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

You need to pass a reference of your MainWindow to the other class in order to access its members. To achieve this create a constructor within your MENU-class that expects an instance of your MainWindow-class. Furthermore the member you want to change should be public, and preferably a property instead of a field:

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 = //
        }
    }
}

Upvotes: 1

tym32167
tym32167

Reputation: 4881

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

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

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460108

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":

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

Upvotes: 1

Related Questions