New topic: Check Combobox Contents
<http://forums.realsoftware.com/viewtopic.php?t=45253> Page 1 of 1 [ 8 posts ] Previous topic | Next topic Author Message JaroDefer Post subject: Check Combobox ContentsPosted: Tue Sep 04, 2012 4:51 pm Joined: Tue Sep 04, 2012 4:41 pm Posts: 3 Hello, I have, what I believe to be, a simple question. I have a combobox that is populated with entries when the program is opened. There is a button that, when clicked, am looking to have the following behavior: If the user manually entered text into the combobox that isn't already in the list, add it to the list. If the user manually entered text into the combobox thas is already in the list, do nothing. If the user selected something from the combobox dropdown, do nothing, as that item is clearly already in the list. Thanks so much for the help! Top Alex2k Post subject: Re: Check Combobox ContentsPosted: Tue Sep 04, 2012 5:03 pm Joined: Sun Nov 26, 2006 11:01 am Posts: 56 Try using a dictionary which stores all Combobox entries, there you can easyily check if a key exists in the dictionary. Top JaroDefer Post subject: Re: Check Combobox ContentsPosted: Tue Sep 04, 2012 6:12 pm Joined: Tue Sep 04, 2012 4:41 pm Posts: 3 Thanks for the quick reply. I forgot to mention in my first post that I am very new Real Studio and this language. I assume that I need to make the dictionary global in order to populate it during the open event and then search it within the button's activate event. After searching around I figured out how to declare global variables, but it doesn't seem to work quite the same for a dictionary. In the Real Studio Language Reference they use the syntax of 'Dim d as New Dictionary' which works fine when declaring a local variable, but i can't declare 'New Dictionary' only 'Dictionary' when creating a new property. Thanks for holding my hand through this while I get acclimated to the language. Top mallen Post subject: Re: Check Combobox ContentsPosted: Tue Sep 04, 2012 6:27 pm Joined: Mon Mar 01, 2010 11:59 pm Posts: 55 Location: Silicon Valley Quote:In the Real Studio Language Reference they use the syntax of 'Dim d as New Dictionary' which works fine when declaring a local variable, but i can't declare 'New Dictionary' only 'Dictionary' when creating a new property. You are right. After you create the dictionary, in an open event add: d = New Dictionary it instantiate it. I'ld use a more descriptive name for your dictionary, to make your code more readable. _________________ mallen iMac 3.4 GHz Intel Core i7; 16 GB; OS X 10.8.1; RS 2012r1 MacBook Pro 2.8 GHz Intel Core 2 Duo; 4 GB; OS X 10.8.1; RS 2012r1 "Sometimes you're the windshield, Sometimes you're the bug." â Mark Knopfler, The Bug, 1991 Top prakashp Post subject: Re: Check Combobox ContentsPosted: Wed Sep 05, 2012 1:05 am Joined: Tue Dec 27, 2011 8:54 am Posts: 136 Location: India HI JaroDefer, Quote:I have a combobox that is populated with entries when the program is opened. You can add the items to the combobox using the Recordset object. Write a query for get all the data from the database and keep the data inside a recordset and using loop you can bind all the data to the combobox like me.AddRow TmpRecordSet.Field("LocationName").StringValue me.RowTag(me.ListCount - 1) = TmpRecordSet.Field("LocationId").Value Quote:If the user manually entered text into the combobox that isn't already in the list, add it to the list. If the user manually entered text into the combobox thas is already in the list, do nothing. If the user selected something from the combobox dropdown, do nothing, as that item is clearly already in the list. You can add your code in "LostFocus" event of combobox. Simple logic is like : In LostFocus of combobox event you will get the value using "me.Text.Trim" and before inserting into the database, first check that this item is already exists or not. if Exist then skip it else insert the new item. Please let me know if you have a query. _________________ PK Pothal Software Developer in ASP.NET, VB.NET, C#, REAL STUDIO AND SQL SERVER 2005/08 WINDOWS N MAC Top amitavak Post subject: Re: Check Combobox ContentsPosted: Wed Sep 05, 2012 3:26 am Joined: Mon Jan 02, 2012 1:51 am Posts: 274 Location: India JaroDefer wrote:There is a button that, when clicked, am looking to have the following behavior: In the button action event, //Check no item is selected in the combobox If ComboBox1.ListIndex < 0 And ComboBox1.Text.Trim <> "" Then //loop through the items to search the text For i as Integer =0 To ComboBox1.ListCount - 1 If ComboBox1.List(i) = ComboBox1.Text Then Return //found do nothing End If Next //Add the text in the combobox ComboBox1.AddRow ComboBox1.Text.Trim End If prakashp wrote:You can add your code in "LostFocus" event of combobox. As prakash mention, you can also do it in lostFocus event of the comboBox. _________________ Amitava Real Studio 2011 Release 4 Windows 7 (32 bit) Top JaroDefer Post subject: Re: Check Combobox ContentsPosted: Wed Sep 05, 2012 8:38 am Joined: Tue Sep 04, 2012 4:41 pm Posts: 3 Thanks for the great suggestions everyone! This forum is incredibly fast and helpful. I will make different versions and try all of these methods for learning purposes. What does comboBox1.text.trim do that comboBox.text does not? Is the .trim only necessary when using comboboxes? I didn't seem to need that for text fields. Amltavak, your solution was the most logical to me at this point in my coding knowledge since my list is so small, but I can definitely see the speed benefit of the dictionary and will use it in the future. That code worked perfectly until I selected another item in the the list before typing in a custom string. Removing comboBox1.ListIndex < 0 caused the button to perform as expected. One last question about that snippit that you posted. The return statement in that if then statement will exit the Action() method completely, so if I want the button to perform other actions regardless of the results on that check I will have to put this code in a separate method that I call from the button, correct? Thanks again for the help! Top amitavak Post subject: Re: Check Combobox ContentsPosted: Wed Sep 05, 2012 9:48 am Joined: Mon Jan 02, 2012 1:51 am Posts: 274 Location: India JaroDefer wrote:What does comboBox1.text.trim do that comboBox.text does not? Is the .trim only necessary when using comboboxes? I didn't seem to need that for text fields. "Trim" just remove the leading and trailing whitespaces. Can be used with any string. Look here. JaroDefer wrote:That code worked perfectly until I selected another item in the the list before typing in a custom string. Removing comboBox1.ListIndex < 0 caused the button to perform as expected. yes that condition is unnecessary. JaroDefer wrote:One last question about that snippit that you posted. The return statement in that if then statement will exit the Action() method completely, so if I want the button to perform other actions regardless of the results on that check I will have to put this code in a separate method that I call from the button, correct? correct. _________________ Amitava Real Studio 2011 Release 4 Windows 7 (32 bit) Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 8 posts ]
-- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml rbforumnotifier@monkeybreadsoftware.de