从从子窗体到父窗体获取值
本文关键字:窗体 获取 | 更新日期: 2023-09-27 18:04:10
我的媒体播放器项目中有两个表单,我有from1的make对象(父表单(,然后从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的代码,现在我想在form1 中使用smileplay、surpriseplay、sadplay、normalplay和ambiguolyplay的值
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);
}
我会添加一个新的构造函数。
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());
就可以正常工作了。
public Form1(Form3 frm3)
{
InitializeComponent();
m_child = frm3;
}
初始化form1 时需要提供Form3对象
类似于:
Application.Run(new Form1(new Form3()));
或者简单的方法是创建两个新的空构造函数。在C#中,当您创建构造函数时,您会丢失默认构造函数。
添加这两个构造函数:
public Form1()
{
InitializeComponent();
}
public Form3()
{
InitializeComponent();
}