Reputation: 1
My code: (GeneralModule.vb)
Imports System.Windows.Forms
Module GeneralModule
Public Sub ClearTxt()
Dim t As Control
For Each t In FormReceipt.Controls()
If TypeOf t Is TextBox Then
t.Text = ""
End If
Next
End Sub
End Module
I'm getting error at FormReceipt.Controls() as BC30469 Reference to a non-shared member requires an object reference. Help plz...
Upvotes: 0
Views: 1307
Reputation: 74605
Drop a button on your form, double click it and paste this modified version of your code into the click handler:
For Each t as Control In Me.Controls
If TypeOf t Is TextBox Then
t.Text = ""
End If
Next
If, as per the commenter, you want the code to be outside your form, you'll have to give the controls collection you want to search, to the module:
Imports System.Windows.Forms
Module GeneralModule
Public Sub ClearTextboxesInCollection(x as ControlControlCollection)
For Each t as Control In x
If TypeOf t Is TextBox Then
t.Text = ""
End If
Next
End Sub
End Module
And call it like this from a button on your form (or some other triggering place where "Me" refers to the form that you want to clear):
GeneralModule.ClearTextboxesIn(Me.Controls)
It's worth noting that controls can be hierarchically nested, and a Panel will have a .Controls collection that could contain textboxes so naively enumerating them wont dig into any panels and find the controls
To this end you might want to consider something more like:
Imports System.Windows.Forms
Module GeneralModule
Public Sub ClearTextboxesInCollection(x as ControlControlCollection)
For Each t as Control In x
If TypeOf t Is TextBox Then
t.Text = ""
End If
If t.HasChildren Then
ClearTextboxesInCollection(t.Controls)
End If
Next
End Sub
End Module
Upvotes: 0
Reputation: 415620
FormReceipt
is the name of a class. You need a reference to an instance of that class.
VB.Net provides default instances of each form in variables with the same name as the class, but there are certain places where you can't access those instances that way. For example, if this code is in a different class library, that separate assembly doesn't know or care about the default form instances; it only sees the imported type names. Certain project settings can also turn these off.
At the point where you show the form, or are about to show the form, you'll be able to find a reference for the instance you need. When you you say something like FormReceiptVariable.Show()
or FormReceiptVariable.ShowDialog()
, you need to assign that variable somewhere you will also be able to access from the code in this question.
Upvotes: 1
Reputation: 8004
BC30469 Reference to a non-shared member requires an object reference
This error is because of this For Each t In FormReceipt.Controls()
that's not an instance of FormReceipt
; probably the default.
To fix you need an instance of FormReceipt
for example: Private fr As FormReceipt = New FormReceipt()
if you don't have one already otherwise just assign the variable.
Then use that variable/instance to get the controls...
For Each t In fr.Controls()
Upvotes: 0