Results 1 to 9 of 9

Thread: open file in combo box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Question 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.
    visit my site
    www.tecknews.co.uk

  2. #2
    Member
    Join Date
    Jun 2009
    Posts
    62

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Re: open file in combo box

    Quote Originally Posted by Guy M View Post
    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.
    visit my site
    www.tecknews.co.uk

  4. #4
    Member
    Join Date
    Jun 2009
    Posts
    62

    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.

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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


  6. #6
    Member
    Join Date
    Jun 2009
    Posts
    62

    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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    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
    visit my site
    www.tecknews.co.uk

  8. #8
    Junior Member
    Join Date
    Aug 2008
    Posts
    18

    Re: open file in combo box

    Quote Originally Posted by adam786 View Post
    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.

  9. #9
    Member
    Join Date
    Jun 2009
    Posts
    62

    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
  •  



Click Here to Expand Forum to Full Width