如何从另一个Form中为Combobox SelectedValue赋值 [英] How assign a value to a Combobox SelectedValue from another Form

查看:158
本文介绍了如何从另一个Form中为Combobox SelectedValue赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表单表单1包含DataGridView,其中包含一个用于城镇ID的列,在表单2中我有一个ComboBox,我已经用DataTable将其名称填充为具有显示成员(TownName)和Value Member(Town Id)的DataSource )。

我的问题是我在表单2中创建了一个getter和setter,用于该ComboBox的Selected Value属性,从Form 1分配DataGidView中所选行的Town Id,所以在按钮单击在表单1中的事件我将列值给予该setter然后我打开表单2以显示ComboBox中的选定城镇但是没有任何反应ComboBox没有收到该值,我做的是正确的吗?还是有另一种方法来解决我的问题?

谢谢大家

解决方案

这是关于表单协作的热门问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用表单的整个实例。



请参阅我的过去的回答:如何复制所有的列表框之间的项目有两种形式 [ ^ ]。



如果对表单类使用部分声明,这将特别方便。请注意,如果您具有不同部分的类(可以在不同的文件中),则不需要显示所有继承列表。比如说,在一个部分中,您添加了基类,而另一部分则不需要它。当你实现一些接口时,将它添加到类的一部分的继承列表中(仅此而已),并在同一部分中实现此接口。



Pass此表单的实例作为另一个表单的接口接口。该表单现在可以通过此接口调用第一个表单的成员。在更罕见的情况下,您可以在两个协作形式中实现两个接口,并将接口引用传递给两者。我希望你有这个想法。



请参阅本讨论中的其他解决方案。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



第一个链接已修复。



-SA


由于这个问题非常受欢迎,我以前的答案往往不太清楚,可能还不够清楚,我决定写一个提示/特丽里k文章附有详细的代码示例和解释:一次回答的许多问题 - Windows窗体或WPF Windows之间的协作



-SA


从这里开始:在两个表单之间传输信息,第1部分:父母对孩子 [ ^ ]

I have two Forms Form 1 contains DataGridView with a Column for Town ID and in Form 2 i have a ComboBox that i already fill it with Town's names from a DataTable as DataSource with Display Member (TownName)and Value Member (Town Id).
My problem is i create in Form 2 a getter and setter for Selected Value Property of that ComboBox to assign to it the Town Id of the selected row in DataGidView from Form 1,so in button click event in form 1 i give the column value to that setter then i open form 2 to show the selected town in ComboBox but nothing happens the ComboBox doesn't receive that value, is what i did correct? or there is another method to solve my issue?
Thank you all

解决方案

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form.

Please see my past answer: How to copy all the items between listboxes in two forms[^].

This is especially convenient if you use partial declaration for form classes. Note that if you have the class in different parts (which can be in different files), you don't need to show all the inheritance list. In one part, say, you add the base class, in another part you don't need it. When you implement some interface, add it in the inheritance list of one part of the class (and nothing more), and implement this interface in the same part.

Pass the instance of this form as the interface interface to another form. That form can now call members of the first form through this interface. In more rare cases, you can implement two interfaces in two collaborating forms and pass the interface references to each one to both. I hope you got the idea.

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

[EDIT #] First link fixed.

—SA


As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA


Start here: Transferring information between two forms, Part 1: Parent to Child[^]


这篇关于如何从另一个Form中为Combobox SelectedValue赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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