Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27350

[RESOLVED] Struggling with trying to access a control on a form from seperate classes...

$
0
0
I still cant seem to wrap my head around this concept.

I get the classes and forms are completely separate objects that can't see each other initially unless you pass on the data from the form to the class or vice versa. What I can't figure out is how to correctly pass those parameters without multiple errors occurring. And yes jmc, i did check out your multiple forms code in your signature : P .

I have 6 checkboxes inside a tab container located on "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:

Code:

dim f as new QoE
in each class, then calling the checkboxes like this:

Code:

If f.chkTcpRTT.checked Then...
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". it looks like its just creating a new instance of the form and not actually giving me any information about the form as it is currently. With that being said:
Code:

Dim f as QoE or Dim f = QoE
Doesnt work.

My second thought was to use the property method, so I use this on the QoE form:

Code:

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

and then I could call it using:
Code:

QoE.getchecker(ChkTcpRTT)
But this still leaves me with having to pass an object from the other class which I cant figure out. so I tried this:

Code:

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

And then I could call it using:

Code:

QoE.getChecker("ChkTcpRTT")
But then I get 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." on the "TabPage2.Controls"

The last thing I've tried was different variations of Event Raiser to see if I could access the control from within the form and send it back to the class. But I either get the error above or I am unable to get the check state without actually having to set a global variable (which I know cant be good practice)

Any help with an explanation will be more than greatly appreciated.

Thanks Guys

Viewing all articles
Browse latest Browse all 27350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>