读书人

c#事件,该怎么处理

发布时间: 2013-09-10 13:42:18 作者: rapoo

c#事件

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

namespace WindowsFormsApplication1
{

public class args : EventArgs
{
private string str;

public string Str
{
get { return str; }
set { str = value; }
}
public args(string s)
{
str = s;
}


}
}



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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string s;

public string S
{
get { return s; }
set { s = value; }
}
public Form1()
{
InitializeComponent();
}
public delegate string test(args e);
public event test tt;
private void Form1_Load(object sender, EventArgs e)
{
if (null != this.tt)


{
label1.Text = this.tt.ToString(); ;
}

}
}
}





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


private void Form2_Load(object sender, EventArgs e)
{

}
public void ShowMessage(object sender, EventArgs e)
{
MessageBox.Show("another form call this method!");
}
public string print(args e)
{

return e.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
f.S = "ssss";
string str = "null";
args c = new args(str);
f.tt += print(c);


f.ShowDialog();
}
}
}



红色代码报错。我想知道 带返回值的事件该如何使用。 c# delegate
[解决办法]
delegate void printhand(evtarg e);
---〉
delegate string printhand(evtarg e);
[解决办法]
事件后面一般是直接跟 函数或者 委托的。
f.tt += print


或者

f.tt += new f.test(print);

[解决办法]
兄弟不是这么用啊,
应该是f.tt += print;

[解决办法]
按照MS推荐的做法,事件通常封装为EventHandler<T> T : EventArgs
也就是事件返回值通常封装在事件参数e中,比如,用你的args.Str作为返回值,可以这么写
public event EventHandler<args> TestEvent;
public void OnTestEvent(args e)
{
if(this.TestEvent != null)
{
return label1.Text = this.TestEvent(e); //这里获取返回值
}
}

Form2
args c = new args(str);
f.TestEvent += print;
f.OnTestEvent(c); //触发事件

[解决办法]
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();


}

private void Form2_Load(object sender, EventArgs e)
{

}
public void ShowMessage(object sender, EventArgs e)
{
MessageBox.Show("another form call this method!");
}
public args print(args e)
{

return e; // 返回参数
}


private void button1_Click_1(object sender, EventArgs e)
{
Form1 f = new Form1();
string str = "null";
args c = new args(str);
f.tt += print;
f.Form1Show(c); //传c
}
}
}


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 WindowsFormsApplication1
{




public partial class Form1 : Form
{
public string s { get; set; }

public Form1()
{
InitializeComponent();
}
public delegate args test(args e); //返回类型


public event test tt;

private void Form1_Load(object sender, EventArgs e)
{

}

public void Form1Show(args e)
{

if (null != tt)
{
label1.Text = tt(e).Str.ToString();
this.Show();
}

}

}


public class args : EventArgs
{
private string str;

public string Str
{
get { return str; }
set { str = value; }
}
public args(string s)
{
this.str = s;
}
}
}

读书人网 >C#

热点推荐