I'm struggling to wrap my head around a concept and I've been at this for 4-5 hours now. So I'm hoping that someone can explain this to me.
我正在努力围绕一个概念,我已经在这里待了4-5个小时了。所以我希望有人可以向我解释这一点。
I have multiple checkboxes inside a tabpage labeled "tabpage2" on the main form "QoE" and I have multiple classes that need to know the check states of those checkboxes.
我在主窗体“QoE”上标有“tabpage2”的标签页中有多个复选框,我有多个类需要知道这些复选框的检查状态。
I first went about this using:
我首先使用以下方法:
dim f as new QoE
in each class, then calling the checkboxes like this:
在每个类中,然后调用这样的复选框:
f.chkTcpRTT.checked
but the result is always coming back "False" whether the box is checked or not, which I am assuming has to do with the fact that I am using the word "new".
但结果总是回到“假”,无论盒子是否被检查,我假设这与我使用“新”这个词的事实有关。
My second thought was to use the property method
我的第二个想法是使用属性方法
Public Shared Property getchecker(ByVal chk As CheckBox) As Boolean
Get
Return chk.Checked
End Get
Set(ByVal value As Boolean)
chk.Checked = value
End Set
End Property
But this still leaves me with having to pass an object from the other class which I cant figure out. so I tried this:
但这仍然让我不得不从另一个类中传递一个我无法弄清楚的对象。所以我试过这个:
Public Shared Property getchecker(ByVal txt As string) As Boolean
Get
For Each ctrl as Control in TabPage2.Controls
If ctrl.name = txt Then
Return ctrl.Checked
End If
Next
End Get
Set(ByVal value As Boolean)
txt.Checked = value
End Set
End Property
But now I am getting the error: "Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."
但现在我收到错误:“如果没有类的显式实例,则无法从共享方法或共享成员初始化程序中引用类的实例成员。”
The methods that I am using to call these features are all "public shared" so why am I getting this error and how can I get these checkstates? If its possible I would like to use some variation of the first code, so why is everything coming back false? is it because of the "new" flag? I would love to know a little background if its possible.
我用来调用这些功能的方法都是“公共共享”,为什么我会收到此错误,如何获取这些检查状态?如果它可能我想使用第一个代码的一些变体,那么为什么一切都回来了?是因为“新”旗帜?如果有可能,我很想知道一点背景知识。
Thanks Guys
2 个解决方案
#1
0
It sounds like TabPage2
is an instance variable, yet you are trying to reference TabPage2
from a Shared
method where TabPage2
doesn't exist.
听起来像TabPage2是一个实例变量,但是你试图从一个不存在TabPage2的Shared方法引用TabPage2。
You need to remove the Shared
modifier on the getchecker
method.
您需要删除getchecker方法上的Shared修饰符。
Response re comments.
回应重新评论。
Try something like this:
尝试这样的事情:
Public Property getchecker(ByVal txt As string) As Boolean
For Each ctrl as Control in Me.TabPage2.Controls
If ctrl.name = txt Then
Return ctrl.Checked
End If
Next
Return False
End Property
To use this you would pass your reference of f
to the other class and it would call it like so f.getchecker("foo")
.
要使用它,你会将f的引用传递给另一个类,它会像f.getchecker(“foo”)那样调用它。
#2
0
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:
对于一些额外的阅读:
#1
0
It sounds like TabPage2
is an instance variable, yet you are trying to reference TabPage2
from a Shared
method where TabPage2
doesn't exist.
听起来像TabPage2是一个实例变量,但是你试图从一个不存在TabPage2的Shared方法引用TabPage2。
You need to remove the Shared
modifier on the getchecker
method.
您需要删除getchecker方法上的Shared修饰符。
Response re comments.
回应重新评论。
Try something like this:
尝试这样的事情:
Public Property getchecker(ByVal txt As string) As Boolean
For Each ctrl as Control in Me.TabPage2.Controls
If ctrl.name = txt Then
Return ctrl.Checked
End If
Next
Return False
End Property
To use this you would pass your reference of f
to the other class and it would call it like so f.getchecker("foo")
.
要使用它,你会将f的引用传递给另一个类,它会像f.getchecker(“foo”)那样调用它。
#2
0
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:
对于一些额外的阅读: