vb.net 从其他类 .net 访问控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19553123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing controls from other classes .net
提问by Nefariis
I know this has been asked a million times and Ive tried 4-5 different solutions but none of them seem to produce any results.
我知道这已经被问了一百万次,我尝试了 4-5 种不同的解决方案,但似乎没有一个产生任何结果。
I have a main form entitled "QoE" and I have two classes entitled "Utils" and and "Tests"
我有一个名为“QoE”的主表单,我有两个名为“Utils”和“Tests”的类
Tests calls a public shared sub from Utils using Utils.ProgressBar()
测试使用 Utils.ProgressBar() 从 Utils 调用公共共享子
Utils.ProgressBar() updates a progress bar control from the main form entitled QoE
Utils.ProgressBar() 从名为 QoE 的主窗体更新进度条控件
The ProgressBar modifier option is set to Public but I can't access the control directly from Utils (which I thought I was able to do in the past). Option 2 was to attempt to use this in the Utils class:
ProgressBar 修饰符选项设置为 Public 但我无法直接从 Utils 访问控件(我认为我过去可以这样做)。选项 2 是尝试在 Utils 类中使用它:
Dim f1 as New QoE()
f1.ProgressBarMain.Increment(+1)
f1.ProgressPercent.Text = f1.ProgressBarMain.Value.ToString() & "%"
but it doesn't produce anything.
但它不会产生任何东西。
option 3-5 was creating a module, a public static class, and trying to put the public shared update sub on the Main form itself. With these options though I usually get the "cannot refer to an instance member of a class from within a shared method" error.
选项 3-5 是创建一个模块,一个公共静态类,并尝试将公共共享更新子放在 Main 窗体本身上。使用这些选项,虽然我通常会收到“无法从共享方法中引用类的实例成员”错误。
So what am I missing? I would love some help. Thanks Guys.
那么我错过了什么?我希望得到一些帮助。谢谢你们。
Edit
编辑
You're really just splitting hairs here, this is completely valid question even without code, but here is the code non the less:
你真的只是在这里分裂头发,即使没有代码,这也是完全有效的问题,但这里是代码:
Public Class QoE
End Class
Public Class Utils
public shared sub ProgressBar
Dim f1 as New QoE()
f1.ProgressBarMain.Increment(+1)
f1.ProgressPercent.Text = f1.ProgressBarMain.Value.ToString() & "%"
'QoE.ProgressBarMain.Increment(+1) Returns The error mentioned in the comments
end sub
End Class
Public Class Tests
public shared sub DoWork
Utils.Progressbar()
End Sub
So why cant I access the controls from the QoE form when everything is set to public? End Class
那么当一切都设置为公开时,为什么我不能从 QoE 表单访问控件?结束班
采纳答案by Nefariis
In order to call controls from other classes you have to initiate the form and all the controls.
为了从其他类调用控件,您必须启动表单和所有控件。
Public Class Form1
Public Shared f as Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
f = Me
End Sub
End Class
Then in other classes just refer to f normally like
然后在其他类中通常只引用 f 就像
Form1.f.Textbox1.Text = ""
or
或者
Form1.f.CheckBox1.Checked = True
Enjoy
享受
For some additional reading:
一些额外的阅读:
回答by Dominique Lambert
Another solution would be to loop through open forms :
另一种解决方案是循环打开表单:
For Each FORM_ITEM As Form In Application.OpenForms
If FORM_ITEM.Name = "Form1" Then
With CType(FORM_ITEM, Form1)
'Enter your code here
End with
End if
Next
However, this will apply to every open "Form1". So if you plan to use multiple "Form1", you can set an identifier on Form_Load like Nefariis showed in his answer.
但是,这将适用于每个打开的“Form1”。因此,如果您打算使用多个“Form1”,您可以在 Form_Load 上设置一个标识符,就像 Nefariis 在他的回答中显示的那样。
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
F = Me
End Sub
And use that variable in the loop.
并在循环中使用该变量。

