如何从类访问表单控件 [英] How do i access form controls from a class

查看:117
本文介绍了如何从类访问表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的Windows窗体应用程序

I have a standard windows form app

我正在将我的代码组织到类中

i am organising my code into classes

但是我无法访问控件在类的主窗体上。

However i am unable to access the controls on the main form from the classes.

表单myform = new Form(); //不起作用,因为这会创建一个不引用我现有形式的新对象

---------------------- --------------

Form myform = new Form(); // does not work as this creates a new object that does not refer to my already existing form
------------------------------------

表单MyForm  = Application.OpenForms.OfType< MainAppForm>()。Single(); //这不起作用

----------------------------------- --------

Form MyForm = Application.OpenForms.OfType<MainAppForm>().Single(); // this doesn't work
-------------------------------------------

var MyForm = Application.OpenForms.OfType< MainAppForm>()。Single();  //这确实有效¥b $ b ----------------------------------- ----------------

var MyForm = Application.OpenForms.OfType<MainAppForm>().Single(); // this does work
---------------------------------------------------

我的问题是,为什么写作时从其他类访问表单控件这么困难单独的类是MVC应该如何工作的一部分。



使用单独的类连接到现有表单控件似乎很荒谬需要这样复杂和复杂的代码...... 。



当然必须有一个更简单的答案。

My question is though why is it so difficult to access the form controls from other classes when writing separate classes is part of how MVC is supposed to work.

it seems ridiculous that connecting to an existing form controls using separate classes requires such complicated and convoluted code....

Surely there must be a simpler answer to this.

推荐答案

我好根据我不是专家,如果你在MVC窗口工作,那么处理这种情况非常复杂。 
well i'm not an expert according to me, if you are working in window form MVC, then it is very complex to handle such situation. 


这篇关于如何从类访问表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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