Problem changing text of custom control based on text of another custom control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pdickson
    New Member
    • Jan 2010
    • 2

    #1

    Problem changing text of custom control based on text of another custom control

    Hi - I am using custom textboxes and I need to be able to set the Text on txb1 based on the Text of txb2. I have simplied my code to the following:
    Code:
        Public Class customtextbox
            Inherits TextBox
            Public Event ControlReset()
            Public Sub New(ByVal strP As String)
                MyBase.New()
                AddHandler customtextbox.ControlReset, AddressOf Me.test
            End Sub
            Public Sub test()
                Me.Text = "chips" + Me.Name
            End Sub
            Public Sub ProcessAlert()
                RaiseEvent ControlReset()
            End Sub
            Private Sub customtextbox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus
                Dim cT As Control
                Dim cS As customtextbox
                If Me.Name = "txb1" Then
                    If Me.Text = "fried" Then
                        For Each cT In frmTest.Controls
                            If cT.Name = "txb2" Then
                                cS = cT
                                Call cS.ProcessAlert()
                            End If
                        Next
                    End If
                End If
            End Sub
        End Class
    and the form code is:
    Code:
    Public Class frmTest
        Public Sub New()
            InitializeComponent()
            Dim txb1 As customtextbox
            Dim txb2 As customtextbox
            txb1 = New customtextbox("params for txb1")
            txb1.Name = "txb1"
            txb1.Location = New Point(10, 10)
            txb1.Size = New Point(200, 20)
            Me.Controls.Add(txb1)
            txb2 = New customtextbox("params for txb2")
            txb2.Name = "txb2"
            txb2.Location = New Point(10, 30)
            txb2.Size = New Point(200, 20)
            Me.Controls.Add(txb2)
        End Sub
    End Class
    So, when txb1 looses focus, if its Text is "fried", then it sets off txb2's ProcessAlert function. I had tried to change the value of txb2 Text in ProcessAlert function, but didnt work, so I tried to get it to raise an event instead, which then tries to set the Text property. However I still cant set txb2's Text property. The Text property seems to be set for the duration of the "life" of the Event handler. When I step through the forms controls within the Event handler, the changed Text value is there, but not outside of the Event handler.

    Any suggestions???
    Many thanks
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I'm a C# guy, not a VB guy, so someone correct me if I am wrong here...
    Code:
                Dim cS As customtextbox
                If Me.Name = "txb1" Then
                    If Me.Text = "fried" Then
                        For Each cT In frmTest.Controls
                            If cT.Name = "txb2" Then
                                cS = cT
                                Call cS.ProcessAlert()
    The event is being fired in the newly created cS textbox not in located cT textbox. I suspect there is no reason for the cS control at all and could be simplified as
    Code:
    If Me.Name = "txb1" Then
                    If Me.Text = "fried" Then
                        For Each cT In frmTest.Controls
                            If cT.Name = "txb2" Then
                                Call cT.ProcessAlert()
    Of course since this is a custom control we have no idea what .ProcessAlert() does, so if the problem resides there....

    Comment

    • pdickson
      New Member
      • Jan 2010
      • 2

      #3
      Thanks for the reply. I had to use the cS as the ProcessAlert() is a sub within the custom control, and thus not available from a standard control type. The ProcessAlert just raised an event on another custom control that changes its text.

      I have changed my coding and taken most of the subs out of the custom control and put them in the form that creates them (there was a lot more code, this was just a simple example I created to try and solve the problem), and also passed the form to the custom controls, so the custom controls have direct access to the subs. Now that I am calling the subs from the form rather than the custom controls, it seems to work OK.

      Cheers

      Comment

      Working...