Topic: Combobox default entry

I have a Country cbo field and I would like to have it be populated with USA as a default, because I assume most of my users will be in the US.  The dropdown does contain other countries and the ability to add/delete more countries, so the user can change the Country if they wish.
-
I set the Country field in my Country database to USA, but USA doesn't show up on the forms.
-
Thanks, Frank

Re: Combobox default entry

Frank,


In the object inspector there is a property called  'DefaultIndex'  it will by default be set at 0, Set it to the index number that your USA entry is at and your problem is solved.

On a clear disk you can seek forever

Re: Combobox default entry

CDB,
Thanks for the tip.  I saw the DefaultIndex but didn't know what it was for so I never tried changing the number.
-
It does work as you say but I have a problem.  I have added an option where the user can ADD or EDIT an entry in the Country Dropdown list.  So when I add a new Country to the list it changes the number of many of the entries. USA was #22 in the list but after I add a new entry USA becomes #23.
-
I have the combobox set to sort on the entries.
-
I saw a "default" setting in the Country database for the country field.  I tried setting it to USA but it didn't seem to have any effect.  I'm not sure what it does?
-
Thanks, Frank

4 (edited by derek 2020-10-12 17:35:08)

Re: Combobox default entry

Hi Frank,
I rather guessed you might hit that problem - the 'defaultindex' is actually the row number in the combobox and, as you say, as soon as you add entries to a combobox list that is sorted, the value(country) associated with the 'defaultindex' changes. 
Personally I think the 'defaultindex' should be the actual record id in the table but that's something for Dmitry to decide.
I've always got around it by doing the following:
1.  In the 'country' (or whatever) table, add an extra column (I call it 'defaultcountry' (very imaginative - LOL!). 
2.  When you add countries to the countries table, nominate one (I use an '*') to be the default country.
3.  Set your combobox to sorted.
4.  Use a small script that selects the 'default' country according to which country has an '*' against it and load that into the combobox.text.
Doing it this way allows you to optionally have the first item blank (so it's not just a 'default', it's also a 'default only if you're filling in the 'country' dropdown).
But the big advantage is that your user gets to nominate what country is the default - so if someone is using your software in Canada, for example, it's now under his control and you don't have to get involved.
And obviously, you can just repeat the code in the script for as many comboboxes as you use in your application where you want to use defaults.
All of which sounds way more complicated (as usual) than it actually is - have a look at the attached example and hopefully it will be clear.
And finally, a slight amendment to the above would be to have your default country always at the top of the combobox list (overriding the alpha sort and then have all the other countries listed alphabetically) - if that's something you'd prefer, give me a shout and I can change the script accordingly.
Derek.

Post's attachments

Attachment icon frank combobox defaults.zip 338.24 kb, 233 downloads since 2020-10-12 

5 (edited by papafrankc 2020-10-13 06:51:50)

Re: Combobox default entry

Derek,
Thanks for the example.  It works fine and does handle the default country OK.
-
Here's my thoughts based on the program and how I think it should work. (I know it seems like I always want more). But that's what you get when you give me new toys to play with smile
-
I think the idea of allowing the user to set the default country is a nice feature. 
-
Your example does just that.
What I'm wondering is if they go to change the default country could they only see the list of countries, without the asterisks?
Then if they double-clicked on a different country it would automatically set that country as the default. There could still be a field for the asterisk but just not visible to the user.
And then I will put the Set Default button on the Start form so they can set it once and not have to do it on each form with a country field.
-
*****Maybe you can also help me with one another thing. I was doing some trial and error stuff and I tried to add a text field to Form1 that will show the country when the form is opened. I was thinking I wanted to show the default country when the form opens.  Well when I start the form, my country text field is blank.  I put fields on my project forms all the time and the data shows up OK.  But I can't seem to figure how to make this work.  I think I'm missing something pretty basic??
-
Thanks for your help, Frank

Re: Combobox default entry

Hi Frank,

And then I will put the Set Default button on the Start form so they can set it once and not have to do it on each form with a country field.

Not sure I understand you on this one - you only have to set the default once on FormLookUps (see screenshot) and not on each form that has a country combobox.
As for showing the country on Form1, do you mean showing what the default country is?  If so, it probably doesn't relate to any of the other information on Form 1 so I guess you'd need to call it with a script (Form1_OnShow) -
form1.edit1.text := sqlexecute'(select country from countries where defaultcountry = "*"'); - something like that.
Derek.

Post's attachments

Attachment icon screenshot.jpg 105.35 kb, 101 downloads since 2020-10-13 

Re: Combobox default entry

Derek,
I apologize for my unclear reply.
I was thinking that I would have to call the FormLookups from each of the forms where I had a country field.  So calling it once from my Start form is what I was thinking and I believe that's what you are saying. So I should be good to go.
-
As far as showing the country on form1 goes, your script (Form1_OnShow) is exactly what I was looking for.  I did have to move the asterisk to the right side of the "(" for it to work. But it works fine now.
This is a side issue from the default country and I was just trying something I had thought of.  I didn't know what the command would look like. Now I know how to call a field anytime I want so I can use it in the future if appropriate.  I learned something new, again, thanks.
-
On the default country issue, I'm wondering how hard it might be to have the user set the asterisk for a country by double-clicking on a country name and not having to delete the old asterisk and insert a new one for a different country name? It's not that difficult to perform this step, I'm just thinking about making it as simple for the user as possible.
-
Thanks, Frank

8 (edited by derek 2020-10-13 18:35:33)

Re: Combobox default entry

Hi,
The problem might be differentiating between simply selecting a country from the drop-down and selecting a country that you want to be the default. Besides, once the default country (or any other default settings) has been set, why would it change (other than very rarely)?
For me, using the 'asterisk' function is more so the user can set it when they first get the software so the developer doesn't have to get involved with each new installation.
Derek.

Re: Combobox default entry

Hi Derek,

Thanks to your help, I've got it working the way I want.
They can now choose a default Country on the programs Start page.
Then when they go through the forms they will see their default country.
-
And then when they go through the forms they can ADD or Edit a country as needed on that particular form.
-
And now I can implement this procedure to any of the other comboboxes that it might apply to.
-
Thanks again for your help.  I've learned a lot from this exercise.
Frank