Handling events of programmatically added controls

G

Guest

When my program is launched, there is a textbox on the form.

When the user enters a character into the textbox (TextChanged), a second,
declared textbox is added using this block of code.

Dim textbox2 As New TextBox
Controls.Add(textbox2)

When the second textbox is filled with character as well, a third textbox is
added using smilar method as adding the second one. However, when I add the
event handler using

Private Sub textbox3_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles textbox3.TextChanged

, VB say that 'Handle clause requires a WithEvent varible'. I checked the
Online Help about WithEvent
(http://msdn.microsoft.com/library/d...n-us/vbcn7/html/vaconWritingEventHandlers.asp) and I have no clue what it is talking about.

Can somebody help me with this problem? Thanks.
 
G

Guest

Xero,

Take a look at the code that the Form Designer generates. Notice that when
you draw a textbox on a form, the designer writes code like this:

Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

You need to do the same thing for your code-generated textbox.

Kerry Moorman
 
A

Armin Zingler

Xero said:
When my program is launched, there is a textbox on the form.

When the user enters a character into the textbox (TextChanged), a
second, declared textbox is added using this block of code.

Dim textbox2 As New TextBox
Controls.Add(textbox2)

When the second textbox is filled with character as well, a third
textbox is added using smilar method as adding the second one.
However, when I add the event handler using

Private Sub textbox3_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles textbox3.TextChanged

, VB say that 'Handle clause requires a WithEvent varible'. I checked
the Online Help about WithEvent
(http://msdn.microsoft.com/library/d...n-us/vbcn7/html/vaconWritingEventHandlers.asp)
and I have no clue what it is talking about.

Can somebody help me with this problem? Thanks.


How and where did you declare Textbox3? You must declare it as a field of
the Form:

friend withevents Textbox3 as textbox

("field":
http://msdn.microsoft.com/library/en-us/vbcn7/html/vbconProperties.asp)

Now you can choose textbox3 and it's events in the top combos of the code
editor. Selecting the textchanged event will create the event handling
procedure including the handles statement.

This is only useful if you've got a limited number of controls.


Armin
 
O

Oenone

Xero said:
When the user enters a character into the textbox (TextChanged), a
second,
declared textbox is added using this block of code.

Dim textbox2 As New TextBox
Controls.Add(textbox2)

I'm not entirely clear, but I'm assuming that this control (textbox2) is
created dynamically at run-time and that the Dim statement above is in your
procedure code, and not at the form's module level?

If you're dynamically adding controls, you need to add event handlers for
the controls' events in order for them to trigger your code when they fire.
Take a look at the AddHandler statement help in MSDN. You should be able to
do this:

\\\
Private Sub AddTextBox()
Dim textbox2 As New TextBox
AddHandler textbox2.TextChanged, AddressOf MyTextChangedHandler
Controls.Add(textbox2)
End Sub

Private Sub MyTextChangedHandler(ByVal sender As System.Object, ByVal e
As System.EventArgs)
'Handle the event here
End Sub
///

You can add the handler procedure to as many controls as you like. To
determine which one was actually used, check the contents of the sender
parameter. It will be a reference to the control that raised the event.
 
J

Josip Habjan

Hi,

As Oenone sayed, for controls you create dynamically at runtime you can only
set event handler thru AddHandler method.

Maby this is what you are looking for: (recursive TextChangedHandler
method):

Public Class Form1
Inherits System.Windows.Forms.Form

Public Sub New()
MyBase.New()

InitializeComponent()
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

Private components As System.ComponentModel.IContainer
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(8, 8)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 271)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

Private _isloaded As Boolean = False

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_isloaded = True
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged
TextChangedHandler(sender, e)
End Sub

Private Sub TextChangedHandler(ByVal sender As Object, ByVal e As
EventArgs)
If _isloaded Then
Dim txtlast As TextBox = CType(sender, TextBox)
If Not TextBoxExist(txtlast.Name + "_child") Then
Dim txtnew As New TextBox
txtnew.Name = txtlast.Name + "_child"
txtnew.Left = txtlast.Left
txtnew.Top = txtlast.Top + txtlast.Height + 4
txtnew.Visible = True
Me.Controls.Add(txtnew)
AddHandler txtnew.TextChanged, AddressOf TextChangedHandler
End If
End If
End Sub

Private Function TextBoxExist(ByVal name As String) As Boolean
For Each ctrl As Control In Me.Controls
If ctrl.Name.ToLower = name.ToLower Then
Return True
End If
Next

Return False
End Function

End Class

Regards,
Josip Habjan
URL: www.habjansoftware.com
 
G

Guest

Hi and thanks for your reply.
When I type in the code you mentioned, Visual Studio underlined 'Friend' and
'WithEvents', saying that they are not valid in local varible declaration.

I added the code inside an event handler. Where should I add the code instead?

Thanks.
 
G

Guest

Hello. Thanks for replying.

Yes, I am trying to do exactly what you said in your post.
But when I added the code you mentioned, 'textbox3' is still not added after
the TextChanged.

I am sure that the name after AddressOf and the event handler is correct. Is
there anything I am missing?

Thanks again.
 
G

Guest

Thanks for replying.
I understand the snippet of code in the page ... but I am encountering the
same problem as I did in Oenone's post.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top