-2

I am trying to pass some strings(device info) from the parent form(the main form )in MDI to the child form

  • The child form displays
  1. Device name
  2. Device model number
  3. Manufactured date,
  • All these features should be passed to the child form when the ON button in the parent form is being clicked.
  • The child form is called through the menu.
  • Till now I have had success in only calling the child through the menu but cannot pass the data.

Parent Form: menu to view the child form on the MDI(parent form) whenever I want to

private void DeviceInfomationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms["Child"] is Child deviceInfo)
            {
                deviceInfo.Focus();
                return;
            }
            deviceInfo = new Child();
            deviceInfo.MdiParent = this;
            deviceInfo.Show();
        }

Parent From: On button event

private void btnOn_Click(object sender, EventArgs e)
        {
            deviceName = "Notebook520220624";
            deviceModel =  "520220627";
            manuFacturedDate = "220627";
            Child form = new Child(deviceName);
        }

Child Form: Receiving the device info and displaying it

public Child(string deviceName)
{
  InitializeComponent();
  name_lbl.Text = deviceName
  //name_lbl.Text = deviceName.ToString();
  //model_lbl.Text = diviceModel;
  //date_lbl.Text = manuFacturedDate;
}
Lha
  • 25
  • 8
  • many of the posts listed to the right under **Related** will enlighten and are relevant. Many, many other posts ion the site could also help - this is a very often asked question – Ňɏssa Pøngjǣrdenlarp Jun 27 '22 at 05:07
  • You should have public property on parent form for Device Name. And in MenuClick event you should get hold of parent form object by doing `Application.OpenForms["Parent"]` and then access it property and pass it to child form when opening. – Chetan Jun 27 '22 at 05:08
  • Make this method on the child form: `public void SetDeviceName(string deviceName)` – Enigmativity Jun 27 '22 at 05:10

2 Answers2

1

You can create two constructor. When the child form is created you can give any data or not.

public Child(string deviceName)
{
  InitializeComponent();
  name_lbl.Text = deviceName  
}

public Child()
{
  InitializeComponent();  
}
1

You can declare static variable in parent form outside your method,in class level - global scope, and then access that variable through the type in your child form constructor, so for example

public static string deviceName = ""; // Here I declared static string global variable, and initialized to empty string

    private void btnOn_Click(object sender, EventArgs e)
            {
                deviceName = "Notebook520220624"; // Assign the value to variable
                deviceModel =  "520220627";
                manuFacturedDate = "220627";
            }

And then, in your child form, you can pass the value of static variable to your textbox through the default constructor, by calling this

public Child()
{
  InitializeComponent();
  name_lbl.Text = ParentForm.deviceName; // Here I put name ParentForm type name regarding to this example, but you should change according to your name of your class
}
Jakobson
  • 101
  • 1
  • 2
  • 10
  • I get this error at the Childform **'Control' does not contain a definition for 'deviceName' and no accessible extension method 'deviceName' accepting a first argument of type 'Control' could be found (are you missing a using directive or an assembly reference?)** – Lha Jun 28 '22 at 00:59
  • Have you declared static variable in global scope of your Control-parent form? You can also try to create property for that variable in parent form and access by property in child form. This is strange issue for my case, because I've tested out this code, everything was working perfectly – Jakobson Jun 28 '22 at 13:14