最近自己继续对WinForm进行学习,把自己学习中的一点体会写出来。
WinForm中的窗体传值有多种方法,自己结合相关资料总结了一下,大概有5种方式(或者更多):
1、通过 ShowDialog()进行传值;
2、通过改造构造函数进行传值(加参数);
3、通过公共静态类进行传值;
4、通过绑定事件进行传值;
5、使用Attribute(本人属初学,尚需深入研究,希望高手给与指正)
代码如下:
主窗体代码:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
namespace
WinTestValue
...
{
public partial class Main : Form
...{
public Main()
...{
InitializeComponent();
F004_ComonClass fc = new F004_ComonClass();
fc.Show();
}
方法1:通过ShowDialog进行页面传值#region 方法1:通过ShowDialog进行页面传值
private void btn_ok_Click(object sender, EventArgs e)
...{
F001_ShowDialog fs = new F001_ShowDialog();
if (fs.ShowDialog() == DialogResult.OK)
...{
this.tbx_value.Text = F001_ShowDialog.returnValue;
MessageBox.Show("从F002_ShowDialog传值到Main窗体成功!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btn_ok1_Click(object sender, EventArgs e)
...{
F001_ShowDialog fs = new F001_ShowDialog();
if (fs.ShowDialog() == DialogResult.OK)
...{
this.tbx_value.Text = fs.ReturnValue();
MessageBox.Show("从F002_ShowDialog传值到Main窗体成功!", "Success1", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btn_ok2_Click(object sender, EventArgs e)
...{
F001_ShowDialog fs = new F001_ShowDialog();
&nbs

本文介绍了作者在学习WinForm过程中总结的五种页面传值方式:1) 使用ShowDialog(),2) 通过带参数的构造函数,3) 利用公共静态类,4) 绑定事件,5) 使用Attribute(需进一步研究)。并提供了相关代码示例,同时表达了对Attribute部分深入理解的需求,期待高手指导。
最低0.47元/天 解锁文章
459

被折叠的 条评论
为什么被折叠?



