cs_jujube
Messages postés19Date d'inscriptiondimanche 27 octobre 2002StatutMembreDernière intervention21 janvier 2013
-
17 mars 2009 à 21:46
cs_jujube
Messages postés19Date d'inscriptiondimanche 27 octobre 2002StatutMembreDernière intervention21 janvier 2013
-
17 mars 2009 à 22:33
Bonjour, j'ai trouvé ce code sur le forum qui permet de créer un textbox par programmation. Pourriez-vous m'indiquer de quelle façon je peux modifier ce code pour créer plusieurs textbox.
Je vous remercie à l'avance.
Option Explicit
Private WithEvents objText As TextBox
Private Sub Form_Load()
CreaText Me, "Text", 1000, 1800, 1500, 300, "Text Box"
End Sub
Private Sub objText_Click()
MsgBox "Test Textbox"
End Sub
Public Sub CreaText(Frm As Form, ByVal Name As String, ByVal Top As Long, ByVal Left As Long, ByVal Width As Long, ByVal Height As Long, _
ByVal Text As String)
Set objText = LoadControl(Frm, "VB.TextBox", Name)
If Not objText Is Nothing Then
objText.Top = Top
objText.Left = Left
objText.Width = Width
objText.Height = Height
objText.Text = Text
objText.ForeColor = vbBlack
objText.BackColor = &H8000000F
objText.BackColor = vbYellow
objText.Appearance = 0
objText.Visible = True
End If
End Sub
Private Function LoadControl(oForm As Object, ctrType As String, ctrName As String) As Object
Dim oCtl As Object
On Error Resume Next
If IsObject(oForm.Controls) Then
Set oCtl = oForm.Controls.Add(ctrType, ctrName)
If Not oCtl Is Nothing Then Set LoadControl = oCtl
End If
End Function
Renfield
Messages postés17287Date d'inscriptionmercredi 2 janvier 2002StatutModérateurDernière intervention27 septembre 202174 17 mars 2009 à 22:22
tu ne stockes qu'un seul objText...
et creaText ne manipule qu'une instance :
Set objText = LoadControl(Frm, "VB.TextBox", Name)
faire donc:
Public Function CreaText(Frm As Form, ByVal Name As String, ByVal Top As Long, ByVal Left As Long, ByVal Width As Long, ByVal Height As Long, _
ByVal Text As String) As TextBox
Dim oCtl As TextBox
Set oCtl = LoadControl(Frm, "VB.TextBox", Name)
If Not oCtl Is Nothing Then
oCtl.Top = Top
oCtl.Left = Left
oCtl.Width = Width
oCtl.Height = Height
oCtl.Text = Text
oCtl.ForeColor = vbBlack
oCtl.BackColor = &H8000000F
oCtl.BackColor = vbYellow
oCtl.Appearance = 0
oCtl.Visible = True
End If
Set CreaText = oCtl
End function
et multiplier les appels :
Private WithEvents objText_1 As TextBox
Private WithEvents objText_2 As TextBox
Private WithEvents objText_3 As TextBox
cs_jujube
Messages postés19Date d'inscriptiondimanche 27 octobre 2002StatutMembreDernière intervention21 janvier 2013 17 mars 2009 à 22:14
Bonsoir, même si je l'appelle n fois il ne se crée qu'une seule fois. Il se charge une fois au démarrage et même si je l'appelle par la Command1_Click, ça ne change rien, j'en ai toujours une seule copie.
Private Sub Command1_Click()
CreaText Me, "Text", 1000, 1800, 1500, 300, "Text box"
End Sub
Private Sub Form_Load()
CreaText Me, "Text", 1800, 1800, 1500, 300, "Text Box"
End Sub