Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Pro VB 6
Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 6th, 2006, 04:03 AM
Registered User
Join Date: Feb 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Event creation for dynamically created controls


I am creating a few text boxes dynamically on an userform which was created at design time. I have this code in Module1. This works with the following line of code

Now I want to add the change or any other event handler to this text box form Module1.

Any work around?


Old February 6th, 2006, 04:09 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts

are you using vb.net or vb6?

Old February 7th, 2006, 07:55 AM
Authorized User
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts

assuming vb6, you'll need to create a wrapper class for the TextBox object. You'll need to initialise that class with the instance of your dynamically-created textbox, and then consume within it the TextBox control events

something like (for a wrapper class called "MyTextBoxWrapper"

Option Explicit

Private Withevents mBaseControl as Vb.TextBox

Public Sub init(Byval TextBoxControl As Vb.TextBox)
  set mBaseControl= TextBoxControl
End Sub

Private sub mBaseControl_Change()
  Debug.Print mBaseControl.Name & " raised the Change event"
End Sub

Private sub mBaseControl_KeyPress(keyascii As Integer)
  Debug.Print mBaseControl.Name & " raised the KeyPress event [Key '" & Chr$(KeyAscii) & "' pressed]"
End Sub

Private sub mBaseControl_Validate(cancel As Boolean)
  Debug.Print mBaseControl.Name & " raised the Validate event"
End Sub
and for your Forms object (e.g. "Forms1")

Option explicit

Private mTextBoxWrapper As MyTextBoxWrapper

Private Sub Form_Load()
  dim tb As VB.TextBox
  Set tb = Me.Controls.Add("Vb.TextBox", "txtDynamicTextbox1")

  tb.Visible = True

  set mTextBoxWrapper = New MyTextBoxWrapper
  call mTextBoxWrapper.init(tb)
End Sub

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me about dynamically created controls vingo_mail ASP.NET 2.0 Basics 1 March 24th, 2008 12:54 PM
Problems with Dynamically created controls Preeti_Singh .NET Framework 1.x 1 March 7th, 2007 01:30 PM
Correctly disposing dynamically created controls simarik C# 2 December 20th, 2005 08:49 PM
Dynamically created controls katsarosj ASP.NET 1.0 and 1.1 Basics 4 February 4th, 2004 09:13 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.