110,539
社区成员
发帖
与我相关
我的任务
分享
class comclass
{
private BackgroundWorker bgw;
public delegate void DataChanged(object sender, DoWorkEventArgs e);
public event DataChanged datachanged;
public comclass()
{
bgw.DoWork += bgw_Dowork;
}
private void bgw_Dowork(object sender, DoWorkEventArgs e)
{
datachanged(sender, e);
}
}
public partial class Form1 : Form
{
private comclass cc=new comclass();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cc.datachanged += cc_datachanged;
}
private void cc_datachanged(object sender, DoWorkEventArgs e)
{
if ((string)e.Result != "")
{
this.Text = (string)e.Result;
}
}
}
只有框架,你参考一下吧。namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ComClass c = new ComClass();
c.action = new Action<string>(ChangeButtonName);
c.Do();
}
private void ChangeButtonName(string buttonName)
{
button1.Name = buttonName;
}
}
public class ComClass
{
public Action<string> action { get; set; }
public void Do()
{
if(action != null)
{
action("修改后的控件名");
}
}
}
}
可以用委托的方式
namespace testProject
{
class COMclass
{
//.................
public void showText(Form1 f1)
{
f1.label4.Text = "myValue";
}
}
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
COMclass comClass = new COMclass();
Form1 f1 = new Form1();
comClass.showText(f1);
}
}
}