且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中以其他形式更改控件属性

更新时间:2023-10-12 21:15:28

返回您的表单,并将所有这些修饰符都更改回private.

当您在窗体上公开控件和字段时,您会将窗体的设计与使用它的对象相关联-您无法更改窗体的工作方式,而不必非常仔细地考虑它可能对其他窗体或类产生的影响.这是解决错误和维护麻烦的秘诀.不要这样做.

有两种适当"的方法可以做到这一点:

1)在表单中创建公共属性,以更新表单控件.您的表单可以使他们更改其内部工作方式,而不会影响外部世界.

2)在您的表单可以订阅的外部世界中创建事件.这些将指示数据已更改",并使更新在表单内完成.
Go back to your form, and change all those modifiers back to private.

When you expose controls and fields on your form, you tie the design of the form to what uses it - you cannot change how the form works any more without having to think very carefully about any possible affect it may have on other forms or classes. This is a recipe for bugs and maintenance hassles. Do not do it.

There are two "proper" ways to do this:

1) Create public properties in your form which up-date your form controls. Your form can them change how it works internally, without affecting the outside world.

2) Create events in the outside world which your form can subscribe to. These would indicate "data changed" and cause the updates to be done inside the form.


使用静态全局类,该类包含您的表单可以设置/获取的属性,并将访问器改回不论是私有的还是受保护的(无论它们是什么).
Use a static global class that contains properties that your forms can set/get, and change the accessors back to either private or protected (whatever they were).


这是关于表单协作的流行问题.最可靠的解决方案是在表单类中实现适当的接口.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [
好吧,唯一有帮助的是经验. :-)
如果您不能遵循Griff和John提供的简单说明,详细的代码示例将无济于事.在这种情况下,我只建议一件事:停止尝试创建真实的窗口化应用程序.首先,学习控制台应用程序上的所有基本技术和非常简单的代码,当您真正有信心时,请回到其他内容.

—SA
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Sorry, I saw your post about being inexperienced developer. First, please never post non-answers as the "solution". Such posts will be removed, no one will get e-mail notification. Instead, add comment, comment on existing comment, use "Improve Question".

Well, the only thing which helps is getting experienced. :-)
Detailed code sample won''t help you much, if you cannot follow such simple instructions as those provided by Griff and John. In this case, I could advise just one thing: stop attempting to create a real-life windowed application. First, learn all the basic techniques on console applications and very simple codes, come back to anything else when you are really confident.

—SA