Reputation: 37
Im having a textfield set up with the text "Hello" and a button When i change the text and press i'd like to have a msgbox replying the changed text
Public Class TestText
Dim Text As String = Textbox.text
Private Sub BtnchangeTXT_Click(sender As Object, e As EventArgs) Handles BtnchangeTXT.Click
Text = Textbox.Text
Msgbox(Text)
Msgbox(TextField.Fieldtext)
End Sub
End Class
Public Class TextField
Public Shared FieldText As String = TestText.Textbox.Text
End Class
All works well untill i request for the "TextField.FieldText" msgBox this will always return the defaulttext ("hello")
where do i go wrong? Ive tried to set a different text "On Load" but still the default "Hello" is being returned.
Upvotes: 0
Views: 492
Reputation: 54457
There are two problems with your code. Firstly, because this code:
Public Class TextField
Public Shared FieldText As String = TestText.Textbox.Text
End Class
uses the name of the TestText
class to refer to an instance of that class, it is referring to the default instance. If you don't know what default instances are, I suggest that you do some reading on the subject. You can find my own writeup here.
That means that that code will always get the Text
of the TextBox
on the default instance of that type. If you display the default instance then that's fine but if you explicitly create an instance and display that, then modify the contents of the TextBox
on that instance, that will have exactly zero effect on the default instance.
The second issue is the fact that you are using a field. You seem to be under the impression that that FieldText
field should change dynamically as you change the contents of the TextBox
. Why would you think that? The code you have DOES NOT get the current contents of the TextBox
every time you get the field value. What is does is get the current contents of the TextBox
and assign it to the field on the first occasion you get that field and then NEVER changes the field value again. If what you actually want is the current contents of the TextBox
then you would need a property rather than a field:
Public Class TextField
Public Shared ReadOnly Property FieldText As String
Get
Return TestText.Textbox.Text
End Get
End Property
End Class
The whole point of properties is that they act like fields on the outside, so your code to get that property value won't change, but they act like methods on the inside, so the code to get the current contents of the TextBox
will be executed every time you get the property value.
Upvotes: 0
Reputation: 39142
In this line:
Public Shared FieldText As String = TestText.Textbox.Text
The value of "FieldText" only gets set ONCE, when that variable is created (and that occurs even before the form with the textbox is shown). It does not get updated whenever the TextBox changes.
If you want that variable to always have the current value of that TextBox, then use the TextChanged() event of that TextBox to update the variable.
Private Sub Textbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Textbox.TextChanged
TextField.Fieldtext = Textbox.Text
End Sub
Upvotes: 2