简体   繁体   English

在同一解决方案中访问来自其他项目的表单的控件

[英]To access controls of a form from a different project in same solution

I want access controls in a form which is in a different project in the same solution.I have designed in a such a way that one solution has different projects in it. 我希望访问控制的形式位于同一解决方案的不同项目中。我设计的方式是,一个解决方案中包含不同的项目。

I have tried the following code. 我尝试了以下代码。

           Form form =  (Form)Application.OpenForms["frm"];
           if (form != null)
           {

             Button cs = (Button)((form.Controls["btn"]);
               cs.Enabled = false;
           }

Using this code am able to locate the form,but am not able to access controls from it."cs" is returning null value and throwing "null refernce exception error".What could be the possible error and how should I modify the code.Its a windows application using c# visual studio 2010. Any suggestion would be of great help. 使用此代码能够找到该窗体,但无法从中访问控件。“ cs”返回空值并抛出“空引用异常错误”。可能是什么错误,我应该如何修改代码。它是使用c#visual Studio 2010的Windows应用程序。任何建议都会有很大帮助。 Thanks 谢谢

Ignoring why you might want to do this in the first place, I would recommend: 我不建议您首先为什么要这样做,我建议:

  1. Add the first project as a reference to the second 将第一个项目添加为对第二个项目的引用
  2. Add a public property to the form you need to access to controls from 将公共属性添加到您需要从中访问控件的表单中
  3. Get the form as you currently are 获取当前的表格
  4. Cast the form to its appropriate/actual type 将表单转换为适当/实际的类型
  5. Use the public property. 使用公共财产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以从同一解决方案中的不同项目访问另一个项目的嵌入式资源吗? - Can I access another project's Embedded Resource from a different project in the same solution? 我如何从C#中同一项目下的不同Windows窗体访问不同的组件? - How can i access the different component from different windows form which are under same project in c#? c#从同一解决方案中的其他项目运行表格 - c# run form from other project in same solution 在相同解决方案下从另一个项目访问文件 - access file from another project under same solution 从同一解决方案MVC中的不同项目访问EF ConnectionString - Accessing EF ConnectionString from different project within same solution MVC Visual Studio工具箱不包含来自同一解决方案的另一个项目的用户控件 - Visual Studio Toolbox Not Including User Controls from Another Project from Same Solution 钩子和测试项目在不同的项目中,但在相同的解决方案中 - Hooks and test project are in different project but in same solution 区分发布相同表格的不同控件 - Distinguish between different controls posting the same form 访问位于不同项目但在相同解决方案中的文件 - Access a file which is in diffrent project but in same solution 如何在同一解决方案中访问另一个项目的服务 - How to access service of another project in same solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM