Uso de diferentes clases para verificar los controles de formulario principal
Frecuentes
Visto 1,512 veces
0
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.
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.
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.
Gracias chicos
2 Respuestas
0
Suena como TabPage2
is an instance variable, yet you are trying to reference TabPage2
de un Shared
método donde TabPage2
no existe
Necesitas quitar el Shared
modificador en el getchecker
método.
Response re comments.
Intente algo como esto:
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")
.
respondido 27 nov., 13:05
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
Form1.f.Textbox1.Text = ""
or
Form1.f.CheckBox1.Checked = True
Disfrutar
For some additional reading:
Respondido el 24 de enero de 14 a las 00:01
Not a great solution. f is null in your example. What happens if you have two Form1s open? - LarsTech
Like I said in the last post - You really should give it a try first. I'm currently using this in two of my programs. - Nefariis
Not trolling, just saw you post the exact same answer, so I posted the exact same comment. :-) - LarsTech
Yeah sorry, i deleted that, and I also added some extra reading and some other options - Nefariis
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas vb.net visual-studio-2010 subclass public-method or haz tu propia pregunta.
Tabpage2 is a container that contains 6 checkboxes, its a tabpage from a TAB control object.... Also, without the shared, how would I call the property from another class ? ... Plus I would have to pass it a checkbox object, how do I reference a checkbox from another class in the first place? - Nefariis
You can set the access modifier of the checkboxes to
public
and then you can access them from your main window as well (through the instance variabletabpage2
). - styxxyThey have been set to public this entire time, and so is the tab container. I still cant seem to access them in anyway - Nefariis
You would need to pass a reference to the form to allow access. At the end of the day you need to pass some reference around to do what you want. A shared method has no idea of any instances of the class without being passed a reference. - Enigmatividad
Can you post some code? and I thought dim f as new QoE was passing a reference? - Nefariis