js怎樣跨頁面傳遞數據,winform+c#之窗體之間的傳值

 2023-12-06 阅读 27 评论 0

摘要:窗體傳值可以分為兩類。1、主窗體往子窗體傳值有兩種方法,一種是在子窗體提供重載構造函數,利用重載構造函數傳遞值,適用于傳值數量比較少;第二種是,在子窗體中定義一個主窗體對象,然后就可以接收到主窗體的屬性值了,適用于

窗體傳值可以分為兩類。
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();
????????????
????????}

????}

js怎樣跨頁面傳遞數據。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();
????????????
????????}

????}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/190125.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息