C#:如何从另一个自定义控件访问自定义控件的公共成员

发布于 2024-11-02 08:08:44 字数 581 浏览 1 评论 0原文

我正在制作 Windows 申请表。我有一个 CustomControl(比如 MasterControl),在上面放置了一个分割面板,现在我的 MasterControl 分为三个部分:

  • Pannel1
  • Pannel2
  • Pannel3

现在我开发了三个自定义控件,并在每个面板中放置一个,例如

  • Pannel1 有 CustomControl1
  • Pannel2 有 CustomControl2
  • Pannel3有 CustomControl3

现在,在 CustomControl3 中的某个位置,我需要访问 CustomControl1 的公共成员。为此,我编写了以下代码:

((MasterControl)this.Parent)._oCustomControl1.PublicMember = this.PublicMember;

上面的代码在我的情况下不起作用。当这行代码在调试模式下执行时,会出现一个消息框并指出“当前位置没有可用的代码”

I am working on windows application form. I have a CustomControl (say MasterControl) on which i put a split panel and now my MasterControl is split into three parts say:

  • Pannel1
  • Pannel2
  • Pannel3

Now i develop three custom controls and put one in each of pannels e.g

  • Pannel1 have CustomControl1
  • Pannel2 have CustomControl2
  • Pannel3 have CustomControl3

Now somewhere in CustomControl3 I need to access a public member of CustomControl1. For which i wrote the following code:

((MasterControl)this.Parent)._oCustomControl1.PublicMember = this.PublicMember;

The code above doen't work in my case. When this line of code is executed in debug mode then a message box appears and states that "There is no code available for current location"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月下客 2024-11-09 08:08:44

对于您的控件来说,依赖于父容器上的排列方式是一个非常糟糕的设计。

例如,在第三个控件中,您通过从父级访问第一个控件来查询第一个控件的属性,然后按名称访问它的子控件。

如果可以编译,您的代码将很容易被破坏 - 我认为您遇到的问题是编译顺序:为了编译您的父窗体,它需要完成子用户控件。另一方面,您创建的用户控件需要具有完整的表单。

最好从这些控件的容器中设置您想要的任何行为 - 例如,通过对来自控件的事件做出反应,并在适当的其他控件上设置适当的内容(当然还有其他方法 - 要点在于信息的方向和流动——谁在设置和使用什么)。

It's a really bad design for your controls to depend on how are the arranged on the parent container.

e.g. inside your third control, you are quering the property of the first one by accessing it from the parent, and then it's child control by name.

Your code will break very easily, if it can be compiled at all - I think the problem you're having is the order of compilation: in order for your parent form to be compiled, it needs to have child user controls finished. On the other hand the user controls you created need to have finished form.

It would be far better to set whatever behaviour you're after from the container of those controls - for example, by reacting to events from the control, and setting appropriate stuff on appropriate other controls (there are other ways as well ofcourse - the point is in the direction and flow of information - who's setting and using what).

谜泪 2024-11-09 08:08:44

如果您的主控制器中有一个分割面板,您应该向上两级找到您的主控制器:

((MasterControl)this.Parent.Parent)._oCustomControl1.PublicMember = this.PublicMember;

If you have a split panel in your master control, you should go two levels up to find your master control:

((MasterControl)this.Parent.Parent)._oCustomControl1.PublicMember = this.PublicMember;
风追烟花雨 2024-11-09 08:08:44

我自己找到了答案。我在这里发表评论是因为它可能对其他人有帮助。
确切的代码是:

((MasterControl)this.Parent.Parent.Parent)._oCustomControl1.PublicMember = this.PublicMember;

基本上我的 coustomcontrol3 位于一个拆分容器面板内,所以当我写道:
this.Parent 然后它指向它所在的面板,如果我写
this.Parent.Parent 然后它指向上面面板所在的分割器容器,如果我写了
this.Parent.Parent.Parent 然后它指向这个分割容器所在的控件

我从“Farzin Zaker”的回答中得到了这个想法,所以感谢他的贡献

I found the answer by myself. I am positing here because it might help some one else.
The exact code is:

((MasterControl)this.Parent.Parent.Parent)._oCustomControl1.PublicMember = this.PublicMember;

Basically my coustomcontrol3 lies inside a split container panel, so when i wrote:
this.Parent then it points to Panel In which it is residing and if i wrote
this.Parent.Parent then it points to the spliter container in which above panel reside and if i wrote
this.Parent.Parent.Parent then it points to control in which this split container resides

I got the idea from "Farzin Zaker" answer, so thanks to him for his contribution

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文