How can I put the text which is in a multiline textbox into a combobox.
Each line in the textbox must be a new item in the combobox.
Printable View
How can I put the text which is in a multiline textbox into a combobox.
Each line in the textbox must be a new item in the combobox.
' Is this what you are after
' Need VB6 for this split
Code:Private Sub Command1_Click()
Dim x As Variant
x = Text1
x = Split(x, vbCrLf)
For i = LBound(x) To UBound(x)
Combo1.AddItem x(i)
Next i
End Sub
Private Sub Form_Load()
Dim i
For i = 1 To 5
Text1 = Text1 & i & vbCrLf
Next i
End Sub
Joe was too fast, as usual, but I'll post my code anyway.
Code:Private Sub Command1_Click()
Dim strArray() As String
Dim x As Integer
strArray = Split(Text1.Text, vbCrLf)
For x = 0 To UBound(strArray)
Combo1.AddItem strArray(x)
Next x
End Sub
Code:' this one for VB5 -
Private Sub Command1_Click()
Dim x As Variant
x = Text1
x = Split2(x, vbCrLf)
For i = LBound(x) To UBound(x)
Combo1.AddItem x(i)
Next i
End Sub
Private Sub Form_Load()
Dim i
For i = 1 To 5
Text1 = Text1 & i & vbCrLf
Next i
End Sub
'split function for VB5 and under vb6 has the function
'posted originally by Aaron Young
Public Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
Dim sParts() As String
Dim lParts As Long
Dim lPos As Long
lPos = InStr(sString, sSeparator)
While lPos
ReDim Preserve sParts(lParts)
sParts(lParts) = Left(sString, lPos - 1)
sString = Mid(sString, lPos + Len(sSeparator))
lPos = InStr(sString, sSeparator)
lParts = lParts + 1
Wend
If Len(sString) Then
ReDim Preserve sParts(lParts)
sParts(lParts) = sString
End If
Split2 = IIf(lParts, sParts, Array())
End Function