By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,218 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,218 IT Pros & Developers. It's quick & easy.

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

P: 2
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:
Expand|Select|Wrap|Line Numbers
  1.     Public Class customtextbox
  2.         Inherits TextBox
  3.         Public Event ControlReset()
  4.         Public Sub New(ByVal strP As String)
  5.             MyBase.New()
  6.             AddHandler customtextbox.ControlReset, AddressOf Me.test
  7.         End Sub
  8.         Public Sub test()
  9.             Me.Text = "chips" + Me.Name
  10.         End Sub
  11.         Public Sub ProcessAlert()
  12.             RaiseEvent ControlReset()
  13.         End Sub
  14.         Private Sub customtextbox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus
  15.             Dim cT As Control
  16.             Dim cS As customtextbox
  17.             If Me.Name = "txb1" Then
  18.                 If Me.Text = "fried" Then
  19.                     For Each cT In frmTest.Controls
  20.                         If cT.Name = "txb2" Then
  21.                             cS = cT
  22.                             Call cS.ProcessAlert()
  23.                         End If
  24.                     Next
  25.                 End If
  26.             End If
  27.         End Sub
  28.     End Class
  29.  
and the form code is:
Expand|Select|Wrap|Line Numbers
  1. Public Class frmTest
  2.     Public Sub New()
  3.         InitializeComponent()
  4.         Dim txb1 As customtextbox
  5.         Dim txb2 As customtextbox
  6.         txb1 = New customtextbox("params for txb1")
  7.         txb1.Name = "txb1"
  8.         txb1.Location = New Point(10, 10)
  9.         txb1.Size = New Point(200, 20)
  10.         Me.Controls.Add(txb1)
  11.         txb2 = New customtextbox("params for txb2")
  12.         txb2.Name = "txb2"
  13.         txb2.Location = New Point(10, 30)
  14.         txb2.Size = New Point(200, 20)
  15.         Me.Controls.Add(txb2)
  16.     End Sub
  17. End Class
  18.  
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
Jan 11 '10 #1
Share this Question
Share on Google+
2 Replies


tlhintoq
Expert 2.5K+
P: 3,525
I'm a C# guy, not a VB guy, so someone correct me if I am wrong here...
Expand|Select|Wrap|Line Numbers
  1.             Dim cS As customtextbox
  2.             If Me.Name = "txb1" Then
  3.                 If Me.Text = "fried" Then
  4.                     For Each cT In frmTest.Controls
  5.                         If cT.Name = "txb2" Then
  6.                             cS = cT
  7.                             Call cS.ProcessAlert()
  8.  
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
Expand|Select|Wrap|Line Numbers
  1. If Me.Name = "txb1" Then
  2.                 If Me.Text = "fried" Then
  3.                     For Each cT In frmTest.Controls
  4.                         If cT.Name = "txb2" Then
  5.                             Call cT.ProcessAlert()
Of course since this is a custom control we have no idea what .ProcessAlert() does, so if the problem resides there....
Jan 11 '10 #2

P: 2
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
Jan 13 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.