Topic: Populate a combobox

Hi All,
.
I have a combobox that I populate with the following script. I wanted to be able to let the user choose between only 2 choices so I didn't want to have to create a separate lookup table for only these 2 choices.
frmEquip.cboInstalledSpare.Items.Add('Installed');
frmEquip.cboInstalledSpare.Items.Add('Spare') ;

.
The script works fine when I'm adding a new record(NewRecord) however when I'm opening a Saved record(ShowRecord) I would like the combobox to show the entry that had been saved previously.  And also allow them to change the entry if they need to.  I think I may be asking too much from the combobox?
.
Any thoughts?
Thanks
Frank

Re: Populate a combobox

Heya papafrankc,
.
Try to prefix your code with:

frmEquip.cboInstalledSpare.Items.Clear;
"Energy and persistence conquer all things."

Re: Populate a combobox

JoshuA,

I actually already do have that line of code just before the ADD lines.  I use this procedure when I'm adding a NEWRECORD and that works OK.  It does clear out the previous entries.  I should have put all my code in my example.  Here it is:

if action = 'ShowRecord' then
  begin
    frmEquip.cboInstalledSpare.Items.Clear ;
    frmEquip.cboInstalledSpare.Items.Add('Installed');
    frmEquip.cboInstalledSpare.Items.Add('Spare') ;
    frmEquip.cboInstalledSpare.DroppedDown := True ;
  end;

    // frmEquip.cboInstalledSpare.Items.Text := frmEquip.EqSparePart.Text ;
.

When I run it as above, it does clear the combobox
And it does populate it with both Installed and Spare
However, since this is a SHOWRECORD procedure, it does not show the previous entry that was chosen before.
I thought by adding the DROPPEDDOWN option it might show what is in the combobox, but all it does is highlight the combobox, it doesn't open it.
.
If I add the line that is commented out, it does put the old saved entry in the combobox.  However it does not show it unless I click on the down arrow of the combobox.  And then I see only the previous entry and not both entries in case I want to change them.
.
The more I think about it it seems that I'm trying to get 2 functions out of the combobox.  1 - show the previous entry AND then 2 - let me change it if I want.  Now that I write down this explanation it sounds more and more to me that this is not the way to go about it?  Maybe there's a better way to try to do what I'm trying to do?  I may have to go back to having a table with 2 entries - Installed and Spare?
.
Sorry if this sounds confusing.
.
Thanks
Frank

4 (edited by sparrow 2021-11-25 12:44:18)

Re: Populate a combobox

Hello everyone.
frmEquip.cboInstalledSpare.Items.Add('string') - adds text to the ComboBox. One by one to the end of the list.
     Add returns the position of the item in the list, where the first item in the list has a value of 0.
frmEquip.cboInstalledSpare.Items.Insert(1, 'string') - inserts text at the position you specify.
frmEquip.cboInstalledSpare.ItemIndex: = 1 - allows you to specify the record number to display in the ComboBox
frmEquip.cboInstalledSpare.DroppedDown - Indicates whether the drop-down list is currently displayed. You don't need it))).


In your case:

if action = 'ShowRecord' then
  begin
    frmEquip.cboInstalledSpare.Items.Clear;
    frmEquip.cboInstalledSpare.Items.Add('Installed');
    frmEquip.cboInstalledSpare.Items.Add('Spare');
  end;

frmEquip.cboInstalledSpare.Items.Insert(1, frmEquip.EqSparePart.Text);  // Inserts text at the first position. 
    // If you use .Add, the entry will be appended to the end.
frmEquip.cboInstalledSpare.ItemIndex := 1; // shows the first entry.

OR


if action = 'ShowRecord' then
  begin
    frmEquip.cboInstalledSpare.Items.Clear;
    frmEquip.cboInstalledSpare.Items.Add('Installed');
    frmEquip.cboInstalledSpare.Items.Add('Spare');
  end;

frmEquip.cboInstalledSpare.ItemIndex := frmEquip.cboInstalledSpare.Items.Add(frmEquip.EqSparePart.Text); 
// adds text to the ComboBox and show this text.

Re: Populate a combobox

