TextChanged on dynamicly added TextBox

mmiller039

Member
Joined
Dec 16, 2008
Messages
8
Programming Experience
Beginner
I have a class that contains a couple of TextBoxes and a couple of Labels, I wangt to add these dynamically to a window and then add them to the new instance of the class. I then want to watch for a TextChange in either of the TextBoxes. Bellow is the class I am using, but get error "Handles clause requires a WithEvents variable defined in the containing type or one of its base types" and unfortunately this is all quite new to me so am not sure how to go about doing this.

VB.NET:
Public Class Spread
    ' TextBox and Label declaration
    Public ratioA, ratioB As TextBox()
    Public sizeL1, sizeL2 As Label()


    Private Sub bid_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bid.TextChanged

    End Sub
End Class
 
Handles clause requires a WithEvents variable, without it you need to use AddHandler statement.
 
I have put in an addHandler as bellow, but still have the same error for the new sub I created and the addHandler has a syntax error.

VB.NET:
    AddHandler bid.textchanged, address of something

    Public Sub something(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bid.TextChanged

    End Sub
 
I have found a workaround, by simply declaring each of the textboxes using a withevents and seems to do the job ...

VB.NET:
Public Class Spread
    ' TextBox and Label declaration
    Public WithEvents ratioA, ratioB As TextBox()
    Public sizeL1, sizeL2 As Label()


    Private Sub bid_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bid.TextChanged

    End Sub
End Class
 
AddHandler Statement is something you do in methods when they run to dynamically add event handlers for dynamically created controls, it is not a statement to define the class. Have a look in help, it also has code examples: AddHandler Statement
If you for example write some code that ask user how many textboxes should be created you can't know the answer, so it is not possible to define WithEvents variables for them, but in method you can loop to create any number of control instances and add them to a container and use AddHandler statement for each of them.
 
Back
Top