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


  • 相关阅读:
    (转)上海驾照到期换证流程及注意事项
    sql 获取批处理信息的脚本(优化器在处理批处理时所发生的优化器事件)
    C# xml通过xslt转换为html输出
    C#基础 继承和实例化
    sqlserver 获取数据库、表和字段相关信息
    Json反序列化为动态类型(dynamic)
    Aspose.cells常用用法1
    C# 生成缩略图 去除图片旋转角度
    C# 压缩图片到指定宽度,假如图片小于指定宽度 判断图片大小是否大于指定大小(KB) 如果大于则压缩图片质量 宽高不变
    C# 2个List<T>比较内部项是否相等
  • 原文地址:https://www.cnblogs.com/HeroBeast/p/849485.html
Copyright © 2011-2022 走看看