c#中有关委托事件的问题

csdnwy1258 csdnwy1258 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我想在一个类里调用主窗体中的控件,所以采用的了委托和事件,但是在执行过程中textBox1.Text有值却不能在窗体控件中显示,请教各位指点,下面是我做的一个简单的代码,贴出来请大家帮忙看看,谢谢!

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 demon
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Class1 cla = new Class1();
cla.FormDisplay();
}

string str = "";
public void Display(int ii)
{
textBox1.Text += ii.ToString();
}

}
}



Class类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace demon
{
class Class1
{

public delegate void Datadelegate(int str);
public event Datadelegate Dataevent;

int[] idata = { 12, 13, 24, 15, 16, 23, 14, 10, 22, 18 };
public void Statr ()
{

for (int i = 0; i < 10;i++ )
{

if ( idata[i]< 20)
{
if (Dataevent != null)
{
Dataevent(idata[i]);

}
}
}
}

public void FormDisplay()
{
Form1 f = new Form1();
Dataevent += f.Display;
Statr();
}
}
}
...全文

相关推荐

评论 12
收藏 已收藏
分享
返回顶部
csdnwy1258
C# 111,129 成员 642,540 内容