1
votes

How can I change this code so the label's text changes. Between this..Controls there was an MdiParent but I don't have one.

private void KeyOfficial_Load(object sender, EventArgs e)
{
    label2.Text = "Login as: " + ((Form)this..Controls["label1"]).Text; 
}
2
Your question is not clear .. you want to change the label2.Text after loading the datatable in c# ? - Phoenician

2 Answers

2
votes

When you initialize KeyOfficial you could submit an reference to your main form like this:

MainForm MainFormRef;

public KeyOfficial(MainForm mainRef)
{
    this.MainFormRef = mainRef;
}

In Main Form you initialize like this:

KeyOfficial koForm = new KeyOfficial(this);

And the reference is set. Now you can reach the lable1:

label2.Text = "Login as: " + this.MainFormRef.label1.Text;
1
votes

Here you can use this to get the instance of the MainForm whenever you want to access its fields

 public static MainForm instance=null;

    public static MainForm GetInstance()
    {

        if (instance != null)
        {
            return instance;
        }
        else
        {
            instance = new MainForm();
            return instance;
        }
    }

Place this code in MainForm.cs and then call this function whenever you want to access that field directly like,

MainForm.GetInstance().<field_name>

I have used the similar things in my app when I need to access different fields from one form to another and its working fine for me.