problem with FindString in a ListBox

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 2833 Views - Last Post: 04 February 2017 - 07:28 AM Rate Topic: -----

#1 BernardDijon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 18-October 15

problem with FindString in a ListBox

Posted 28 January 2017 - 10:58 AM

Hi

I have a ListBox with the following items
2 NOTE Test de Notes 1
3 CONT Test de Notes 2
3 CONT Test de Notes 3

I want to select all the items that contains a defined char and use the following function

Public Function ChercheCharDansNote(chr As String) As Boolean
        Dim el As Integer = LBNotes.FindString(chr)
        Dim trouve As Boolean = False
        With LBNotes
            Do
                Debug.Print("------------------")
                Debug.Print(LBNotes.Items(0).ToString)
                Debug.Print(LBNotes.Items(1).ToString)
                Debug.Print(LBNotes.Items(2).ToString)
                If el <> -1 Then
                    trouve = True
                    If .SelectedIndices.Count > 0 Then
                        If el = .SelectedIndices(0) Then
                            Exit Do
                        End If
                    End If
                    .SetSelected(el, True)
                    .Refresh()
                End If
            Loop While el <> -1
        End With
        Return trouve
    End Function


But it works only if I try to find a char on the fist caracter of the line, that is 2 or 3.
Any other search returns -1.

The help seems to indicate that I should be able to find any string in whole items, correct?
What is wrong?

THanks for help.
Bernard

Is This A Good Question/Topic? 0
  • +

Replies To: problem with FindString in a ListBox

#2 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Re: problem with FindString in a ListBox

Posted 28 January 2017 - 12:32 PM

If you read the msdn documents on the ListBox.FindString Method you would find that it only finds the first item that starts with the given string to find.

If you want to find any items in the ListBox that contain the given string to find anywhere in them, then you will want to iterate through the items in the listbox and check each one to see if it contains the given string to find. You can use the String.Contains Method to do that.

If it contains the given string to find, then add the index of the item to the ListBox`s SelectedIndices.

For example...
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim StringToFind As String = "2" 'This is the string that you want to find in the items

        For indx As Integer = 0 To ListBox1.Items.Count - 1 'iterate through the items
            'if the item contains the StringToFind, then add the index to the SelectedIndices
            If ListBox1.Items(indx).ToString.Contains(StringToFind) Then
                ListBox1.SelectedIndices.Add(indx)
            End If
        Next
    End Sub



EDIT: I should also mention that if you do not want the search to be case sensitive, you can take advantage of the String.ToLower Method in the line below that finds the string.
If ListBox1.Items(indx).ToString.ToLower.Contains(StringToFind.ToLower) Then


If you searched for "A", it would find any items that contain a capital "A" or lower case "a".
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6795
  • View blog
  • Posts: 28,082
  • Joined: 12-December 12

Re: problem with FindString in a ListBox

Posted 28 January 2017 - 12:37 PM

FindString

The first sentence says "Finds the first item in the ListBox that starts with the specified string."

If you describe clearly what you want to achieve you'll probably receive some advice about how to approach this.

In particular, are you looking for a single character, such as the numbers 2 or 3? And anywhere in the item(s)? This wouldn't provide useful information based on your sample data as surely the numbers at the beginning or end represent two different things?
Was This Post Helpful? 0
  • +
  • -

#4 BernardDijon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 18-October 15

Re: problem with FindString in a ListBox

Posted 29 January 2017 - 03:25 AM

Hi Andrew

Yes I'm trying to find the first instance of a caracter like 2 or 3 or " anywhere in the line and multiselect all the lines that contain this caracter.
In fact this code only find the caracter if it is in the first posiotion of the line.
You say that the code will detect the line starting with which is not really clear in the help.
How to solve my problem?
Thanks for your help.

Regards
Bernard
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6795
  • View blog
  • Posts: 28,082
  • Joined: 12-December 12

Re: problem with FindString in a ListBox

Posted 29 January 2017 - 03:44 AM

Did you miss IronRazer's post above?
Was This Post Helpful? 0
  • +
  • -

#6 BernardDijon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 18-October 15

Re: problem with FindString in a ListBox

Posted 29 January 2017 - 05:05 AM

Hi
Yes I missed it!!
THank you very much!
Bernard
Was This Post Helpful? 0
  • +
  • -

#7 BernardDijon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 18-October 15

Re: problem with FindString in a ListBox

Posted 29 January 2017 - 05:57 AM

Hi
Now my search is working fine thanks to your help with the following code
Public Function ChercheCharDansNote(chr As String) As Boolean
        Dim trouve As Boolean = False       
        For indx As Integer = 0 To LBNotes.Items.Count - 1
            If LBNotes.Items(indx).ToString.Contains(chr) Then
                trouve=True
                LBNotes.SelectedIndices.Add(indx)
            End If
        Next
        LBNotes.Refresh()
        Return trouve
    End Function


that select in the listBox the lines containing the search letter.

Now I want to be able to click on a selected line and bring this line in a TextBox in order to alow me to modify this line and return the new version in the selected line of the ListBox
For that I have the following function

Private Sub LBNotes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LBNotes.SelectedIndexChanged        
            Dim idx As Integer=LBNotes.SelectedIndex
            TBLigne.Text = LBNotes.SelectedItem.ToString()           
 TBLigne.Refresh()
        
    End Sub


But whatever is the line clicked in the ListBox the SElectedIndes is always 1 and the selectedItem is also always the text of the line 1!!
What is wrong ?

THanks for your help
Bernard
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Re: problem with FindString in a ListBox

Posted 29 January 2017 - 06:53 AM

It is not clear what you are trying to do. However, perhaps this is what you are trying to do, you will want the ListBox`s SelectionMode property set to MultiExtended for this...
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LBNotes.SelectionMode = SelectionMode.MultiExtended
    End Sub


    Private Sub LBNotes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LBNotes.SelectedIndexChanged
        If LBNotes.SelectedItem IsNot Nothing Then
            TextBox1.Text = LBNotes.SelectedItem.ToString 'show the SelectedItem in the textbox
        End If
    End Sub

    'this button is clicked to set the selected item`s text to whatever you type in the textbox.
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If LBNotes.SelectedItem IsNot Nothing Then 'make sure there is still an item selected
            LBNotes.Items(LBNotes.SelectedIndex) = TextBox1.Text 'set the selected item`s text to whatever is in TextBoxt1
        End If
    End Sub


