C# 窗体间互相传值

C# 窗体间互相传值

Intent.cs是解决方案的核心,使用一个Key-Values的字典dict,这个字典用于存放窗体与窗体之间传递的数值,dict左边的key用string,右边的values用Object,这样面对不同的数值传递都可以应付。同时将这个字典dict直接设置成public static,调用不用这么麻烦。

设计效果:在form1 点击修改按钮后将打开form2,form2中有一个文本框,在输入完内容后无论是按回车键还是点击确认键,都会将输入的结果返回到form1的label1上,如果用户点击右上角的关闭按钮则不反馈。同时form1点击修改按钮后form2出现的位置在form1处,即form2会覆盖form1,form2修改确认完成后form1又会在form2的位置覆盖form2。

项目解决方案内容:

Intent.cs

using System;
using System.Collections.Generic;
using System.Text;
namespace test_transf_values
{
internal class Intent
{
public static Dictionary<string,Object> dict = new Dictionary<string,Object>();
}
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace test_transf_values
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//修改按钮
private void button1_Click(object sender, EventArgs e)
{
//声明要使用的窗体form2
Form2 form2 = new Form2();
//将form1 的当前位置压入Intent中的dict,通过其传给form2
Intent.dict["form1_top"] = this.Top;
Intent.dict["form1_left"] = this.Left;
Intent.dict["form1_label1_text"] = label1.Text;
if(form2.ShowDialog() == DialogResult.OK)
{
//该判断会等到form2被关闭之后才执行,如果form2返回一个OK值的话
//根据返回的值更新form1的位置
this.Top = (int)Intent.dict["form2_top"];
this.Left = (int)Intent.dict["form2_left"];
//更新label的值
label1.Text = Intent.dict["form2_textbox1_text"].ToString();
}
}
}
}

Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace test_transf_values
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
//初始化时,取出Intent中form1传来的值
//更新自己的位置
this.Top = (int)Intent.dict["form1_top"];
this.Left = (int)Intent.dict["form1_left"];
//更新textbox1的内容
textBox1.Text = Intent.dict["form1_label1_text"].ToString();
//一定是先有焦点,再选中
textBox1.Focus(); //设置焦点停留在textBox1中
textBox1.Select(); //同时全选中textBox1 中的所有文本
}
//确定 按钮
private void button1_Click(object sender, EventArgs e)
{
//关闭form2前,将要传递给form1 的值压入Intent中的dict
Intent.dict["form2_top"] = this.Top;
Intent.dict["form2_left"] = this.Left;
Intent.dict["form2_textbox1_text"] = textBox1.Text;
//同时设置返回值为OK,不设置的话默认返回Cancel
this.DialogResult = DialogResult.OK;
this.Close();
}
//输入完Enter可替代确定按钮
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
//一旦用户在输入过程中输入Enter则执行button1的确定按钮点击事件
if (e.KeyCode == Keys.Enter)
{
this.button1_Click(sender, e);
}
}
}
}

Form1.cs 设计和Form2.cs 设计:

image-20230316233837869

Form1:属性:

​ MaxmizeBox=False 禁用最大化

​ MinmizeBox=False 禁用最小化

​ StartPosition=CenterScreen

Form2:属性:

​ MaxmizeBox=False 禁用最大化

​ MinmizeBox=False 禁用最小化

​ 事件:

​ Form2_Load

​ textbox1 带有 Keydown事件

​ TextAlign = Center

效果:

image-20230316234520403

说明:

C#容器可以直接像python那样操作,比如Intent.dict["form2_top"] = this.Top;该语句,如果dict中没有form2_top这一项,就自动将this.Top这个值压入,如果存在就修改为this.Top,程序员无需考虑该项是否存在。

在窗体间互相传值关键在理解,form2.ShowDialog()之后,form1的代码是在这里中断,形成一个断点,在form2.ShowDialog()==DialogResult.OK即form2返回一个OK值后form1知道form2被关闭之后代码才继续向下执行。

form2.ShowDialog()之后form2是个模态对话框,就是form2不关闭,form1不能进行任何操作。

posted @   ben犇  阅读(135)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示