我在我的媒体播放器项目中有2个表单我已经创建了from1(父表单)的对象,并且从form3中的form1获取值。但我还需要从form3到form1获取变量的值。但问题是,当我在form1中创建form3的对象时,就像这样
Form3 m_child;
public Form1(Form3 frm3)
{
InitializeComponent();
m_child = frm3;
}
它显示 program.cs 中的错误 from1不包含包含0参数的构造函数。我知道我必须在Application.Run(new Form1());
但我应该通过的我不知道。 plz帮助,如果有任何解决方案或任何其他方式来获取从子窗体到父窗体的值。
这是我的form3的代码现在我想使用smile1,surpriseplay,sadplay,normalplay,formint中的模糊玩法的价值
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);
}
答案 0 :(得分:2)
我会添加一个新的构造函数。
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;
}
}
然后Application.Run(new Form1());
就可以了。
答案 1 :(得分:1)
public Form1(Form3 frm3)
{
InitializeComponent();
m_child = frm3;
}
初始化form1时需要提供Form3对象
类似的东西:
Application.Run(new Form1(new Form3()));
或者简单的方法是创建两个新的空构造函数。在C#中创建构造函数时,您会松开默认构造函数。
添加这两个构造函数:
public Form1()
{
InitializeComponent();
}
public Form3()
{
InitializeComponent();
}