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

Handling events of programmatically added controls

P: n/a
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/de...ntHandlers.asp) and I have no clue what it is talking about.

Can somebody help me with this problem? Thanks.

--
Xero

http://www.chezjeff.net
My personal web portal
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
"Xero" wrote:
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/de...ntHandlers.asp) and I have no clue what it is talking about.

Can somebody help me with this problem? Thanks.

--
Xero

http://www.chezjeff.net
My personal web portal

Nov 21 '05 #2

P: n/a
"Xero" <jeff_at_chezjeff_dot_net> schrieb
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/de...ntHandlers.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...Properties.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

Nov 21 '05 #3

P: n/a
Xero wrote:
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.

--

(O)enone
Nov 21 '05 #4

P: n/a
Xero,

Took a time tho find it.

In this is message is a sample for a winform and for a webform that I made
some time ago.

http://groups-beta.google.com/group/...066fe2c0?hl=en

I hope this helps,

Cor
Nov 21 '05 #5

P: n/a
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
"Xero" <jeff_at_chezjeff_dot_net> wrote in message
news:03**********************************@microsof t.com...
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/de...ntHandlers.asp)
and I have no clue what it is talking about.

Can somebody help me with this problem? Thanks.

--
Xero

http://www.chezjeff.net
My personal web portal

Nov 21 '05 #6

P: n/a
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.

--
Xero

http://www.chezjeff.net
My personal web portal
"Kerry Moorman" wrote:
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
"Xero" wrote:
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/de...ntHandlers.asp) and I have no clue what it is talking about.

Can somebody help me with this problem? Thanks.

--
Xero

http://www.chezjeff.net
My personal web portal

Nov 21 '05 #7

P: n/a
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.

--
Xero

http://www.chezjeff.net
My personal web portal
"Cor Ligthert" wrote:
Xero,

Took a time tho find it.

In this is message is a sample for a winform and for a webform that I made
some time ago.

http://groups-beta.google.com/group/...066fe2c0?hl=en

I hope this helps,

Cor

Nov 21 '05 #8

P: n/a
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.

--
Xero

http://www.chezjeff.net
My personal web portal
"Oenone" wrote:
Xero wrote:
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.

--

(O)enone

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.