Home > Software engineering >  VBA dynamically generated in the control
VBA dynamically generated in the control

Time:11-17

The last post for help, to automatically create the ComboBox control
 
The Set McOlCombos1=New Collection
For I=1 To 5
Dim ctlCombo1 As ComboBox
The Set ctlCombo1=Frame1. Controls. Add (" Forms.com bobox. 1 ", "modular subbce" & amp; I, True)

With ctlCombo1
Left=200
The Top 30 +=(I - 1) * 50 + 6
Width=120
The Height=20
The AddItem "M 5/2 Monostable"
The AddItem "B 5/2 Bistable"
End With

Dim objMyCombo1 As MyCombo
The Set objMyCombo1=New MyCombo

The Set objMyCombo1. McTlCombo=ctlCombo1
ObjMyCombo1. MlngIndex=I

McOlCombos1. Add objMyCombo1
Next I

Besides his create several TextBox control
 
Dim working As Control
Dim As Integer I
With UserForm1
For I=1 To 5
The Set working=Frame1. Controls. Add (" Forms. The textbox. 1 ", "Txt" & amp; I, True)
With working
Left=100
The Top 30 +=(I - 1) * 50 + 6
Width=80
The Height=20
End With
Next I
End With

Purpose is to users to choose the form of dynamically generated ComboBox Additem value, select the respectively the value of the code is part of a display in the TextBox, keep the explanation in the ComboBox, if choose "M 5/2 Monostable" is displayed as "M" in the TextBox, ComboBox is shown as "5/2 Monostable",
Due to the limited level, the existing the following questions:
1. If the form control is easy to distinguish between the two, in the form of controls change () event can be solved, but if for dynamically added controls cannot operate,
2. Tried to processing in the class module, the code is as follows:
 
Option Explicit

Public WithEvents McTlCombo As ComboBox

Public mlngIndex As Long
Public McOlCombosVal1 As String
Public McOlCombosVal1x As String

Private Sub McTlCombo_Change ()
MsgBox McOlCombosVal1
McOlCombosVal1x=McOlCombos (1). McTlCombo. Value
McOlCombosVal1=Mid (McOlCombosVal1x, 1, 1)
Txt1 UserForm1. Controls (" "). The Text=McOlCombosVal1
McOlCombos (1). McTlCombo. Value=https://bbs.csdn.net/topics/Mid (McOlCombosVal1x, 4)
End Sub

Runtime errors "object variables or with variable block is not set"
Why don't know is wrong? Please guide, level co., LTD. Please kindly understanding, thank you!

CodePudding user response:

https://download.csdn.net/download/bakw/9619630 I had sent an example, consider.

CodePudding user response:

Try this:
 
Option Explicit

Public WithEvents McTlCombo As ComboBox

Public mlngIndex As Long
Public mstrCombosVal1 As String
Public mstrCombosVal1x As String

Private Sub McTlCombo_Change ()
MsgBox mstrCombosVal1
MstrCombosVal1x=McTlCombo. Text
MstrCombosVal1=Mid (mstrCombosVal1x, 1, 1)
Txt1 UserForm1. Controls (" "). The Text=mstrCombosVal1
McTlCombo. Text=Mid (mstrCombosVal1x, 4)
End Sub

CodePudding user response:

If only deals with the first combobox control, can be like this:
 
Option Explicit

Public WithEvents McTlCombo As ComboBox

Public mlngIndex As Long
Public mstrCombosVal1 As String
Public mstrCombosVal1x As String

Private Sub McTlCombo_Change ()
If mlngIndex & lt;> 1 Then
The Exit Sub
End the If
MsgBox mstrCombosVal1
MstrCombosVal1x=McTlCombo. Text
MstrCombosVal1=Mid (mstrCombosVal1x, 1, 1)
Txt1 UserForm1. Controls (" "). The Text=mstrCombosVal1
McTlCombo. Text=Mid (mstrCombosVal1x, 4)
End Sub
  •  Tags:  
  • VBA
  • Related