Ecere SDK/eC Forums • View topic - Dropbox Sort




Ecere SDK/eC Forums

Ecere Developers Community

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.

Dropbox Sort

Postby jerome » Fri Mar 01, 2013 2:43 pm

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
jerome
Site Admin
 
Posts: 623
Joined: Sat Jan 16, 2010 11:16 pm

Re: Dropbox Sort

Postby nickdobrinich » Fri Mar 01, 2013 5:15 pm

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.
nickdobrinich
 
Posts: 10
Joined: Thu Feb 14, 2013 9:24 am

Re: Dropbox Sort

Postby jerome » Fri Mar 01, 2013 5:28 pm

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
jerome
Site Admin
 
Posts: 623
Joined: Sat Jan 16, 2010 11:16 pm

Re: Dropbox Sort

Postby nickdobrinich » Sat Mar 02, 2013 12:17 pm

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.
nickdobrinich
 
Posts: 10
Joined: Thu Feb 14, 2013 9:24 am

Re: Dropbox Sort

Postby jerome » Sat Mar 02, 2013 5:05 pm

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
jerome
Site Admin
 
Posts: 623
Joined: Sat Jan 16, 2010 11:16 pm


Return to GUI Toolkit & 2D Graphics

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

cron