Windows窗体组织代码(windows forms organize code)

编程入门 行业动态 更新时间:2024-10-23 15:32:57
Windows窗体组织代码(windows forms organize code)

好的,这是我的第一个Windows窗体应用程序,我在重构我的代码时面临一些困难。 开始将所有内容放在默认提供的Form1类中,它已经变得太快了。 我知道这是一个非常糟糕的编程习惯,所以我开始进行一些清理并制作了不同的类来满足单一责任原则。 但是,由于一些与按钮相关且与网格相关的事件在Form1类中运行良好,因此将它们带到外面会使它们崩溃,我无法完成它应该如何完成。

public partial class Form1 { public Form1() { InitializeComponent(); } private void method1(object sender, EventArgs e) { //CODE } private void method2(object sender, DataGridViewCellEventArgs e) { //CODE } private void method3(object sender, EventArgs e) { //CODE } }

所以我想将method1 , method2 , method3等移动到不同的类(或不同的类),但在不破坏应用程序的情况下如何做到这一点几乎失败了。 好吧,私有显然设置为公共授予访问权限,但我不知道如何在Form1类中调用这些方法。 当我创建新类然后尝试调用事件上的方法时,如onClick: Newclass.method1()它不起作用。 我能想到的一件事就是在Form1 onClick: thisMethodCallsTheOneINeedInADifferentClass()调用一些方法Form1 onClick: thisMethodCallsTheOneINeedInADifferentClass() ,但这似乎是一个笨拙的解决方案...我敢肯定有更好的方法来解决这个我只是不知道如何。 建议表示赞赏,谢谢

Okay, so this is my first windows forms app and I'm facing some difficulties re-structuring my code. started off putting everything in the Form1 class that provided by default, it has grown too large quickly. I'm aware this is a pretty bad programming practice so I started some clean up and made different classes to satisfy single responsibility principle. however, as some button-related and grid-related events are working well in the Form1 class, taking them outside makes them crash and I'm clueless how it should be done.

public partial class Form1 { public Form1() { InitializeComponent(); } private void method1(object sender, EventArgs e) { //CODE } private void method2(object sender, DataGridViewCellEventArgs e) { //CODE } private void method3(object sender, EventArgs e) { //CODE } }

so I'd like to move method1, method2, method3 etc to a different class (or different classes), but pretty much lost with how to do this without breaking the app. well, private is obviously set to public to grant access but I have no idea how to call these methods afterwards in Form1 class. when I created new classes and then tried to call the methods on event, like onClick: Newclass.method1() it didn't work. one thing I could think of is like calling some method in Form1 onClick: thisMethodCallsTheOneINeedInADifferentClass(), but this seems to be a clumsy solution... I'm sure there are better ways to sort this out I just don't know how to. Advice appreciated, thank you

最满意答案

您应该在表单中保留事件处理程序,但您可以将代码移动到另一个类。 如果你想给你的班级打电话,你就不能直接打电话给你。 你需要做这样的事情:

YourClass class1 = new YourClass();

您也可以将其声明为静态,然后您只需要调用一次。

private static YourClass class1 = new YourClass();

然后你可以像这样使用你的方法:

class1.YourMethod();

You should keep the event handlers in the Form, but you can move the code to another class. If you want to call your class you cant directly call it. You need to do something like this:

YourClass class1 = new YourClass();

You can also declare it static then you only need to call it once.

private static YourClass class1 = new YourClass();

Then you can use your method like this:

class1.YourMethod();

更多推荐

本文发布于:2023-04-27 14:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327159.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   代码   组织   Windows   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!