Nefariis Nefariis - 11 days ago 5
Vb.net Question

Accessing controls from other classes .net

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.

I have a main form entitled "QoE" and I have two classes entitled "Utils" and and "Tests"

Tests calls a public shared sub from Utils using Utils.ProgressBar()

Utils.ProgressBar() updates a progress bar control from the main form entitled 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:

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.

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

Answer

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

Enjoy

For some additional reading:

http://www.vbforums.com/showthread.php?744677-RESOLVED-Struggling-with-trying-to-access-a-control-on-a-form-from-seperate-classes&p=4563273&highlight=#post4563273