5 Replies - 7390 Views - Last Post: 09 November 2008 - 07:03 AM Rate Topic: -----

#1 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

How to compare text in a combobox with text in combobox items collecti

Posted 06 November 2008 - 06:08 PM

I have one combobox which has default items collections.
User can choose to write his/her own value in the combobox without using the default value hardcode in the list.
if user writes a string that is not found in the items collections, combobox will add the string, else nothing will be added.

Private Sub cbxEcho_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxEcho.TextChanged
        If cbxEcho.Text <> "" And cbxEcho.FindStringExact(cbxEcho.Text) = 0 Then
            cbxEcho.Items.Add(cbxEcho.Text)
            My.Settings.Echos.Add(cbxEcho.Text)
        End If

    End Sub



I ran the program, i tried add a string which is the same as the one in the list, the combo box still add this string.
So it just adds any new string regardless whether the string is equal to the string in the list or not....

Am I doing it wrongly?

Is This A Good Question/Topic? 0
  • +

Replies To: How to compare text in a combobox with text in combobox items collecti

#2 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

Re: How to compare text in a combobox with text in combobox items collecti

Posted 07 November 2008 - 05:06 AM

bump..

anyone?
Was This Post Helpful? 0
  • +
  • -

#3 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

Re: How to compare text in a combobox with text in combobox items collecti

Posted 07 November 2008 - 01:47 PM

Please help... :)
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How to compare text in a combobox with text in combobox items collecti

Posted 07 November 2008 - 10:00 PM

The TextChanged event will fire for each character you enter into the ComboBox. In addition the
ComboBox does not have a "SaveTextOnEnterKeyEvent" :)

What you want to do :) is have a a KeyDown event handler to detect when the user hits the enter or return keys. You also want a Leave event handler so that when the focus goes to another control
the contents of the textbox, if any, can be saved.

Note that the FindStringExact() method returns the index of the matched text or -1 if no match exists. Checking for zero will always find the first list entry!
    Private Sub cbxEcho_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cbxEcho.KeyDown
        If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return Then
            If cbxEcho.Text <> "" And cbxEcho.FindStringExact(cbxEcho.Text) < 0 Then
                cbxEcho.Items.Add(cbxEcho.Text)
                My.Settings.Echos.Add(cbxEcho.Text)
                cbxEcho.Text = ""
            End If
        End If
    End Sub

    Private Sub cbxEcho_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxEcho.Leave
        If cbxEcho.Text <> "" And cbxEcho.FindStringExact(cbxEcho.Text) < 0 Then
            cbxEcho.Items.Add(cbxEcho.Text)
            My.Settings.Echos.Add(cbxEcho.Text)
            cbxEcho.Text = ""
        End If
    End Sub


Was This Post Helpful? 1
  • +
  • -

#5 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

Re: How to compare text in a combobox with text in combobox items collecti

Posted 08 November 2008 - 03:02 PM

thanks for your help :)
Was This Post Helpful? 0
  • +
  • -

#6 Pwn  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 458
  • Joined: 25-November 07

Re: How to compare text in a combobox with text in combobox items collecti

Posted 09 November 2008 - 07:03 AM

View Postn8wxs, on 7 Nov, 2008 - 09:00 PM, said:

You also want a Leave event handler so that when the focus goes to another control
the contents of the textbox, if any, can be saved.


I would think twice about this, if somebody enters garbage into the textbox, then decides to click somewhere else (read kids on your pc for example or you are accidentally on the wrong control and trying to type) then it's going to add garbage to your list. I would make it strictly a purposeful add, such as enter or even put a button saying add to list.

This post has been edited by Pwn: 09 November 2008 - 07:05 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1