zoukankan      html  css  js  c++  java
  • WinForm中页面传值的方式

    最近自己继续对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进行页面传值

            
    方法2: 通过构造函数进行页面传值
            
            
    //定义textbox属性
            public TextBox Tvalue
            
    {
                
    get
                
    {
                    
    return this.tbx_value;
                }

            }


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


            
    方法4:通过绑定事件进行传值

            
    方法3:通过公共静态类进行传值

            
    方法5:使用Attribute
        }

    }

    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部分还没完成,有些方面没有搞明白,过段时间再加上。


  • 相关阅读:
    HDU 2236 无题Ⅱ
    Golden Tiger Claw(二分图)
    HDU 5969 最大的位或 (思维,贪心)
    HDU 3686 Traffic Real Time Query System (图论)
    SCOI 2016 萌萌哒
    Spring Boot支持控制台Banner定制
    构建第一个Spring Boot程序
    Spring Boot重要模块
    Java fastjson JSON和String互相转换
    BCompare 4 Windows激活方法【试用期30天重置】
  • 原文地址:https://www.cnblogs.com/HeroBeast/p/849485.html
Copyright © 2011-2022 走看看