5 Replies - 1153 Views - Last Post: 21 November 2011 - 05:08 AM Rate Topic: -----

#1 cwd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-March 11

Find exact match of a word in a textbox if it exists

Posted 20 November 2011 - 10:51 PM

Hi,

I need a way to tell me if a certain word already exists in a multi-lined textbox. Right now I have a multi-lined textbox with some names inside it. After each name I add a newline so all the names are on their own line inside the textbox.

I use this code to add names to the textbox,

TextBox1.AppendText(ComboBox1.Text + vbCrLf)


Then I have been checking if the name exists by doing it this way,

If TextBox1.Text.Contains(ComboBox1.Text) = True Then
            MsgBox("Yes")
        Else
            MsgBox("No")
        End If


I have ran into a problem because lets say My textbox names look like this:

Ted
Frank
Ben
Teddy

The problem is that Ted and Teddy return the same when searching for Ted even if Ted isnt in the textbox at all, I have googled for the last few days and even tried RegularExpressions and still the same results. I think this might be a simple true or false check but I may be wrong... or even a loop or something line by line...

Any help in the right direction would be great,

Thanks for ready this.

Is This A Good Question/Topic? 0
  • +

Replies To: Find exact match of a word in a textbox if it exists

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Find exact match of a word in a textbox if it exists

Posted 20 November 2011 - 11:01 PM

For my view, I dont see if it a good idea to use textBox here, why not using listBox instead?
If you need to make sure of that, I think you will need a loop to loop through all lines of a textBox and use equal() method to check if a word equals to the one available at the line.
Was This Post Helpful? 0
  • +
  • -

#3 DimitriV  Icon User is offline

  • Don't try to save yourself… the circle is complete
  • member icon

Reputation: 544
  • View blog
  • Posts: 2,632
  • Joined: 24-July 11

Re: Find exact match of a word in a textbox if it exists

Posted 20 November 2011 - 11:04 PM

Wow. Perhaps check the ComboBox items, see if it changes anything:
If ComboBox1.Items.Contains(TextBox1.Text) = True Then
'Do stuff
Else
ComboBox1.Items.Add(TextBox1.Text)
End If



Wow smohd, how do you do that so fast?
Was This Post Helpful? 0
  • +
  • -

#4 cwd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-March 11

Re: Find exact match of a word in a textbox if it exists

Posted 21 November 2011 - 02:19 AM

I would like more information on how to loop through a textbox line for line.
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Find exact match of a word in a textbox if it exists

Posted 21 November 2011 - 02:32 AM

If a multiline property of a textBox is set to true, means you can use line property to get the text of every line. For example TextBox1.Lines(i) will give me the line i text. So you can use it with equal() method to check if is the one entered by user
Was This Post Helpful? 0
  • +
  • -

#6 cwd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-March 11

Re: Find exact match of a word in a textbox if it exists

Posted 21 November 2011 - 05:08 AM

Thanks guys, I figured it out now here is what I did. I am sure this isn't the best way but it will work great for what I need to do.

Dim strText = TextBox1.Text
        Dim arrLines = Split(strText, vbCrLf)
        Dim NE As Integer = 0


        For Each strLine In arrLines

            TextBox2.Text = (strLine)

            If ComboBox1.Text = strLine = True Then
                NE = 1
                Exit For
            End If

        Next


        If NE = 0 Then
            TextBox1.AppendText(TextBox2.Text + vbCrLf)
            ' do something
        Else
            ' do something
        End If


Thanks again for always helping me out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1