Board index » cppbuilder » code request: Sort MainMenu

code request: Sort MainMenu


2005-01-08 09:59:57 AM
cppbuilder50
I am looking for code to sort the contents of a drop down menu in the
TMainMenu. Does anyone have code for doing this they can send me? I would be
very grateful. XXXX@XXXXX.COM
Thanks.
Larry Johnson
 
 

Re:code request: Sort MainMenu

"LarryJ" < XXXX@XXXXX.COM >wrote:
Quote

I am looking for code to sort the contents of a drop down
menu in the TMainMenu. [...]
In actuality, I can't see where you would even need to sort
the menu. If the menu is one that you have already designed
using the IDE, then you can use the Menu Designer (right click
the component) to drag the individual menu item to the desired
location.
If you are creating the menu or modifying the menu at runtime,
then use the Insert method instead of the Add method so that
you can keep the menu sorted as you go.
If you have an MDI application and you want to do something
with the MDIChild menus that get merged into the main form's
menu, that's a bit different but basically the same concept of
Insert(ing) them into the desired location.
Sorting a menu is not too difficult to implement but why do you
think that you need such a function? What exactly are you
trying to accomplish under what circumstances?
~ JD
 

Re:code request: Sort MainMenu

I am using a menu that have all the drop down items dynamically created.
There can be items added and deleted to the menu. I need the menu in
alphabetical order. The Insert method does not have a sort function. I still
need the code to loop through the menu and figure out where to use Insert.
So the question is how do I locate the correct position to insert a new menu
item?
Thanks
Larry
"JD" < XXXX@XXXXX.COM >wrote in message
Quote

Sorting a menu is not too difficult to implement but why do you
think that you need such a function? What exactly are you
trying to accomplish under what circumstances?

~ JD

 

{smallsort}

Re:code request: Sort MainMenu

Why not try loading the menu captions in a TStringList
to handle the sorting, then repopulate the menu captions
based on the TStringList sorted items?
Kent
 

Re:code request: Sort MainMenu

"LarryJ" < XXXX@XXXXX.COM >wrote:
Quote

[...] So the question is how do I locate the correct
position to insert a new menu item?
Since you're already adding the items, then you already know
which menu item that you're adding it to so I designed the
function to accept as parameters, the menu items that is to be
added and the menu item that it's being added to:
//-------------------------------------------------------------
void __fastcall TForm1::InsertMenuItem( TMenuItem *ParentItem, TMenuItem *ChildItem )
{
bool Inserted = false;
AnsiString ChildCaption = stripAmpersand( ChildItem->Caption );
for( int x = 0; x < ParentItem->Items->Count && !Inserted; ++x )
{
AnsiString CurrentCaption = stripAmpersand( ParentItem->Items[x]->Caption );
if( AnsiCompareText(CurrentCaption, ChildCaption)>0 )
{
ParentItem->Insert( x, ChildItem );
Inserted = true;
}
}
if( !Inserted ) ParentItem->Add( ChildItem );
}
//-------------------------------------------------------------
AnsiString __fastcall TForm1::stripAmpersand( AnsiString s )
{
int Pos = s.Pos("&");
if( Pos ) s.Delete( Pos, 1 );
return s;
}
//-------------------------------------------------------------
~ JD
 

Re:code request: Sort MainMenu

Thanks, that code worked for me.
"JD" < XXXX@XXXXX.COM >wrote in message
Quote