get value from child from to parent form

2019-08-08 21:44发布

i have 2 form in my media player project i have make object of from1 (parent form) and by that get value from form1 in form3. but i also need to get value of a variable from form3 to form1. but the problem is that when i make the object of form3 in form1 like this

 Form3 m_child;

    public Form1(Form3 frm3)
    {
        InitializeComponent();
        m_child = frm3; 
    }

it shows the error in the program.cs that from1 does not contain a constructor that contain 0 argument. i know i have to pass there a parameter in Application.Run(new Form1());

but what i should pass i have no idea. plz help if is there any solution or any other way to get the value from child to parent form.

this is my code for form3 now i want to use value of smileplay, surpriseplay ,sadplay,normalplay,ambiguousplay in form1

 Form1 m_parent;
    public Form3(Form1 frm1)
    {
        InitializeComponent();
        m_parent = frm1; 
    }

    private void Form3_Load(object sender, EventArgs e)
    {
       WMPLib.IWMPPlaylistArray allplaylist= m_parent.axWindowsMediaPlayer1.playlistCollection.getAll();
       for (int litem = 0; litem < allplaylist.count; litem++)
       {
         smilecombo.Items.Add( allplaylist.Item(litem).name);
         surprisecombo.Items.Add(allplaylist.Item(litem).name);
         sadcombo.Items.Add(allplaylist.Item(litem).name);
         normalcombo.Items.Add(allplaylist.Item(litem).name);
         ambiguouscombo.Items.Add(allplaylist.Item(litem).name);
       }
    }

    private void savebtn_Click(object sender, EventArgs e)
    {
      WMPLib.IWMPPlaylist smileplay=   m_parent.axWindowsMediaPlayer1.playlistCollection.getByName(smilecombo.SelectedItem.ToString()).Item(0);
      WMPLib.IWMPPlaylist surpriseplay = m_parent.axWindowsMediaPlayer1.playlistCollection.getByName(surprisecombo.SelectedItem.ToString()).Item(0);
      WMPLib.IWMPPlaylist sadplay = m_parent.axWindowsMediaPlayer1.playlistCollection.getByName(sadcombo.SelectedItem.ToString()).Item(0);
      WMPLib.IWMPPlaylist normalplay = m_parent.axWindowsMediaPlayer1.playlistCollection.getByName(normalcombo.SelectedItem.ToString()).Item(0);
      WMPLib.IWMPPlaylist ambiguousplay = m_parent.axWindowsMediaPlayer1.playlistCollection.getByName(ambiguouscombo.SelectedItem.ToString()).Item(0);
    }

2条回答
我想做一个坏孩纸
2楼-- · 2019-08-08 22:09

I would add a new constructor.

public Form3 : Form
{
    public Form1 Parent { get; set; }

    // TODO: Replace object with the proper type.
    public object SomeComboboxValue
    {
                    // TODO: Replace with the value you want to access.
        get { return SomeComboBox.PropertyYouWantToAccess; }
    }

    public Form3()
    {
        InitializeComponent();
    }

    public Form3(Form1 form1)
    {
        InitializeComponent();
        Parent = form1; 
    }
}

public Form1 : Form
{
    private Form3 _form3;

    public Form1()
        :this(new Form3())
    {
    }

    public Form1(Form3 form3)
    {
        _form3 = form3;
        _form3.Parent = this;
    }
}

Then Application.Run(new Form1()); will work just fine.

查看更多
时光不老,我们不散
3楼-- · 2019-08-08 22:13
 public Form1(Form3 frm3)
 {
    InitializeComponent();
    m_child = frm3; 
 }

You need to supply Form3 object when initializing form1

Something like:

Application.Run(new Form1(new Form3()));

Or the easy way will be to create two new empty Constructor. In C# when you create a constructor you loose default constructor.

Add these two constructors:

 public Form1()
 {
    InitializeComponent();
 }

 public Form3()
 {
    InitializeComponent();
 }
查看更多
登录 后发表回答