首页 新闻 会员 周边

委托 事件 调用 用户控件

0
悬赏园豆:5 [已解决问题] 解决于 2013-09-01 21:54
 public delegate void getMessage();
    public partial class UserControl1 : UserControl
    {
        public event getMessage GMS;
        public UserControl1()
        {
            InitializeComponent();
        }
        
        protected void issuer()
        {
            if (GMS != null)
                GMS();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.issuer();
        }
    }
 public partial class Form1 : Form
    {
        
        public Form1()
        {
           
            InitializeComponent();
            UserControl1 uc = new UserControl1();
            uc.GMS += new getMessage(get);
         
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public void get()
        {
            this.label1.Text = "尼玛";
        }

       
    }

点击 按钮的时候label的之值没有变化 看了下是因为那个GMS事件为空,不知道它怎么为空的,求大哥指点

牧人羊的主页 牧人羊 | 初学一级 | 园豆:5
提问于:2013-09-01 14:47
< >
分享
最佳答案
0

这是WinForm程序吗?我看了一下,你这个是没有搞清楚对象之间的关系导致,

  public Form1()
        {
           
            InitializeComponent();
            UserControl1 uc = new UserControl1();
            uc.GMS += new getMessage(get);
         
        }

你这里Register的这个UserControl1和你界面上的UserControl1是两个不同的对象,你点击的是你界面上的,但是你注册的另外一个对象,你修改成这样子看看怎么样:

  public Form1()
        {
           
            InitializeComponent();
            UserControl1 uc = this.UserControl1;//不知道语法是不是这样子,希望意思能够明白。
            uc.GMS += new getMessage(get);
         
        }

其他委托以及事件的语法没有问题。

或者直接这样子:

  public Form1()
        {
           
            InitializeComponent();
            this.UserControl1 .GMS += new getMessage(get);
         
        }
收获园豆:3
小AI | 菜鸟二级 |园豆:354 | 2013-09-01 15:46

非常感谢 是个这个原因。 

牧人羊 | 园豆:5 (初学一级) | 2013-09-01 17:03

@行者之刃: 木事,木事,给分给分啊。。再多给点啊。。哈哈哈

小AI | 园豆:354 (菜鸟二级) | 2013-09-01 18:12

@小AI: 只有五分哦,一人给点吧

牧人羊 | 园豆:5 (初学一级) | 2013-09-01 21:54
其他回答(2)
0

public delegate void getMessage();这是委托

 public event getMessage GMS;这是声明事件

uc.GMS += new getMessage(get);这是注册事件

 public void get()这是处理函数

那么谁触发事件?
     

收获园豆:1
LiloT | 园豆:6 (初学一级) | 2013-09-01 14:55

private void button1_Click(object sender, EventArgs e) { this.issuer(); }这个不是触发吗

支持(0) 反对(0) 牧人羊 | 园豆:5 (初学一级) | 2013-09-01 14:59

@行者之刃: 这是谁的成员?

支持(0) 反对(0) LiloT | 园豆:6 (初学一级) | 2013-09-01 15:32

@行者之刃: 

您的component有问题吧

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
          

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 uc = new Form2();
            uc.Show();
            uc.GMS += new getMessage(this.gett);

        }
        public void gett()
        {
            this.label1.Text = "ninhao";

        }
    }

   public delegate void getMessage();
    public partial class Form2 : Form
    {
        public event getMessage GMS;
        public Form2()
        {
            InitializeComponent();
        }
        protected void issuer()
        {
            if (GMS != null)
                GMS();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.issuer();
        }
 
    }

支持(0) 反对(0) LiloT | 园豆:6 (初学一级) | 2013-09-01 16:22

@行者之刃: 是自定义控件。

支持(0) 反对(0) LiloT | 园豆:6 (初学一级) | 2013-09-01 16:25
0

看样子已经解决了

收获园豆:1
jerry-Tom | 园豆:4077 (老鸟四级) | 2013-09-01 21:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册