1

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);
    }
vidhi
  • 411
  • 1
  • 6
  • 19
  • 1
    See [My answer](http://stackoverflow.com/a/15389047/2109503) to a previous question. It will allow you to access the forms without having the parent/child necessity. The specific example was just for visibility, you could easy return the `TForm form` or just use the concept. – Infinite Possibilities Mar 15 '13 at 16:04
  • That won't work, since Form3 doesn't have a 0 param constructor either. Easiest thing to do - make a constructor for Form1 without the Form3 parameter. – Dragos Bobolea Mar 15 '13 at 16:04

2 Answers2

2

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.

Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
  • here form1 object works fine but when i want to access a combobox from Form3 in Form1 i cant get using _form3 – vidhi Mar 15 '13 at 16:31
  • Do you have the combobox as a field? You'll need to expose whatever you want other objects to access as public properties or use the `_form3.Controls`. I'll edit in an example. – Dustin Kingen Mar 15 '13 at 16:34
  • but in which constructor i should initialize the components? if in both it shows error because i have used a timer for face detaction so it shows error that **Attempted to read or write protected memory. This is often an indication that other memory is corrupt.** and after i use combobox like this **axWindowsMediaPlayer1.currentPlaylist = axWindowsMediaPlayer1.playlistCollection.getByName(_form3.smilecombo.SelectedItem.ToString()).Item(0);** it is not working – vidhi Mar 15 '13 at 16:47
  • You should initialize the controls in whichever form owns the controls. You can exposed properties on Form1 to get the data you need to initialize the control. – Dustin Kingen Mar 15 '13 at 16:52
1
 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();
 }
Emmanuel N
  • 7,350
  • 2
  • 26
  • 36
  • You need to create two default/empty constructors. In C# you loose default constructor when you create an additional constructor. – Emmanuel N Mar 15 '13 at 16:11
  • if i use `Application.Run(new Form1(new Form3()));` then it shows error that form3 does not contain contructor that takes 0 argument bcos inversely i also created object of form1 in form3. – vidhi Mar 15 '13 at 16:14
  • but if i made 2 empty contructor how can i made the object of from3 and how to access. i have no idea about it – vidhi Mar 15 '13 at 16:17