|
-
Jul 10th, 2009, 03:13 PM
#1
Thread Starter
Addicted Member
open file in combo box
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.
-
Jul 10th, 2009, 03:50 PM
#2
Member
Re: open file in combo box
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
-
Jul 10th, 2009, 03:55 PM
#3
Thread Starter
Addicted Member
Re: open file in combo box
 Originally Posted by Guy M
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
Yes that is part of what i want.
-
Jul 10th, 2009, 04:12 PM
#4
Member
Re: open file in combo box
The first part of the code:
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 not change a Lable caption. You can display the Combo data into a Textbox if that is of any use?
You can ofcourse make the Textbox appear like a Lable, so there is no difference visually.
To do so use:
Code:
Private Sub Combo1_Click()
Dim list As String
list = Combo1
Text1.Text = list
End Sub
Hope that helps. If not let me know & I will attempt to help you more.
Guy M
Last edited by Guy M; Jul 10th, 2009 at 04:20 PM.
-
Jul 10th, 2009, 04:37 PM
#5
Re: open file in combo box
Guy M, I'm not sure what you mean by "you can't change the label caption"? 
You can just put: Label1.Caption = TheCombo.Text
-
Jul 10th, 2009, 05:55 PM
#6
Member
Re: open file in combo box
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:
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
Dont know if there is a better way, but the above code seems to work correctly.
Guy M
-
Jul 11th, 2009, 02:26 PM
#7
Thread Starter
Addicted Member
Re: open file in combo box
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
-
Jul 12th, 2009, 08:54 AM
#8
Junior Member
Re: open file in combo box
 Originally Posted by adam786
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
i believe it would be possible but wouldn't know how sorry.
-
Jul 12th, 2009, 09:17 AM
#9
Member
Re: open file in combo box
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:
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
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!"
Hope that helps.
Guy M
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|