从另一个类访问表单控件而不是创建它们的类 [英] Accesing form controls from another class than the one tha created them

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

问题描述




我有一个带有一些控件的表单,还有一个不同的类需要在运行时修改一些控件属性。


Hoy可以引用来自所以我可以访问它的控件和

因此可以修改它的属性吗?


问候,


迈克

Hi,

I have a form with some controls, and a different class that needs to
modify some control properties at run time.

Hoy can I reference the from so I have access to its controls and
therefore being able to modify its properties?

Regards,

Mike


推荐答案

" Mike" < mi ** @ hitnext.comschrieb
"Mike" <mi**@hitnext.comschrieb




我有一个带有一些控件的表单,另外一个需要

的类在运行时修改一些控件属性。


Hoy可以引用from,所以我可以访问它的控件和

因此可以修改其属性?
Hi,

I have a form with some controls, and a different class that needs
to modify some control properties at run time.

Hoy can I reference the from so I have access to its controls and
therefore being able to modify its properties?



将表格参考传递给班级。

Armin

Pass the Form reference to the class.
Armin


10月25,上午11:34,迈克 < m ... @ hitnext.comwrote:
On Oct 25, 11:34 am, "Mike" <m...@hitnext.comwrote:




我有一个带有一些控件的表单,以及一个不同的类需要在运行时修改一些控件属性。


Hoy可以引用from,所以我可以访问它的控件和

因此可以修改其属性吗?


问候,


Mike
Hi,

I have a form with some controls, and a different class that needs to
modify some control properties at run time.

Hoy can I reference the from so I have access to its controls and
therefore being able to modify its properties?

Regards,

Mike



我个人的偏好是保持控件的私密性,并且只有

通过属性公开必要的功能。这可以防止

外部对象修改超过你希望修改的



例如,如果你想要第二个表格能够修改标签'

文本属性这样暴露它:


//////// ////

公共财产MyLabelText()字符串

获取

返回MyLabel.Text

结束获取

设置(ByVal值为字符串)

MyLabel.Text = value

结束获取

结束财产

///////////


如果您给他们一个完整的标签参考,没有什么

会阻止他们调用form.MyLabel.Dispose()或其他东西

你并不打算。换句话说,使用属性维护

封装。


谢谢,


Seth Rowe

My personal preference is to keep the Controls private, and only
expose the necessary functionality via properties. This prevents an
outside object from being able to modify more than you want to have
modified.

For example, if you want a second form to be able to modify a label''s
text property expose it like this:

////////////
Public Property MyLabelText() As String
Get
Return MyLabel.Text
End Get
Set (ByVal value As String)
MyLabel.Text = value
End Get
End Property
///////////

If instead you gave them a complete reference to the label, nothing
would stop them from calling form.MyLabel.Dispose() or something else
you didn''t intend. In other words, using a property maintains
encapsulation.

Thanks,

Seth Rowe


好的,我做了,从第二张表格购买,如何参考表格''

属性?


这是一个直接的电话,如:


frmMain.Myproperty = MyValue





Dim frm As new frmMain

with frmMain

.MyProperty = MyValue

结束


我试过两个似乎都没有。


谢谢!

-----原帖---- -

来自:rowe_newsgroups [mailto:ro ******** @ yahoo.com]

发表于:2007年10月25日星期四下午2:01

发布到:microsoft.public.dotnet.languages.vb

对话:从另一个类访问表单控件而不是创建它们的一个类



主题:Re:从另一个类中访问表单控件而不是另一个类

cre他们是他们的b / b 10月25日上午11点34分,迈克 < m ... @ hitnext.comwrote:
Ok, I did, buy from the 2nd form, how do I reference the forms''
property?

Is it a direct call, like:

frmMain.Myproperty = MyValue

or

Dim frm As new frmMain
With frmMain
.MyProperty = MyValue
End With

I have tried both an none of them seem to work.

Thanks!
-----Original Message-----
From: rowe_newsgroups [mailto:ro********@yahoo.com]
Posted At: Thursday, October 25, 2007 2:01 PM
Posted To: microsoft.public.dotnet.languages.vb
Conversation: Accesing form controls from another class than the one tha
created them
Subject: Re: Accesing form controls from another class than the one tha
created them

On Oct 25, 11:34 am, "Mike" <m...@hitnext.comwrote:




我有一个带有一些控件的表单,以及一个不同的类需要在运行时修改一些控件属性。


Hoy可以引用from,所以我可以访问它的控件和

因此可以修改其属性吗?


问候,


Mike
Hi,

I have a form with some controls, and a different class that needs to
modify some control properties at run time.

Hoy can I reference the from so I have access to its controls and
therefore being able to modify its properties?

Regards,

Mike



我个人的偏好是保持控件的私密性,并且只有

通过属性公开必要的功能。这可以防止

外部对象修改超过你希望修改的



例如,如果你想要第二个表格能够修改标签'

文本属性这样暴露它:


//////// ////

公共财产MyLabelText()字符串

获取

返回MyLabel.Text

结束获取

设置(ByVal值为字符串)

MyLabel.Text = value

结束获取

结束财产

///////////


如果您给他们一个完整的标签参考,没有什么

会阻止他们调用form.MyLabel.Dispose()或其他东西

你并不打算。换句话说,使用属性维护

封装。


谢谢,


Seth Rowe

My personal preference is to keep the Controls private, and only
expose the necessary functionality via properties. This prevents an
outside object from being able to modify more than you want to have
modified.

For example, if you want a second form to be able to modify a label''s
text property expose it like this:

////////////
Public Property MyLabelText() As String
Get
Return MyLabel.Text
End Get
Set (ByVal value As String)
MyLabel.Text = value
End Get
End Property
///////////

If instead you gave them a complete reference to the label, nothing
would stop them from calling form.MyLabel.Dispose() or something else
you didn''t intend. In other words, using a property maintains
encapsulation.

Thanks,

Seth Rowe


这篇关于从另一个类访问表单控件而不是创建它们的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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