Compare text in a combobox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Niels

    #1

    Compare text in a combobox

    Hello,
    I'm trying to get te index of a specific text in a ComboBox.
    The ComboBox is filled by searching for filenames in a directory.
    The text of the last item selected in the ComboBox is saved in a
    textfile in the OnExit Event of the Mainform.
    While loading the Mainform I want the text that was saved in the file
    to be compared with the items in the ComboBox ad get the index of the
    found item, so that I can set it as selected.

    I thought i could do it something like this, but the program hangs
    before I can see anything.
    ---------
    Code
    ---------
    Dim i, As Integer
    Dim line As String
    For i = 0 To ComboBox1.Items .Count
    If ComboBox1.Items (i) = line Then
    IndexFound = i
    Exit For
    End If
    Next
    ---------
    Code
    ---------

    Can anyone help me please,

    Thanx
  • J French

    #2
    Re: Compare text in a combobox

    I'll bet the program does not hang
    - it should come up with a ruddy great Error Message

    For i = 0 To ComboBox1.Items .Count - 1

    Note the minus one
    - Zero Based arrays are a PITA


    On 10 May 2004 03:50:30 -0700, nielsh@hollandr idderkerk.nl (Niels)
    wrote:
    [color=blue]
    >Hello,
    >I'm trying to get te index of a specific text in a ComboBox.
    >The ComboBox is filled by searching for filenames in a directory.
    >The text of the last item selected in the ComboBox is saved in a
    >textfile in the OnExit Event of the Mainform.
    >While loading the Mainform I want the text that was saved in the file
    >to be compared with the items in the ComboBox ad get the index of the
    >found item, so that I can set it as selected.
    >
    >I thought i could do it something like this, but the program hangs
    >before I can see anything.
    >---------
    >Code
    >---------
    > Dim i, As Integer
    > Dim line As String
    > For i = 0 To ComboBox1.Items .Count
    > If ComboBox1.Items (i) = line Then
    > IndexFound = i
    > Exit For
    > End If
    > Next
    >---------
    >Code
    >---------
    >
    >Can anyone help me please,
    >
    >Thanx[/color]

    Comment

    • Randy Birch

      #3
      Re: Compare text in a combobox

      The easiest way, if there can be only one match, is by using the
      CB_FINDSTRING or CB_FINDSTRINGEX ACT messages ...

      sItemToFind = "Hello World"

      matchIndex = SendMessage(com bo1.hwnd, CB_FINDSTRINGEX ACT, -1, ByVal
      sItemToFind)

      if matchIndex >-1 then msgbox "the item is index " & matchIndex


      CB_FINDSTRINGEX ACT (or LB_* for list boxes) does just that ... if the combo
      entry was "Hello World here I come" CB_FINDSTRINGEX ACT would fail above.
      CB_FINDSTRING on the other hand would locate the item. Note however if the
      item was "I'd like to say Hello World", neither messages would find the item
      since the item does not begin with the words "Hello World".


      --

      Randy Birch
      MVP Visual Basic

      Please respond only to the newsgroups so all can benefit.


      "Niels" <nielsh@holland ridderkerk.nl> wrote in message
      news:b19918e.04 05100250.2f0ad3 fe@posting.goog le.com...
      : Hello,
      : I'm trying to get te index of a specific text in a ComboBox.
      : The ComboBox is filled by searching for filenames in a directory.
      : The text of the last item selected in the ComboBox is saved in a
      : textfile in the OnExit Event of the Mainform.
      : While loading the Mainform I want the text that was saved in the file
      : to be compared with the items in the ComboBox ad get the index of the
      : found item, so that I can set it as selected.
      :
      : I thought i could do it something like this, but the program hangs
      : before I can see anything.
      : ---------
      : Code
      : ---------
      : Dim i, As Integer
      : Dim line As String
      : For i = 0 To ComboBox1.Items .Count
      : If ComboBox1.Items (i) = line Then
      : IndexFound = i
      : Exit For
      : End If
      : Next
      : ---------
      : Code
      : ---------
      :
      : Can anyone help me please,
      :
      : Thanx

      Comment

      • The Grim Reaper

        #4
        Re: Compare text in a combobox

        I've just upgraded 90% of my stuff to VB.NET.. if you hate 0 based array's
        steer clear... EVERY array is 0 based in .NET :((

        "J French" <erewhon@nowher e.com> wrote in message
        news:409f78bc.3 0176974@news.bt click.com...[color=blue]
        > I'll bet the program does not hang
        > - it should come up with a ruddy great Error Message
        >
        > For i = 0 To ComboBox1.Items .Count - 1
        >
        > Note the minus one
        > - Zero Based arrays are a PITA
        >
        >
        > On 10 May 2004 03:50:30 -0700, nielsh@hollandr idderkerk.nl (Niels)
        > wrote:
        >[color=green]
        > >Hello,
        > >I'm trying to get te index of a specific text in a ComboBox.
        > >The ComboBox is filled by searching for filenames in a directory.
        > >The text of the last item selected in the ComboBox is saved in a
        > >textfile in the OnExit Event of the Mainform.
        > >While loading the Mainform I want the text that was saved in the file
        > >to be compared with the items in the ComboBox ad get the index of the
        > >found item, so that I can set it as selected.
        > >
        > >I thought i could do it something like this, but the program hangs
        > >before I can see anything.
        > >---------
        > >Code
        > >---------
        > > Dim i, As Integer
        > > Dim line As String
        > > For i = 0 To ComboBox1.Items .Count
        > > If ComboBox1.Items (i) = line Then
        > > IndexFound = i
        > > Exit For
        > > End If
        > > Next
        > >---------
        > >Code
        > >---------
        > >
        > >Can anyone help me please,
        > >
        > >Thanx[/color]
        >[/color]


        Comment

        Working...