如何从form1到form2获取ComboBox1.text。 [英] How to Get ComboBox1.text from form1 to form2.

查看:196
本文介绍了如何从form1到form2获取ComboBox1.text。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在我的应用程序form1和form2中形成


form1有combobox和form2有文本框


我想要检索值组合框到文本框由

设置如下属性。

I have to form in my application form1 and form2

form1 having combobox and form2 having textbox

I wants to retrieve the value of combobox to textbox by
setting property like below.

展开 | 选择 | Wrap | 行号

推荐答案

你写的代码将允许你完全按照你想说的去做,我害怕我不明白问题是什么。你能告诉我们一些关于你要做什么的更多信息,或许还包括你的表格创建方式的代码吗?


谢谢!


(注意,或者你可以让表单1在组合框的值改变时触发一个事件,然后表单2监听该事件并相应地做出响应)
The code you have written will allow you to do exactly as you''ve said you want to do, I''m afraid I don''t understand what the problem is. Can you tell us a little more about what you''re trying to do, perhaps include the code for how your forms are created?

Thanks!

(Note, alternatively you could have form 1 trigger an event when the combo box has its value changed, then form 2 listens for that event and responds accordingly)


代码将如果你让它静止就可以工作。


public static string comboString ....


你也可以在form1中做一些声明public static string variable,它将存储comboBox.Text属性的值。

在form2中使用form1的静态变量并将其值设置为textBox.Text属性。
The code will work if you make it static.

public static string comboString....

You can also do something like, in form1 declare a public static string variable which will store the value of comboBox.Text property.
In form2 use the static variable of form1 and set its value to textBox.Text property.


小心公共静态对象,因为它们实际上是一个全局变量。您可能不希望访问此类信息的类将无法控制它。另一个缺点是,如果你有多个该类的实例,静态成员将只包含一个值(说实话,如果有两个实例,我不知道它会得到什么)。如果在没有声明实例的情况下尝试访问它,你也会得到null。


通常情况下,这是一种编程习惯。通常认为传递对象的引用以从特定实例获取信息或使用某种事件更好。


显然,这是你的程序并且你可以做你想做的事情......这些指南更能防止你陷入常见的陷阱并调试可能发生的问题。坚持这些通常是一个好主意,并且不需要花费太多精力。
Be careful with public static objects as they are effectively a global variable. Classes that you may not wish to have access to this information will and you can''t really control it. Another downside is that if you ever have more than one instance of that class, the static member will only contain one value (and to be honest, I''m not sure which it would get if there were two instances). You''d also get null if you tried to access it when no instances had been declared.

Typically, this is frowned upon as a programming practice. It''s often thought better to pass a reference to your object to get information from that particular instance, or to use events of some sort.

Obviously, it''s your program and you can do what you want... these guidelines are more a way to prevent you from falling into common pitfalls and debugging issues that can occur. It''s usually a good idea to adhere to them and doesn''t take much effort.


这篇关于如何从form1到form2获取ComboBox1.text。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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