如何在未在c#中声明的形式中使用变量的值? [英] how to use the value of a variable in a form in which it is not declared in c#?

查看:68
本文介绍了如何在未在c#中声明的形式中使用变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式。变量VAR在form1中声明,我想在form2中使用它的值。我怎么能这样做?

解决方案

首先你不能声明变量(使用var关键字)。

一种方法是只需使用代理人在表单之间传递数据,

让调用表单将其注册为事件并完成。



谢谢,

Debata


这里有几件事:

1)你想要访问哪个VAR实例 - 可以有几个。

2)为什么Form1让Form2访问它的变量。



要从Form2访问Form1上的内容,你需要两件事: Form1的实例,它包含您关注的变量,并且Form1有意地公开它以便您可以访问它。我将以相反的顺序处理这些。



公开它的简单方法是将其声明为 public

 public string myString =你好,我是Form1中的一个刺痛; 

但这有问题,不建议使用,因为它将两种形式联系在一起 - 您无法在不考虑对Form2(以及可能的Form3,Form4等)的影响的情况下更改Form1在内部的工作方式。这违反了OOP的原则,通常被认为是一个坏主意。

更好的解决方案是在Form1中使用 public 属性:

 private string myString =你好,我是Form1中的一个刺痛; 
public string MyString
{
get {return myString; }
set {myString = value; }
}

它做了很多相同的事情,但允许你改变内部处理字符串的方式,并允许你检查值等等以确保你的代码会工作。



实例是另一回事。也许,当您创建Form2的实例时,您是从Form1执行的,反之亦然。如果您从Form2创建Form1并将其显示为对话框,那么您已准备好实例,并且可以使用属性,就像使用OpenFileDialog一样:

 Form1 f = new Form1(); 
if(f.ShowDialog()== DialogResult.OK)
{
Console.WriteLine(f.MyString);
}

如果没有,那么你必须在创建时将Form1实例传递给Form2。



或更好,信号Form2中Form1订阅的事件,并传递EventArgs中的信息 - 这样Form2可以决定是否需要这些信息。


这是一个非常流行的表单协作问题,I不知道为什么。



最强大的方法是在表单类中实现适当的接口。

请参阅我过去的答案中的详细信息:

如何以两种形式复制列表框之间的所有项目 [ ^ ]。



请参阅其他建议和讨论。



还有一件事:超过一个非米odal形式不太好。尝试在一个主窗体的控件中设计所有应用程序。您可以显示,隐藏或停靠表单的某些部分(面板,标签页,等等)。推荐。



祝你好运,

-SA


i have two forms. a variable VAR is declared in form1, iwant to use its value in form2. how can i do this?

解决方案

First of all you are not allowed to declare variable (using var keyword).
One way is just use a delegate to pass data between forms,
Let the calling form register it as an event and you are done.

Thanks,
Debata


There are a few things here:
1) Which instance of VAR do you want to access - there can be several.
2) Why should Form1 let Form2 access it's variables.

To access something on Form1 from Form2 you need two things: the instance of Form1 that holds the variable you are concerned with, and for Form1 to deliberately expose it so that you can access it. I'll deal with these in reverse order.

The simple way to expose it is to declare it as public:

public string myString = "Hello, I'm a sting in Form1";

But this has problems, and is not recommended because it ties the two forms together - you cannot change the way Form1 works internally without considering the effects on Form2 (and potentially Form3, Form4 and so on). This is against the principles of OOP and is generally thought of as a bad idea.
A better solution is to use a public property in Form1:

private string myString = "Hello, I'm a sting in Form1"; 
public string MyString 
   { 
   get { return myString; } 
   set {myString = value; }
   }

which does much the same thing, but allows you to change the way the string is handled internally if you need to, and allow you to check values and so forth to ensure your code will work.

Instances are another thing all together. Probably, when you created your instance of Form2, you did it from Form1, or vice versa. If you create Form1 from Form2 and display it as a dialog, then you have the instance ready, and a property is the way to go, just as you do with a OpenFileDialog:

Form1 f = new Form1();
if (f.ShowDialog() == DialogResult.OK)
   {
   Console.WriteLine(f.MyString);
   }

If not, then you have to pass the Form1 instance to Form2 when you create it.

Or better, signal an event in Form1 that Form2 subscribes to, and pass the information in the EventArgs - that way Form2 can decide if it wants the information.


This is a very popular question of form collaboration, I don't know why.

The most robust way is implementing appropriate interface in the form class.
Please see detail in my past answer:
How to copy all the items between listboxes in two forms[^].

Please see also other suggestions and the discussion.

One more thing: more then one non-modal form is not so good. Try to design all the application in the controls of one main form. You can show, hide or dock away parts of the form (panels, tab pages, whatever). Just a recommendation.

Good luck,
—SA


这篇关于如何在未在c#中声明的形式中使用变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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