在Main.cs中使用控件 [英] Using controls in Main.cs

查看:190
本文介绍了在Main.cs中使用控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个Main.cs表单,它上面有一系列按钮和控件.现在,通常,最好为正在处理的所有内容创建一个类文件,例如,我有一个串行端口图标,当我单击主窗体时,它应该启动串行端口通信.单击此按钮后,它应该调用串行端口类(我应该提到的是它自己的文件),并且我将拥有一系列独立于Main.cs的这些类(它们将位于一般为空).

所以我想知道有一种方法可以从我将要创建的任何类文件中访问Main窗体中的控件,如按钮和文本框等.方案

基本上,控件只是类的实例,因此,如果在运行时创建它们并将它们存储到变量,集合等中,则以后可以通过变量使用相同的控件.如果在主窗口中定义了变量,则只要主窗口正在运行,就可以通过任何方法在主窗口中对其进行访问.当然,如果您将新创建的控件添加到Windows控件集合中,则也可以通过该集合进行访问.更好地理解这一点的一种好方法是查看生成的Main.Designer.cs.


我建​​议您退后一步,看看您的类.您的思维过程在这里几乎是正确的.您绝对希望为诸如串行端口类之类的东西使用单独的类.您不希望做的是在那些操作UI的类中包含代码.功能代码必须正确区分域和UI逻辑.在Main.cs代码中,您已经在Serial Port类中启动了一些过程.您可能希望做的是从头开始,但是当串行端口类中的过程完成时,请在运行完成后通过检查串行端口类中的适当值来从Main.cs中更新您的UI.

您应该做的是创建一组表单可以订阅的自定义事件,让其他类发布事件,然后表单可以订阅它们.这样,您的类无需了解有关使用它们的UI组件的任何知识.它要做的就是发布事件,如果有订阅者,这些订阅者将做他们需要做的事情.


Say I have a form Main.cs, and it has a series of buttons and controls on it. Now generally it is good practice to create a class file for everything that is being handled, for example I have a serial port Icon, when I click in the main form, it should start the serial port communications. In this button click, it should call the Serial port Class (which I should mention is a file in its own), and I will have a series of these classes which are on their own separated from Main.cs (which will just be in general empty).

So what I would like to know is there a way to access the controls in the Main form like buttons and Text boxes and the likes from any of the class files that I will create????

解决方案

Basically controls are just instances of classes, so if you create them at runtime and store them to a variable, collection etc you can later use the same control via the variable. If the variable is defined in your main window it''s accessible in the main window in any method as long as the main window is running. Of course, if you add the newly created control to your windows controls collection, it''s accessible via that collection also. One good way to understand this more thoroughly is to have a look at the generated Main.Designer.cs.


My suggestion to you would be to take a step back and look at your classes. Your thought processes are close to being correct here. You absolutely want to have separate classes for things like your Serial Port class. What you do NOT want to do is have code in those classes manipulating UI. Proper separation of your domain and UI logic is imperative to functional code. In your Main.cs code you already kick off some process in the the Serial Port class. What you may wish to do is start as you have, but when the process in Serial port class is complete, update your UI from within Main.cs by checking the appropriate values in the Serial Port class after the run is complete.


What you should do is create a set of custom events that the form can subscribe to, make the other classes post events, and then the form can subscribe to them. That way, your classes don''t need to know anything about the UI component that uses them. All it has to do is post events, and if there are any subscribers, those subscribers will do what they need to do.


这篇关于在Main.cs中使用控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