Dropbox Sort

General help with the Ecere Cross Platform GUI toolkit: Window, common controls, events, etc.
Help with the 2D Graphics library: Surface, Display, Bitmap, Font and others.
Post Reply
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Dropbox Sort

Post by jerome »

nickdobrinich asks:
Jerome,
Can you please show me a brief example of the DropBox.Sort() method?
I can't quite figure out the DataField parameter.

As a workaround I added elements to an array of strings, qsorted that and then DropBox.AddString() them to the box.
Seems that the DropBox.Sort() would do the same.
Hi Nick,

Upon creation, elements in a DropBox are automatically sorted in ascending order by the first DataField.

When adding an element later, however, that element does not get inserted at the sorted place.
This is because usually, drop boxes are used with a fixed list of elements and are sorted before form creation.

To sort the contents when adding an element, simply call sort with either 1 for ascending order or -1 for ascending order. By default, a DropBox (like a ListBox) has a single DataField of 'String' data type. Passing null for the 'DataField' to a DropBox method refers to the first field (the default field in this case).

Here is an example:

Code: Select all

import "ecere"
 
class Form1 : Window
{
   caption = "Form1";
   background = activeBorder;
   borderStyle = sizable;
   hasMaximize = true;
   hasMinimize = true;
   hasClose = true;
   size = { 640, 480 };
 
   DropBox dropBox1 { this, position = { 144, 88 } };
   Button button1
   {
      this, caption = "button1", position = { 312, 240 };
 
      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         dropBox1.AddString("Fox");
         dropBox1.Sort(null, -1);
         return true;
      }
   };
 
   Form1()
   {
      dropBox1.AddString("Elephant");
      dropBox1.AddString("Dragon");
      dropBox1.AddString("Bee");
      dropBox1.AddString("Chimpanzee");
      dropBox1.AddString("Alligator");
   }
}
 
Form1 form1 {};
I assume you are using the default 'String' DataField. If you want to use another data type, for example an int, or multiple data fields so that the rows consist of multiple columns, that is when you create and add the data fields to the DropBox. In this case you can decide whether to sort by any of the data fields. Here is a more complex examples:

Code: Select all

import "ecere"
 
//define sortField = fldAnimal; // Sort by animal
define order = -1;            // Descending
 
define sortField = fldInt; // Sort by integer
//define order = 1;          // Ascending
 
class Form1 : Window
{
   caption = "Form1";
   background = activeBorder;
   borderStyle = sizable;
   hasMaximize = true;
   hasMinimize = true;
   hasClose = true;
   size = { 640, 480 };
 
   DropBox dropBox1 { this, position = { 144, 88 } };
   DataField fldInt { dataType = "int", alignment = center, width = 10 };
   DataField fldAnimal { dataType = "String", alignment = center, width = 0 }; // width = 0 to use the rest of the row
   Button button1
   {
      this, caption = "button1", position = { 312, 240 };
 
      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         DataRow row = dropBox1.AddRow(); row.SetData(fldAnimal, "Fox"); row.SetData(fldInt, 6);
         dropBox1.Sort(sortField, order); 
         return true;
      }
   };
 
   Form1()
   {
      DataRow row;
 
      dropBox1.AddField(fldInt);
      dropBox1.AddField(fldAnimal);
 
      row = dropBox1.AddRow(); row.SetData(fldAnimal, "Elephant"); row.SetData(fldInt, 1);
      row = dropBox1.AddRow(); row.SetData(fldAnimal, "Dragon"); row.SetData(fldInt, 2);
      row = dropBox1.AddRow(); row.SetData(fldAnimal, "Bee"); row.SetData(fldInt, 3);
      row = dropBox1.AddRow(); row.SetData(fldAnimal, "Chimpanzee");   row.SetData(fldInt, 4);
      row = dropBox1.AddRow(); row.SetData(fldAnimal, "Alligator"); row.SetData(fldInt, 5);
 
      dropBox1.Sort(sortField, order);
   }
}
 
Form1 form1 {};
Hope this helps!

-Jerome
nickdobrinich
Posts: 10
Joined: Thu Feb 14, 2013 9:24 am

Re: Dropbox Sort

Post by nickdobrinich »

Jerome,
Merci for your suggested code.
As you stated, dropBox1.Sort(null, 1) worked.
I found that I did need this sorting step because when I AddString()ed the out of order strings into the drop box, they were not automatically sorted.
Thx.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Dropbox Sort

Post by jerome »

Nick,

When did you call AddString() ?
If you called them in the Form constructor, you should not need it ( they should automatically be sorted in alphabetical order ). Try the first example I provided.

-Jerome
nickdobrinich
Posts: 10
Joined: Thu Feb 14, 2013 9:24 am

Re: Dropbox Sort

Post by nickdobrinich »

Ok, I used your code in the 1st example.
Should have paid more attention to the dropbox being initialized in the Form() constructor.

However, the initial dropdown list is not sorted after the AddString() calls UNTIL the button is clicked to add the newest element.
Tested on both Linux and Win XP.
This is not a big problem now that I know how to dropBox.Sort()

Your 2nd example also addressed more of my problem.
I want to present 400 elements in a sorted dropbox list of
index, cleanfilename and filepathfilenameext like this after Sort():
77 ABC /mnt/home/data1/ABC.dat
334 DEF /mnt/home/data7/DEF.dat
...
281 XYZ /mnt/home/data0/XYZ.dat

The user doesn't see the file path part.
And I can do this with a parallel array and the index from the sorted dropbox.
Thank goodness you gave me the capability to use eC code.

Oh, and I just discovered that you built in 1st key accelerators to jump around the dropbox list without scrolling.
Very nice.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Dropbox Sort

Post by jerome »

Hi nick,
However, the initial dropdown list is not sorted after the AddString() calls UNTIL the button is clicked to add the newest element.
Really? When I tested the code I pasted everything came up sorted first. Note sure if it's a misunderstanding, or a the behavior of a different version?

Note that you can also associate a user-defined data value with each row of the list box, using the Row::tag property. You can then retrieve a row by tag, or retrieve the tag of a specific row. This might be useful to you as well.

Regards,

Jerome
Post Reply