[英].net add item from listbox to another project in the same solution
I'm developping a .NET 4 project with more than one projects. 我正在开发一个包含多个项目的.NET 4项目。 The principal project is a Windows Application within a listbox.
主要项目是列表框中的Windows应用程序。 In the same solutions I have a Class Library project.
在相同的解决方案中,我有一个类库项目。
In the code behind in my windows form, I call a function in my class library. 在Windows窗体后面的代码中,我在类库中调用一个函数。
Can I pass my listbox control from my project to my another project in the same solution and add to it items? 是否可以将列表框控件从我的项目传递到同一解决方案中的另一个项目并添加到项目中?
thank you 谢谢
Indeed you can. 确实可以。
But first add a reference to System.Windows.Controls in your Class Library. 但是首先要在类库中添加对System.Windows.Controls的引用。 Then you can have a public method on one of your classes
然后您可以在一个类上使用公共方法
public void DoSomethingToMyListBox(ListBox list)
{
list.Clear(); //or whatever
}
However. 然而。 I would ask you to think about what it is you want to do with this listbox, and why you would need to pass it into another module - generally speaking that's a sign of a flaw in your design.
我想请您考虑使用此列表框要做什么,以及为什么需要将其传递到另一个模块中-通常来说,这表明您的设计存在缺陷。
If for example you want to pass it in because you want to fill it with some items that are coming from a database, and you want to isolate the main Forms project from the database access (a good thing) then you should have your Class Library returning data
and you should use the Windows Forms DataBinding components instead. 例如,如果您要传递它是因为您要用数据库中的某些项目填充它,并且想要将主Forms项目与数据库访问隔离(一件好事),那么您应该拥有类库返回
data
,则应改用Windows Forms DataBinding组件。
eg 例如
//on your Form's code
public Form1()
{
var myThing =new MyClass(); //whatever the class in the class library is called
var theData = MyClass.GetData(); //GetData returns List<something>
MyListBox.DataSource=theData;
//if "something" is a class, use the two lines below
//if something is just a list of strings for example, you don't need to bother
MyListBox.DisplayMember="name of property on something to display in the list";
MyListBox.ValueMember="name of property on something to use as list index";
}
Of course I don't know if this is what you want to do :) 当然我不知道这是否是您想要做的:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.