Was This Post Helpful? 1
  • +
  • -

#9 BernardDijon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 18-October 15

Re: problem with FindString in a ListBox

Posted 29 January 2017 - 08:53 AM

Ok that is fine now.
The problem was coming from the SElectionMode set to Multisimple !!

Thanks
Bernard
Was This Post Helpful? 0
  • +
  • -

#10 BernardDijon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 18-October 15

Re: problem with FindString in a ListBox

Posted 30 January 2017 - 10:55 AM

Hi

I want to make the search on the following caracters " >> << that is coded in decimal 34, 194 187, 194 171.
I used the function AscW(34) for the "
but how to deal with 194187 and 194171 ?
Thanks

Bernard
Was This Post Helpful? 0
  • +
  • -

#11 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Re: problem with FindString in a ListBox

Posted 30 January 2017 - 12:29 PM

I don`t understand what you mean. Where are these characters coming from? Why do you need to convert them?

Can you just use a string like this...
Dim SomeString as String = Chr(34) & ">> <<"
Was This Post Helpful? 0
  • +
  • -

#12 BernardDijon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 18-October 15

Re: problem with FindString in a ListBox

Posted 30 January 2017 - 12:45 PM

Hi

In the text file that I read and put in the ListBox line per line I need to replace some letter or caracters by an other.
In fact I need to replace the first occurence of an " (code=34) by a <i>, the second one by </i>. that works fine. To find the " I use ChrW(34) .
But for the double braket << or >> code 194187 and 194171 ChrW does not work .
So my question is how shall I detect a caracter which code = 194171?
THanks
Bernard
Was This Post Helpful? 0
  • +
  • -

#13 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Re: problem with FindString in a ListBox

Posted 30 January 2017 - 01:26 PM

Why can`t you just use a string "<<" or ">>" to find the occurrences of them in the text?
Was This Post Helpful? 0
  • +
  • -

#14 BernardDijon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 18-October 15

Re: problem with FindString in a ListBox

Posted 30 January 2017 - 01:41 PM

Well << is only double < what I'm looking for is the caracter code in dec 194171 in the UTF-8 code

Bernad
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6795
  • View blog
  • Posts: 28,082
  • Joined: 12-December 12

Re: problem with FindString in a ListBox

Posted 30 January 2017 - 01:48 PM

194187 and 194171 are utf-8 character codes for chevron characters.

Can't you just embed them in the string to search for "«" ? You can hold Alt-Gr and type 0171 on the numeric keypad to obtain the character.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2