Form1中的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 跨窗体调用控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
void f2_SetMainFormTopMost(bool topmost) //回调函数
{
label1.Text = "调用成功";
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.SetMainFormTopMost += new SetMainFormTopMostHandle(f2_SetMainFormTopMost);//给form2中的委托实例SetMainFormTopMost 注册方法
f2.Show();
}
}
}
Form2中的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 跨窗体调用控件
{
public delegate void SetMainFormTopMostHandle(bool topmost); //定义委托
public partial class Form2 : Form
{
public SetMainFormTopMostHandle SetMainFormTopMost;//定义委托实例
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SetMainFormTopMost(true);//执行委托实例
}
}
}
C# 跨窗体调用控件
最新推荐文章于 2023-08-28 17:31:41 发布