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

解决方案 »

  1.   

       f.tt += print(c);这句代码报错
      

  2.   

    delegate void printhand(evtarg e);
    f.tt+=new printhand(print);
      

  3.   

    delegate void printhand(evtarg e);
    ---〉
    delegate string  printhand(evtarg e);
      

  4.   

    事件后面一般是直接跟 函数或者 委托的。
    f.tt += print或者f.tt += new f.test(print);
      

  5.   

    兄弟不是这么用啊,
    应该是f.tt += print;
      

  6.   

    按照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);  //触发事件
      

  7.   

    public void OnTestEvent(args e)
    {
        if(this.TestEvent != null)
        {
             label1.Text = this.TestEvent(e);   //这里获取返回值
        }    
    }
     
    Form2
    args c = new args(str);
    f.TestEvent += print;
    f.OnTestEvent(c);  //触发事件
      

  8.   

    我目前的代码 该如何把c 从form2传到form1中
      

  9.   

    我目前的代码 该如何把c 从form2传到form1中 
      

  10.   

    我目前的代码 该如何把c 从form2传到form1中 
      

  11.   

    form1是调用事件的  form2是注册事件的   我想把参数从form2 传到form1
      

  12.   

    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.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;
            }
        }
    }
      

  13.   


    事件一般分三步执行
    首先注册
    也就是
    form1.事件+= 你的函数
    form1.事件满足触发条件
    form1.你的函数
    所以
      public string print(args e)
            {
     
                return e.ToString();
            }
    --〉改为
    public string print(....)
    {
    return this.sss;
    }
      

  14.   

    因为我定义的委托就是传进去一个args参数的。求解
      

  15.   

    触发消息要有时机,form1调用那个事件函数时,要确保form2已经把它挂街上
      

  16.   

    那么 该怎么调用Form1Sho我这个函数呢?
      

  17.   

    那么 该怎么调用Form1Show 这个函数呢? 
    求解
      

  18.   

    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;
            }
        }
    }