public partial class Form1 : Form
{
Form2 f2 ;
public Form1()
{
InitializeComponent();
f2 = new Form2(this);//此处将Form1传递到Form2中,在Form2的构造函数中接收
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
f2.Show();
}
public void Test()
{
MessageBox.Show("Form2 调用Form1中的方法");
}
}
Form2的代码:
public partial class Form2 : Form
{
Form1 f1 ;
public Form2(Form1 form1)//添加一个带参数的构造函数
{
InitializeComponent();
this.f1=form1;//在此处接收Form1的实例
}
private void button1_Click(object sender, EventArgs e)
{
f1.Test();
}
}
Form1 f1 = new Form1();
f1.ShowDialog();
alarmValue alarmValue = new alarmValue();
alarmValue.ShowDialog();
this.AddOwnedForm(alarmValue);
如是静态的:
Form1.方法名(参数);
非静态的:
Form1 frm=new Form1();
frm.方法名(参数);
form1中方法
public void ReName(string newname)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
listBox1.Items.Add(str3[0] + " " + newname);
}
form2中
frmMP3 mp = new frmMP3();
textBox1.Text = Console.Read().ToString();
mp.ReName(textBox1.Text);