Hi Frank, Joshua, Sparrow,
Personally, I'd go with just creating another look-up table and not worry about any possible 'overhead'.
1,  it's 'standard' MVD,
2.  you're not hard-coding anything into your script and therefore it will be easier to maintain.
3.  if you create all of your look-up tables as editable tablegrids, it's probably quicker than writing a script.
4.  if you decided to change the wording of the combobox values (ie from 'spare' to 'not installed') it can be done by the user without any input from the programmer.
4.  It's consistent as all look-ups can be maintained from a single form.
5.  having everything on one form makes it easy, if required, to restrict access to certain key users.
6.  your scripts will be more straightforward
For me, it's hard to see a down-side.
Derek.

Post's attachments

Attachment icon lookups.zip 339.25 kb, 191 downloads since 2021-11-25 

Re: Populate a combobox

Thanks so much folks for the tips and suggestions.
.
Sparrow,
I appreciate your explanations about the options for a combobox.  I've learned that in scripting there can often be a number of options associated with most code.  Until someone like yourself explains these options or I see it in an example, then I'm not usually aware of their existence.  I do a lot of Google searches when I'm trying to do something new but that can be time consuming and doesn't always prove helpful.
.
Derek,
I see a lot of logic in your reply about letting MVD do as much work as it can, based on it's design.  I'm seeing that sometimes I get a little too deeply involved in using IF statements to do what I want.  I need to first focus on MVD's built in features before jumping into scripting.  And since my scripting skills are still pretty limited, in case you haven't noticed HA, I need to give MVD a chance to do the job.
I think my SHOWRECORD code full of IF statements is a good example of how doing too much scripting can be a bad idea.
.
I don't know that I'll ever be a "programmer" but I'm having a good time along the way.
.
Thanks
Frank

Re: Populate a combobox

Sparrow,
I just want to thank you for this code suggestion:
frmEquip.cboInstalledSpare.ItemIndex: = 1 // - allows you to specify the record number to display in the ComboBox
.
It helped me fix a problem I had been struggling with for days.  My ComboBox kept going back to SPARE when I wanted it to say INSTALLED.
. NOTE: I noticed that in my lookup table that SPARE is position 0 and INSTALLED is position 1.  So I used 1 and it put in INSTALLED just like I wanted.
.
I thought when I first saw your references to INDEX that I probably wouldn't need to use it.  But as I got further into things it saved my day smile
.
I also cleaned up some of my code and put in a number of well-placed comments, along with consistent indenting.  Now it's much easier for me to figure out what is going on.
Many thanks
Frank

Re: Populate a combobox

Hi Papafrankc, hi Derek


Papafranc I'm glad I helped you.
Just do not try to make a universal mechanism for all occasions from your program ).
Derek gives good advice. I will add, strive for reasonable simplicity in the solution.


Searching on Google is great. But start with what's on the forum itself. The number of examples,
descriptions, questions and solutions on the forum allows you to solve most of the issues.
Here is a link to a help page on properties, component functions and more
- http://myvisualdatabase.com/help_en/ComboBox.html
On the left, you can select other components and categories.
If the description does not contain an example of use, you can find it on the program forum.


You can also search the forum via Google.
The search string will be in the form - "your text site:myvisualdatabase.com",
where "your text" is your search text.
This search will avoid some of the limitations of the forum search.
And of course ask questions).


The main thing is that you enjoy the process, even if you are not a programmer.

Re: Populate a combobox

Sparrow,
I have seen the help site that you referenced and I have been there before.  There's a lot of good info on the site.
.
When I'm searching for help info I usually do start with an MVD search.  If the result is empty or doesn't address what I'm looking for, then I usually go to Google.  An interesting note - when I go to Google, usually they send me back to MVD.  They provide a link to a forum string that many times more closely addresses my subject.
.
The format you provided your text site:myvisualdatabase.com is different from what I was using.  I used yourtext "myvisualdatabase".  It looks like the 2 searches show pretty much the same result.  Although yours is slightly simpler.
.
My application was coming along nicely until I had that combobox problem with INSTALLED and SPARE.  I would pick INSTALLED then I would go to another form to enter Service info for that piece of equipment.  Then when I would close that form and come back to the previous form, my choice of INSTALLED would have changed.  It's a small thing but is pretty important to the way I have designed the program.  So, as I mentioned, using the INDEX option for the ComboBox fixed that problem.
.
I have to work on trying to keep things simpler.  With all the help from yourself and others on the forum, plus my trial and error experiences, I think I'm making progress.
.
Thanks for having the patience to work with us newbies smile
Frank