• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 906
  • Last Modified:

Clearing MFC Combo Box for repopulating

I have a combo box whose displayed list is dependent upon another user selection. When the user makes the selection,
the code:

  ModelCount = m_modelCombo.GetCount();

  for (i=0; i<ModelCount; i++)
    m_modelCombo.DeleteString(i);

is executed. The combo box "m_modelCombo" is the given new values through the AddString method.

The new values are being displayed, but the previous values are partially left over, and the list continues to grow with each update attempt. What am i missing?

Randy
0
cox8355
Asked:
cox8355
2 Solutions
 
fuzzyjon79Commented:
Have you tried using UpdateData(TRUE) to your code?  I would add the UpdateData(TRUE); line right underneath the m_modelCombo.DeleteString(i); line.  If that does not work try using UpdateData(FALSE);  I can't remember which one would work but try either one to see if that fixes your problem.  Hope that this helps.


Jon
0
 
subhadeepinCommented:
hi
Instead of using AddString; use InsertString.

for (int i=0; i<m_combo1.GetCount(); i++)
     m_combo1.DeleteString(i);
m_combo1.InsertString(0,"*.iii");
m_combo1.InsertString(1,"*.kkk");

That shall do the work for you

0
 
cox8355Author Commented:
Thanks...but...tried both of these, but the problem remains. Portions of the previous list still linger after supposedly being deleted. For example, this is what typically happens:

First list:     Second list:   ...etc.

1st_Item_1      2nd_Item_1
1st_Item_2      2nd_Item_2
1st_Item_3      2nd_Item_3
1st_Item_4      2nd_Item_4
1st_Item_5      2nd_Item_5
                1st_Item_2
                1st_Item_4

Any other ideas?

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
subhadeepinCommented:

This codes works perfectly well
Check it out


     int temp=m_combo1.GetCount();
     int text1;
     
     for(int i=temp;i>=0;--i)
          text1=m_combo1.DeleteString(i);
         
     
     
     m_combo1.InsertString(0,"hi");
     m_combo1.InsertString(1,"hello");
     
     m_combo1.SetCurSel(0);


0
 
subhadeepinCommented:
the problem is with the following loop

for (int i=0; i<m_combo1.GetCount(); i++)
    m_combo1.DeleteString(i);

As elements are deleted GetCount value reduces; so elements are left out not being deleted

The following code does the work You need to do


void importexport::OnChange()
{
     int temp=m_combo1.GetCount();
     int text1;
     
     for(int i=temp-1;i>=0;--i)
          text1=m_combo1.DeleteString(i);
         
     
     
     m_combo1.InsertString(0,"hi");
     m_combo1.InsertString(1,"hello");
     
     m_combo1.SetCurSel(0);


}
0
 
cox8355Author Commented:
Thanks!

Still somewhat of a mystery for this reason...I was going from element 0 to (GetCount-1), where your loop was (GetCount) to 0.

I thought if GetCount returned, say, 10, that the elements would be referenced from 0 to 9. Your loop deletes elements 10 through 0....one more than it seems it should...??? Maybe I'm missing something that I'll see later...but it works.

Many thanks,
Randy
0
 
LSkywalkerCommented:
Try
m_Combo.ResetContent();

This will delete all items rather than for you to loop thru.

LS
0
 
bcladdCommented:
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points split between subhadeepin and LSkywalker

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now