zoukankan      html  css  js  c++  java
  • C#窗体传值的集中方法,亲测可用,随便选

      1 //1、使用专门的数据存储类的静态全局字段
      //笔者经常用这种方式,因为平时开发的都是小程序
    2 1、定义一个类,DataClass.cs 3 2、定义一个全局变量 4 class DataClass 5 { 7 public static string UserName ;//用来存储用户的登录名,作为共享资源 8 } 9 10 3、给变量赋值 11 //把“登录窗口”的值传递给DataClass类的全局变量 12 DataClass.UserName = comboBox_User.SelectedItem.ToString(); 13 4、使用 14 15 //在“主窗口”显示登录用户 16 label_LoginUser.Text = DataClass.UserName; 17 18 19 //******************************************************************************************* 20 21 方法2:通过提供外部可访问的属性和DialogResult的状态来传值(这个例子同时实现了父传子、子传父)。 22 23 Form1中: 24 25 using System; 26 using System.Windows.Forms; 27 28 namespace WindowsForms跨窗体传值大全 29 { 30  public partial class Form1 : Form 31 { 32 public Form1() 33 { 34 InitializeComponent(); 35   } 36 37 private void button1_Click(object sender, EventArgs e) 38 { 39 Form2 f2 = new Form2(); 40  f2.TextBox2Value = textBox1.Text;//顺便把父窗体的值先传给子窗体 41 42 //这一行不需要再写f2.ShowDialog();或者f2.Show();,否则f2会弹出来两次 44 if (f2.ShowDialog() == DialogResult.OK)//这样的语句是合法的:DialogResult f = f2.ShowDialog(); 45 { 46 textBox1.Text = f2.TextBox2Value; 47 //f2.Close();//这一句写或者不写,f2都会关闭 48 } 49 } 50 } 51 } 52 53 //Form2中: 54 using System; 55 using System.Windows.Forms; 56 57 namespace WindowsForms跨窗体传值大全 58 { 59 public partial class Form2 : Form 60 { 61 public Form2() 62 { 63 InitializeComponent(); 64 } 65 66 public string TextBox2Value //将外部不可访问的textBox2.Text封装成属性TextBox1Value 67 { 68 set { textBox2.Text = value; } 69 get { return textBox2.Text; } 70 } 71 72 private void button2_Click(object sender, EventArgs e) 73 { 74 this.DialogResult = DialogResult.OK;//这里的DialogResult是Form2类对象的属性 75 } 76 77 } 78 } 79 80 //******************************************************************************************* 81 方法3:通过委托事件来传值。(这种方式可以实现不同控件数据实时联动的复杂功能) 82 83 Form1中: 85 using System; 86 using System.Windows.Forms; 87 88 namespace WindowsForms跨窗体传值大全 89 { 90 public partial class Form1 : Form 91 { 92 public Form1() 93 { 94 InitializeComponent(); 95 } 96 97 private void button1_Click(object sender, EventArgs e) 98 { 99 Form2 f2 = new Form2(); 100 f2.ChangeText += new ChangeTextHandler(Change_Text);//将事件和处理方法绑在一起,这句话必须放在f2.ShowDialog();前面 101 f2.ShowDialog(); 102 } 103 104 public void Change_Text(string str) 105 { 106 textBox1.Text = str; 107 } 108 } 109 } 110 111 Form2中: 112 using System; 113 using System.Windows.Forms; 114 115 namespace WindowsForms跨窗体传值大全 116 { 117 public delegate void ChangeTextHandler(string str); //定义委托 118 119 public partial class Form2 : Form 120 { 121 public Form2() 122 { 123 InitializeComponent(); 124 } 125 126 public event ChangeTextHandler ChangeText; //定义事件 127 128 private void button2_Click(object sender, EventArgs e) 129 { 130 if (ChangeText != null)//判断事件是否为空 131 { 132 ChangeText(textBox2.Text);//执行委托实例 133 this.Close(); 134 } 135 } 136 } 137 } 138 139 //******************************************************************************************* 140 可以用.NET提供的Action<>泛型委托和lambda表达式简化上面这个例子: 141 //Form1中: 142 using System; 143 using System.Windows.Forms; 144 145 namespace WindowsForms跨窗体传值大全 146 { 147 public partial class Form1 : Form 148 { 149 public Form1() 150 { 151 InitializeComponent(); 152 } 153 154 private void button1_Click(object sender, EventArgs e) 155 { 156 Form2 f2 = new Form2(); 157 f2.ChangeText = (str) => textBox1.Text = str;//用lambda表达式实现,这句话必须放在f2.ShowDialog();前面 158 f2.ShowDialog(); 159 } 160 } 161 } 162 163 164 //Form2中: 165 using System; 166 using System.Windows.Forms; 167 168 namespace WindowsForms跨窗体传值大全 169 { 170 public partial class Form2 : Form 171 { 172 public Form2() 173 { 174 InitializeComponent(); 175 } 176 177 public Action<string> ChangeText;//之前的定义委托和定义事件由这一句话代替 178 179 private void button2_Click(object sender, EventArgs e) 180 { 181 if (ChangeText != null)//判断事件是否为空 182 { 183 ChangeText(textBox2.Text);//执行委托实例 184 this.Close(); 185 } 186 } 187 } 188 }
  • 相关阅读:
    jQuery.extend()
    reconnecting-websocket.js
    网页防止嵌套
    mysql 一张表的多个字段关联另外一张表
    php把一些预定义的 HTML 实体转换为字符。
    weex用阿里矢量图
    nodeJS有多快
    关于jquery中prev()和next()的用法
    关于手动添加属性的方法总结
    关于一个div上下左右居中的css方法
  • 原文地址:https://www.cnblogs.com/MaZai/p/10280584.html
Copyright © 2011-2022 走看看