I have a file I would like to open in a combo box, is there any way I can do this also if I click a certain item in the list I need it to also appear in a label. Any ideas? I have searched the forums but cannot find anything but will try again.
Printable View
I have a file I would like to open in a combo box, is there any way I can do this also if I click a certain item in the list I need it to also appear in a label. Any ideas? I have searched the forums but cannot find anything but will try again.
Hi adam786,
I do something similar to what you want I believe. I have a txt file that contains a list, it is then displayed in the Combobox. If thats what you want I can probably help you.
Let me know.
Guy M
The first part of the code:
You can not change a Lable caption. You can display the Combo data into a Textbox if that is of any use?Code:Private Sub Form_Load()
Call LoadCM_List(Combo1)
End Sub
Private Sub LoadCM_List(TheCombo As ComboBox)
Dim File As Integer
Dim List() As String
Dim iItem As Integer
File = FreeFile
Open "YOUR FILE" For Input As File
List = Split(Input$(LOF(File), File), vbCrLf)
Close File
With TheCombo
.Clear
For iItem = 0 To UBound(List)
If Len(List(iItem)) > 0 Then
.AddItem List(iItem)
End If
Next
If .ListCount > 0 Then
.ListIndex = 0
End If
End With
End Sub
You can ofcourse make the Textbox appear like a Lable, so there is no difference visually.
To do so use:
Hope that helps. If not let me know & I will attempt to help you more.Code:Private Sub Combo1_Click()
Dim list As String
list = Combo1
Text1.Text = list
End Sub
Guy M
Guy M, I'm not sure what you mean by "you can't change the label caption"? :confused:
You can just put: Label1.Caption = TheCombo.Text
:duck:
Uhm...
Thats my bad, sorry. DigiRev is right, you can change the Lable caption fine. I was reading something that wasn't even VB6 related & thats why I thought it wasn't possible
Anyway, you can do as you wish & I apologies for the mistake. The code I would use is:
Dont know if there is a better way, but the above code seems to work correctly.Code:Private Sub Form_Load()
Call LoadCM_List(Combo1)
End Sub
Private Sub LoadCM_List(TheCombo As ComboBox)
Dim File As Integer
Dim List() As String
Dim iItem As Integer
File = FreeFile
Open "YOUR FILE" For Input As File
List = Split(Input$(LOF(File), File), vbCrLf)
Close File
With TheCombo
.Clear
For iItem = 0 To UBound(List)
If Len(List(iItem)) > 0 Then
.AddItem List(iItem)
End If
Next
If .ListCount > 0 Then
.ListIndex = 0
End If
End With
End Sub
Private Sub Combo1_Click()
Label1.Caption = Combo1.Text
End Sub
Guy M
ok thnaks for the replies, is it possible to do it backwards such as if a label = say 1 it highlights 1 in the combo box or not? but the item should be in the list or display an error message
Hi,
I'm not sure what you want or how your program is going to work, but I assume you will have several Labels that are also in the Combobox.
To be able to click a lable then highlighting it in the Combobox use the code below:
That will highlight the item in the Combo box if it the same as the Label, otherwise it will popup a msbox saying "Not Found!"Code:Dim CBfind As String
Dim intX As Integer
Dim CBfound As Boolean
CBfind = Label1.Caption
If CBfind = "" Then Exit Sub
CBfound = False
For intX = 0 To Combo1.ListCount - 1
If UCase$(Combo1.list(intX)) = UCase$(CBfind) Then
Combo1.ListIndex = intX
CBfound = True
Exit For
End If
Next
If Not CBfound Then
MsgBox "Not Found!"
End If
Hope that helps.
Guy M