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
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
f.tt+=new printhand(print);
---〉
delegate string printhand(evtarg e);
f.tt += print或者f.tt += new f.test(print);
应该是f.tt += print;
也就是事件返回值通常封装在事件参数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); //触发事件
{
if(this.TestEvent != null)
{
label1.Text = this.TestEvent(e); //这里获取返回值
}
}
Form2
args c = new args(str);
f.TestEvent += print;
f.OnTestEvent(c); //触发事件
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.Str.ToString();
}
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);
}
}
}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 string 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);
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;
}
}
}
事件一般分三步执行
首先注册
也就是
form1.事件+= 你的函数
form1.事件满足触发条件
form1.你的函数
所以
public string print(args e)
{
return e.ToString();
}
--〉改为
public string print(....)
{
return this.sss;
}
求解
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;
}
}
}