C#实现窗口之间的传值
作者:hebedich 时间:2022-05-26 08:28:14
为了解决在多个窗口之间的传值问题,我们可以通过设置静态类和静态变量的办法来实现窗口间值的传递
窗体一代码
//窗体1的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
sharedclass.sharedvalue = textBox1.Text.ToString(); //静态变量的用法:类名.变量名 赋值给静态变量
Form2 frm2 = new Form2();
frm2.Show();
}
}
public static class sharedclass //在命名空间设置一个静态类sharedclass,不要放置在form1前面
{
public static string sharedvalue; //设置一个静态变量sharedvalue
}
}
窗体2代码
//窗体2的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
textBox1.Text = sharedclass.sharedvalue; //静态变量传入给窗口2的textBox
}
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
标签:C#,窗口之间的传值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解JAVA中priorityqueue的具体使用
2022-05-17 22:53:50
![](https://img.aspxhome.com/file/2023/2/68222_0s.png)
浅析C#静态类,静态构造函数,静态变量
2022-04-10 03:33:14
Java日常练习题,每天进步一点点(40)
2022-12-20 14:39:53
![](https://img.aspxhome.com/file/2023/7/95977_0s.png)
Java中的复合数据类型
2022-10-05 08:08:08
![](https://img.aspxhome.com/file/2023/3/69273_0s.png)
基于SPRINGBOOT配置文件占位符过程解析
2021-06-27 04:25:12
SpringBoot自定义注解实现Token校验的方法
2023-11-13 23:17:52
Mybatis Plus select 实现只查询部分字段
2022-07-23 18:51:32
![](https://img.aspxhome.com/file/2023/9/61279_0s.jpg)
java Quartz定时器任务与Spring task定时的几种实现方法
2021-11-14 22:31:47
Java Exchanger并发类使用方法
2023-08-19 20:20:41
Spring MVC URL地址映射的示例代码
2022-12-09 19:53:33
![](https://img.aspxhome.com/file/2023/3/72903_0s.jpg)
C#先判断是否存在再创建文件夹或文件与递归计算文件夹大小
2023-07-29 00:04:58
![](https://img.aspxhome.com/file/2023/6/89806_0s.png)
java使用Socket类接收和发送数据
2021-05-26 15:28:47
Go Java算法之找不同示例详解
2021-10-29 19:37:08
解决RedisTemplate存储至缓存数据出现乱码的情况
2023-04-20 15:30:58
![](https://img.aspxhome.com/file/2023/8/92718_0s.jpg)
Windows系统中Java调用cmd命令及执行exe程序的方法
2021-11-27 23:00:02
java开发线上事故理解RocketMQ异步精髓
2023-07-25 07:41:20
![](https://img.aspxhome.com/file/2023/1/57501_0s.jpg)
Spring Data JPA中 in 条件参数的传递方式
2023-11-08 06:45:47
C#实现GZip压缩和解压缩入门实例
2021-05-29 08:20:45
Java正则验证正整数的方法分析【测试可用】
2022-08-02 21:50:05
![](https://img.aspxhome.com/file/2023/9/66229_0s.jpg)
java连接zookeeper实现zookeeper教程
2022-09-19 03:04:35
![](https://img.aspxhome.com/file/2023/7/77127_0s.png)