简体   繁体   English

.net将列表框中的项目添加到同一解决方案中的另一个项目

[英].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.

相关问题 动态地在同一解决方案中添加项目对另一个项目的引用 - Add project's reference to another project in same solution dynamically 将用户控件从一个项目复制到同一解决方案中的另一个项目 - Copy User Control from one project to another within same solution 如何从同一解决方案检查另一个项目的版本 - How to check the version of another project from the same solution 从同一解决方案中的另一个项目访问 wwwroot 文件夹 - Access wwwroot folder from another project within the same solution 从同一解决方案中的另一个项目访问 appsettings.json 很热吗? - Hot to access appsettings.json from another project in same solution? 从同一解决方案C#中的另一个项目中读取配置属性 - Read config properties from another project in the same solution C# 我可以在同一解决方案中使用.NET Framework 4.5.2和4.6.2的另一个项目吗? - Can I have a project which uses .NET Framework 4.5.2 and another project with 4.6.2 in the same solution? 在解决方案资源管理器中将项添加到解决方案或项目节点的“添加”菜单 - Append item to Add menu of solution- or project node in Solution Explorer 如何在同一解决方案中访问另一个项目的服务 - How to access service of another project in same solution 如何在同一解决方案中执行另一个项目? - How to execute another project within the same solution?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM