惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
T
The Blog of Author Tim Ferriss
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
Engineering at Meta
Engineering at Meta
量子位
I
InfoQ
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog
J
Java Code Geeks
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
V
V2EX
腾讯CDC
P
Proofpoint News Feed
A
About on SuperTechFans
爱范儿
爱范儿
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI

博客园 - pensir

ef报错:实体类型XXX不是当前上下文的模型的一部分。 让AutoCAD.NET支持后台线程 将cad嵌入到vb中 sql server 2005 msxml安装失败解决及msxml 6.0 卸载 智能替换DataTable.Select中会导致错误的单引号 - pensir - 博客园 CADVBA代码移植到.NET 最全ASCII码对照表(备用) C# 删除字符串中任何位置的空格 - pensir - 博客园 在VB.NET中使用MS Access存储过程(转载) Access存储过程,环境:VB 2005+.NET2.0+ACCESS2003(转载) 关键性代码整理 WinForm窗体间传值 WinForm主要控件分类一览 MaskedTextBox掩码元素一览 ArcGIS中的坐标系统 Geodatabase组织结构 GIS关键词 栅格数据与矢量数据 ArcGIS的文件结构
winform+c#之窗体之间的传值 - pensir - 博客园
pensir · 2008-05-05 · via 博客园 - pensir

窗体传值可以分为两类。
1、主窗体往子窗体传值
有两种方法,一种是在子窗体提供重载构造函数,利用重载构造函数传递值,适用于传值数量比较少;第二种是,在子窗体中定义一个主窗体对象,然后就可以接收到主窗体的属性值了,适用于传值数量大。
主窗体代码如下:

 public partial class frmParent : Form
    
{
        
private string strValueA = "";
        
public string StrValueA
        
{
            
get
            
{
                
return this.strValueA;
            }

            
set this.strValueA = value; }
        }

        
public frmParent()
        
{
            InitializeComponent();
        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            
this.strValueA = textBox1.Text;
            frmChild frmchild 
= new frmChild();
            frmchild.Owner 
= this;
            frmchild.ShowDialog();
            frmchild.Dispose();
        }


        
private void button2_Click(object sender, EventArgs e)
        
{
            frmChild frmchild 
= new frmChild(this.textBox1.Text);
            
string returnValue = "";
            
if (frmchild.ShowDialog() == DialogResult.OK)
            
{
                returnValue 
= frmchild.Str;
                
this.textBox1.Text = returnValue;
            }

        }

    }

子窗体代码如下:

public partial class frmChild : Form
    
{
        
private string str;
        
public string Str
        
{
            
get return this.str; }
            
set this.str = value; }
        }

        
private frmParent frmparent;

        
public frmChild()
        
{
            InitializeComponent();
        }

        
public frmChild(string str)
        
{
            
this.str = str;
            InitializeComponent();
            
this.textBox1.Text = str;
        }

        
private void frmChild_Load(object sender, EventArgs e)
        
{
            frmparent 
= (frmParent)this.Owner;
            
//this.textBox1.Text = frmparent.StrValueA;
        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            
//frmparent = (frmParent)this.Owner;
            this.Str = this.textBox1.Text;
            
this.DialogResult = DialogResult.OK;
            
this.Close();
            
        }

    }

2、从子窗体返回值到主窗体中
利用了子窗体的属性保存子窗体的值,在主窗体中可以访问到子窗体的属性
主窗体代码如下:

 public partial class frmParent : Form
    
{
        
private string strValueA = "";
        
public string StrValueA
        
{
            
get
            
{
                
return this.strValueA;
            }

            
set this.strValueA = value; }
        }

        
public frmParent()
        
{
            InitializeComponent();
        }

        
private void button2_Click(object sender, EventArgs e)
        
{
            frmChild frmchild 
= new frmChild(this.textBox1.Text);
            
string returnValue = "";
            
if (frmchild.ShowDialog() == DialogResult.OK)
            
{
                returnValue 
= frmchild.Str;
                
this.textBox1.Text = returnValue;
            }

        }

    }

子窗体代码如下:

public partial class frmChild : Form
    
{
        
private string str;
        
public string Str
        
{
            
get return this.str; }
            
set this.str = value; }
        }

        
private frmParent frmparent;

        
public frmChild()
        
{
            InitializeComponent();
        }

 
        private void frmChild_Load(object sender, EventArgs e)
        
{
            frmparent 
= (frmParent)this.Owner;
            
//this.textBox1.Text = frmparent.StrValueA;
        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            
//frmparent = (frmParent)this.Owner;
            this.Str = this.textBox1.Text;
            
this.DialogResult = DialogResult.OK;
            
this.Close();
            
        }

    }