Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,308 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 802 people online right now. Registration is fast and FREE... Join Now!




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

 
Reply to this topicStart new topic

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

rakyomin
6 Nov, 2008 - 05:08 PM
Post #1

D.I.C Head
**

Joined: 12 Sep, 2008
Posts: 77



Thanked: 3 times
My Contributions
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.

vb

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?

User is offlineProfile CardPM
+Quote Post


rakyomin
RE: How To Compare Text In A Combobox With Text In Combobox Items Collecti
7 Nov, 2008 - 04:06 AM
Post #2

D.I.C Head
**

Joined: 12 Sep, 2008
Posts: 77



Thanked: 3 times
My Contributions
bump..

anyone?

User is offlineProfile CardPM
+Quote Post

rakyomin
RE: How To Compare Text In A Combobox With Text In Combobox Items Collecti
7 Nov, 2008 - 12:47 PM
Post #3

D.I.C Head
**

Joined: 12 Sep, 2008
Posts: 77



Thanked: 3 times
My Contributions
Please help... smile.gif
User is offlineProfile CardPM
+Quote Post

n8wxs
RE: How To Compare Text In A Combobox With Text In Combobox Items Collecti
7 Nov, 2008 - 09:00 PM
Post #4

--... ...-- -.. . -. ---.. .-- -..- ...
Group Icon

Joined: 6 Jan, 2008
Posts: 1,609



Thanked: 223 times
My Contributions
The TextChanged event will fire for each character you enter into the ComboBox. In addition the
ComboBox does not have a "SaveTextOnEnterKeyEvent" smile.gif

What you want to do smile.gif 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!
vb

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

User is offlineProfile CardPM
+Quote Post

rakyomin
RE: How To Compare Text In A Combobox With Text In Combobox Items Collecti
8 Nov, 2008 - 02:02 PM
Post #5

D.I.C Head
**

Joined: 12 Sep, 2008
Posts: 77



Thanked: 3 times
My Contributions
thanks for your help smile.gif
User is offlineProfile CardPM
+Quote Post

Pwn
RE: How To Compare Text In A Combobox With Text In Combobox Items Collecti
9 Nov, 2008 - 06:03 AM
Post #6

D.I.C Regular
***

Joined: 25 Nov, 2007
Posts: 319



Thanked: 12 times
My Contributions
QUOTE(n8wxs @ 7 Nov, 2008 - 09:00 PM) *

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: 9 Nov, 2008 - 06:05 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 06:56PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month