如何将值从Windows窗体传递到另一个窗体(从子窗口到父窗口) [英] How I can pass values from a Windows Form to Another (from Child Window to Parent Window)

查看:70
本文介绍了如何将值从Windows窗体传递到另一个窗体(从子窗口到父窗口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

请给我任何我可以传递值的代码,示例或链接
从Windows窗体转换为另一个窗体.


提前谢谢.

提示:

Leonardo Ayala R.

Hello everybody,

Please give me any code, example or link, that I can pass values
from a windows form to another.


Thanks in advance.

Att:

Leonardo Ayala R.

推荐答案

您将在此讨论中找到我的解决方案和其他解决方案:
如何在列表框之间复制所有项目两种形式 [ ^ ].

注意:如果这些窗口是窗体,则没有子窗口"和父窗口".有所有权形式"和所有者形式".如果需要UI完整性,则需要使用此关系.

—SA
You will find my solution and other solutions in this discussion:
How to copy all the items between listboxes in two forms[^].

A note: there is no "child window" and "parent window" in case those windows are forms. There are "owned form" and the "owner form". You need to use this relationship if your need UI integrity.

—SA


在您的子窗体中,声明一个公开可见的变量.然后从父表单中,通过child.variable访问它.

公共类子窗体
{
public int变量;
}

在父级中,您可以通过
读取/传递值 child = new childform();
child.variable = 1;

您可能还希望将其声明为
静态公共int变量
如果您不希望先创建子窗体的实例.
In your child form, declare a publicly visible variable. Then from the parent form, access it by child.variable.

public class childform
{
public int variable;
}

in parent, you can read/pass the value by
child = new childform();
child.variable = 1;

You may also wish to declare it as
static public int variable
if you do not wish to create an instance of the childform first.


您可以创建一个Class,其中包含要存储的值.将Class作为参数传递给您的子窗体.
You could create a Class that holds the values you want to store. Pass the Class as a parameter to your child form.
MyClass cls = new MyClass();
MyForm frm = new MyForm(cls);
frm.Show(); // Or ShowDialog();
string something = cls.Value; // Although it probably won't have a value here yet...



所有这些当然都是非常基本的.NET内容...您应该考虑阅读一些书和/或 ^ ]:thumbsup:



All of this is of course pretty basic .NET stuff... You should consider reading some books and/or articles[^] :thumbsup:


这篇关于如何将值从Windows窗体传递到另一个窗体(从子窗口到父窗口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