zoukankan      html  css  js  c++  java
  • Winform 委托窗体传值

    有窗体Form1和窗体Form2,单击Form1按钮弹出Form2,单击Form2吧Form2的textBox控件文本传给Form1的label控件。

     

    窗体1里:

    实例化Form2,注册Form2的事件,并弹出窗体。

     1 public partial class Form1 : Form
     2     {
     3         public Form1()
     4         {
     5             InitializeComponent();
     6         }
     7 
     8         private void button1_Click(object sender, EventArgs e)
     9         {
    10             Form2 f2 = new Form2();
    11             f2.myevent += new Form2.myDelegate(getValue);//注册事件
    12             f2.Show();
    13         }
    14         //事件处理方法
    15         public void getValue(string text)
    16         {
    17             this.label1.Text = text;
    18         }
    19     }

    窗体2里:

    定义委托和事件,还有事件的调用。

     1     public partial class Form2 : Form
     2     {
     3         public delegate void myDelegate(string text);//委托
     4         public event myDelegate myEvent;//事件
     5 
     6         public Form2()
     7         {
     8             InitializeComponent();
     9         }
    10 
    11         private void button1_Click(object sender, EventArgs e)
    12         {
    13             if (myEvent != null)//判断
    14             {
    15                 myEvent(textBox1.Text);//调用事件
    16             }
    17         }
    18     }
  • 相关阅读:
    HDU 1022 Train Problem I
    HDU 1702 ACboy needs your help again!
    HDU 1294 Rooted Trees Problem
    HDU 1027 Ignatius and the Princess II
    HDU 3398 String
    HDU 1709 The Balance
    HDU 2152 Fruit
    HDU 1398 Square Coins
    HDU 3571 N-dimensional Sphere
    HDU 2451 Simple Addition Expression
  • 原文地址:https://www.cnblogs.com/xifengyeluo/p/6078286.html
Copyright © 2011-2022 走看看