从另一个类访问一个表单上的标签
我知道这是一个令人眼花缭乱的明显问题,但我是 c# 新手,发现它有点令人困惑,所以任何帮助将不胜感激......
我有一个主窗体和第二个窗体,我们称之为 form2 。 form2上有一些标签。我想从 mainform 类/cs 表中控制这些标签的文本。无论我如何尝试,我似乎都无法访问它们。我已经将其中一些设置为公开,但我仍然无法在 mainform 类中“看到”它们。
mainform是wpf(项目也是如此)。 form2 是一个常规的 winform。
如果有人能帮助我,我将永远感激不已。
谢谢,
Possible Duplicate:
Best way to access a control on another form in WinForms?
I know this is a blinding obvious question but I am a c# newbie and find it a little confusing, so any help would be greatly appreciated...
I have a mainform and a second form, let's call it form2. form2 has some labels on it. I would like to control the text of those labels from the mainform class / cs sheet. No matter what I try, I cannot seem to access them. I have set some of them to public already and I still can't "see" them in the mainform class.
mainform is wpf (as is the project). form2 is a regular winform.
If someone would help me out I would be eternally grateful.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的标签是私有的,因此只能从所有者表单访问它们。不要试图将它们更改为公共,这是错误的做法(公共成员是邪恶的)。
添加一个公共方法来更新您的标签,以便可以从您的第二个表单访问它。
表格2:
主表格:
As yours labels are private, they can be accessed from the owner form only. Do not try to change them to public, it's a wrong approach (public members are evil).
Add a public method that updates your labels so it can be accessed from your second form.
form2:
mainform: