zoukankan      html  css  js  c++  java
  • 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();
                if (fs.ShowDialog() == DialogResult.OK)
                ...{
                    this.tbx_value.Text = fs.RValue;
                    MessageBox.Show("从F002_ShowDialog传值到Main窗体成功!");
                }
            }
            #endregion

            方法2: 通过构造函数进行页面传值#region 方法2: 通过构造函数进行页面传值
            private void btn_gouzao_Click(object sender, EventArgs e)
            ...{
                F002_GouZao gz = new F002_GouZao(this);
                gz.Show();
            }
            #endregion
            
            //定义textbox属性
            public TextBox Tvalue
            ...{
                get
                ...{
                    return this.tbx_value;
                }
            }

            private void btn_cancel_Click(object sender, EventArgs e)
            ...{
                this.Close();
            }

            方法4:通过绑定事件进行传值#region 方法4:通过绑定事件进行传值
            private void btn_event_Click(object sender, EventArgs e)
            ...{
                F003_Event fe = new F003_Event();
                fe.myenent +=new MyEventHandler(fe_myenent);
                fe.ShowDialog();
            }
            private void fe_myenent(object sender, MyEvent e)
            ...{
                this.tbx_value.Text += e.MValue;
            }
            #endregion

            方法3:通过公共静态类进行传值#region 方法3:通过公共静态类进行传值
            private void btnComDate_Click(object sender, EventArgs e)
            ...{
                //F004_ComonClass fc = new F004_ComonClass();
                //fc.ShowDialog();
                this.tbx_value.Text = ComonData.sTextBox;
            }
            #endregion

            方法5:使用Attribute#region 方法5:使用Attribute
            protected override void WndProc(ref Message m)
            ...{
                if (m.Msg = "")
                ...{

                }
                base.WndProc(ref m);
            }

            #endregion
        }
    }

    F001_ShowDialog的代码:
    public partial class F001_ShowDialog : Form
        ...{
            //第一种类型
            //定义一个变量
            public static string returnValue;

            //第二种类型
            //定义一个方法
            public string ReturnValue()
            ...{
                return this.tbx_value.Text.Trim();
            }

            //第三种类型
            //定义一个属性
            private string _value;
            public string RValue
            ...{
                get
                ...{
                    return _value;
                }
                set
                ...{
                    _value = value;
                }
            }

            public F001_ShowDialog()
            ...{
                InitializeComponent();
            }

            private void btn_ok_Click(object sender, EventArgs e)
            ...{
                returnValue = this.tbx_value.Text.Trim();
            }

            private void btn_ok1_Click(object sender, EventArgs e)
            ...{
                _value = this.tbx_value.Text.Trim();
            }
        }

     

    F002_GouZao的代码:
        public partial class F002_GouZao : Form
        ...{
            //定义Main窗体的实例
            private Main main;

            //public F003_GouZao()
            //{
            //    InitializeComponent();
            //}

            public F002_GouZao(Main m)
            ...{
                InitializeComponent();
                main = m;
            }

            private void btn_ok_Click(object sender, EventArgs e)
            ...{
                main.Tvalue.Text = this.tbx_value.Text.Trim();
                MessageBox.Show("成功!");
            }
        }

     

    F004_ComonClass的代码:
        public partial class F004_ComonClass : Form
        ...{
            public F004_ComonClass()
            ...{
                InitializeComponent();
                //ComonData.sTextBox = this.tbxInfo.Text.Trim().ToString();
            }

            private void btn_data_Click(object sender, EventArgs e)
            ...{
                ComonData.sTextBox = this.tbxInfo.Text.Trim().ToString();
            }
        }

     

    F003_Event的代码:
        public partial class F003_Event : Form
        ...{
            //定义一个事件
            public event MyEventHandler myenent;
            public F003_Event()
            ...{
                InitializeComponent();
            }

            private void btn_ok_Click(object sender, EventArgs e)
            ...{
                if (myenent != null)
                ...{
                    myenent(this,new MyEvent(this.tbx_value.Text.Trim()));
                }
            }
        }

     

    自定义的委托和事件的代码:
        public delegate void MyEventHandler(object sender, MyEvent e);
        public class MyEvent : EventArgs
        ...{
            private string mvalue;
            public string MValue
            ...{
                get
                ...{
                    return mvalue;
                }
            }
            public MyEvent(string s)
            ...{
                mvalue = s;
            }
        }

     
        // 定义一个公共数据类
        public static class ComonData
        ...{
            public static string sTextBox;
        }

     

    Attribute部分还没完成,有些方面没有搞明白,过段时间再加上。
  • 相关阅读:
    javascript:让表单 文本框 只读,不可编辑的方法
    javascript从url中获取请求参数
    jQuery 效果函数,jquery文档操作,jQuery属性操作方法,jQuerycss操作函数,jQuery参考手册-事件,jQuery选择器
    IIS中添加MIME类型
    谷歌工作方法流程相关4本书
    发财、升职带来的幸福感很快会消失,量化研究幸福的5+3本书
    棉纺织品的历史就是工业革命和资本主义的历史:5星|《棉花帝国:一部资本主义全球史》(上)
    (严重剧透)被川岛芳子包养的民国老军医:3.5星|麦家《人生海海》
    3星|《医疗创业与投资启示录》:相关资料汇编
    3星|秦朔《文明寻思录第三辑》:时事点评文集,观点多事实少
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/2078971.html
Copyright © 2011-2022 走看看