Hi Nick,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.
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 {};
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 {};
-Jerome