现在的位置: 首页 > 综合 > 正文

WinForm中页面传值的方式

2012年04月24日 ⁄ 综合 ⁄ 共 1320字 ⁄ 字号 评论关闭
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);
         

抱歉!评论已关闭.