TValueListEditor - A Cell is cleared after sorting lazarus / 31-LCL ---------------------------------------------------------------------- egsuh · 2018-08-17 08:36 I'd like to sort rows of TValueListEditor based on the "key" value. But as the keys are strings,  sorting numbers would result like   1,11,2,3  instead of 1,2,3,11.   So I added a little trick. Anyway my point is that, once sorted, Keys or Values column of the first row is blanked if the cursor is somewhere within the TValueListEditor.   So I added another trick, which moves focus to other control in the form first. The following codes work fine. But deleting a cell is rather strange. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    if vlEditor.RowCount >= 3 then   begin      edCodeLabel.SetFocus;  // If this is omitted, first key or value column is erased depending on                                            // the last position of cursor.       for ti := 1 to vlEditor.RowCount-1 do vlEditor.Keys[ti] := Format('%5s', [trim(vlEditor.keys[ti])]);      vlEditor.Strings.Sort;      for ti := 1 to vlEditor.RowCount-1 do vlEditor.Keys[ti] := trim(vlEditor.keys[ti]);   end; ---------------------------------------------------------------------- Bart · 2018-08-17 10:10 Does this also happen if you enclose this in BeginUpdate /EndUpdate? You can also disable editing (so the editor is nof visible anymore): Options := Options-[goEdit] What Lazarus version? What OS? What widgetset? Bart ---------------------------------------------------------------------- wp · 2018-08-17 10:11 The attached demo shows that sorting does not seem to be implemented for TValueListEditor at all. The left part of the form shows a TValueListEditor, the right part a standard TStringGrid, both with the same dummy entries. Clicking on the "Sort" button does have an effect on the TStringGrid, but nothing changes for the TValueListEditor. Looking at this issue in more detail reveals that TValueListEditor and TStringGrid store the strings differently, but TValueListEditor does not implement its own Sort method (its Strings do implement a CustomSort but this is never called). So, I think the following patch fixes the issue. Go to folder "lcl" of your Lazarus installation and make a backup copy of the file "valedit.pas"Load this file into Lazarus and make these modificationsIn the declaration part of TValueListEditor add a protected method "Sort":  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type    TValueListEditor = class(TCustomStringGrid)    ...    protected     ...     procedure Sort(ColSorting: Boolean; index,IndxFrom,IndxTo:Integer); override;     ...    end;And in the implementation part of the unit add this code:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TValueListEditor.Sort(ColSorting: Boolean;  index,IndxFrom,IndxTo:Integer);begin  if not ColSorting then    raise Exception.Create('TValueListEditor: Only sorting by columns allowed.');  Strings.Sort;end; With this modification the TValueListEditor short my demo correctly. Please test. If it works correctly I can apply the changes to the Lazarus sources. ---------------------------------------------------------------------- Bart · 2018-08-17 10:20 Delphi does not implement Sort vor TValueListEditor AFAICS. The problem lies in the fact that manipulating entries by code is easier (and less accident prone) if you manipulate the internals strings (TStrings). The grid is just a fancy display for the internal strings... Overriding Sort is OK with me. But current example code should not mess up like it does. Bart ---------------------------------------------------------------------- wp · 2018-08-17 10:32 Sorry I don't understand... To me the problem was that I called SortColRow which is a public method inherited from TCustomDrawGrid, and there is nothing forbidden in doing so. But this method does not seem to work for TValueListEditor, there were even cases in the input data crashing the program. Therefore, this must be fixed, and overriding TValueListEditor.Sort fixes it although my solution ignores the index parameters passed. maybe there are better solutions... AFICS, Delphi does not implement direct sorting in none of its grids. Therefore, the issue does not show up here. ---------------------------------------------------------------------- Bart · 2018-08-17 12:33 TS sorts the ValueListEditor using vlEditor.Strings.Sort; This is not the same as TStringGrid.Sort. The underlying data in a TValueListEditor are kept in a TStrings descendant. All data manipulation is internally handled as manipulating these TStrings. The grid is just a visual front-end. It's an ugly design IMO. However, directly manipulating the Strings property should not lead to visual anomalies, nor should it delete a cells content in this case. Q: is the content really deleted, or does it just no show anymore? I'm unsure how this can be fixed. Your suggestion to implement Sort/CustomSort is good though, but it won't solve th eproblem TS described. (The errormessage should of course be a resourcestring) Feel free to implement that (otherwise open a bugreport an assign it to me). There may be more methods of TCustomDrawGrid that may screw up a TValueListEditor  I tried to override the ones I found in the past. Bart ---------------------------------------------------------------------- wp · 2018-08-17 14:28 @Bart: Thanks for the explanation. As usual I did not read the first post very carefully  and did not notice the ValEdit.Strings.Sort - otherwise I propably would not have tried SortColRow... I one found one more inconsistency: TValueListEditor.InsertRowWithValues adds a new row after, TStringGrid.InsertRowWithValues before the specified row index. See attached modified demo, Button "Insert row with values". Initially I thought, maybe ValueListEditor has Row=0 below the fixed row because the title row is handled differently, but in all other cases that i checked the indexing seems to be consistent between ValueListEditor and StringGrid. OK, I can post the SortColRow patch to BugTracker for you to take care of it. Thank you. @egsuh: I tried to follow your method of sorting the Strings of the TValueListEditor and don't see any blank rows (Button "Strings.Sort" below the ValueListEditor at the left). Could you please post a compilable project which shows the issue? ---------------------------------------------------------------------- Bart · 2018-08-17 20:57 Delphi 7 has InsertRow(Key: string; Value: String; Append: Boolean); The 3rd parameter controls if insert is before or after the current row. @wp: thanks for the patch in the bugtracker. I'll take a look when I have time. Bart ---------------------------------------------------------------------- Bart · 2018-08-17 21:30 Hmm, I cannot seem to reproduce the original problem of TS. Strings.Sort sorts using Key column. A sort using Value column might also come in handy? Bart ---------------------------------------------------------------------- wp · 2018-08-17 21:47 Quote from: Bart on August 17, 2018, 11:30:01 pmHmm, I cannot seem to reproduce the original problem of TS.Like me Quote from: Bart on August 17, 2018, 11:30:01 pmStrings.Sort sorts using Key column. A sort using Value column might also come in handy?That's what a TValueListEditor.SortColRow could be good for. But now it strikes my mind that the flexibility to sort alongs rows and columns will always be confusing - sorting of the valuelisteditor can only be by colums. Maybe my entire patch is nonsense: Why not just override SortColRow in TValueListEditor and raise an exception with a proper error message to prevent the coder from using it? ---------------------------------------------------------------------- Bart · 2018-08-17 22:00 Let's continue discussing in the bugtracker. Sorting TStrings on the Value pair might be a challenge though? Bart ---------------------------------------------------------------------- egsuh · 2018-08-20 03:42 @Bart, Now it does not make blank cell, but the content of a cell is not changed after sorting. I added a new form (Form2)  to your project, and added a button at the right-lower corner.   1) Goto Form2,   2) set any one cell of the valuelisteditor FOCUSED,   3) then press "sort" tool button. In your form (Form1), the focus is automatically moved to the button, so there are no problem in cell values. ---------------------------------------------------------------------- wp · 2018-08-20 07:15 Your project is not very helpful because it depends on non-common components (TCFrameEditor). Please modify it such that it only uses components of the standard Lazarus installation. ---------------------------------------------------------------------- egsuh · 2018-08-20 09:50 @wp It's nothing but a frame containing TValueListEditor. The difference is only that I use TToolButton instead of TButton. Why should it make any difference in action? ---------------------------------------------------------------------- Bart · 2018-08-20 10:03 Please test latest patch in bugtracker issue (valedit.sort.3.diff) and if it does not work for you then also please report in the bugtracker. I will not discuss this further in the forum. The bugtracker is now the place where this is handled. Bart ---------------------------------------------------------------------- egsuh · 2018-08-20 10:07 I modified Form1 you made. I just added a popup menu, and it is linked to ValueListEditor1. You may click any cell of the ValueListEditor1, then right-click mouse, then press sort. You'll see what I'm talking about. Precisely speaking, the Sort does not change the content of cell that is focused, when the "sorting" is carried out with TValueListEditor focused. ---------------------------------------------------------------------- Bart · 2018-08-20 11:21 Did you test the patch as I suggested? It is useless to continue stating that you see a bug. We know there is a bug. We are trying to fix it. Bart ---------------------------------------------------------------------- wp · 2018-08-20 11:23 Quote from: egsuh on August 20, 2018, 11:50:02 amIt's nothing but a frame containing TValueListEditor. I don't have this unit. I am not going to build something on my own. It is too often that the code is not where the poster expects it to be. Or I do something differently from you and this bypasses the bug. So, please, post the project with all files required to compile it. @Bart: I am insisting on this demo project because the OP may see a different bug in addition to the one you are fixing. ---------------------------------------------------------------------- Bart · 2018-08-20 21:29 I attached a sample project to the bugtracker. You need to apply the valedit.sort.5.diff patch. Run the sample project. Click on "PickList". Click on the row that has "MyPlainRO" (column does not matter). Click on "Sort" No cells content disappears, but there will be a visual anomaly "MyPlainRO" row now. This anomaly is not there if you uncheck "Always Show Editor". Bart ---------------------------------------------------------------------- egsuh · 2018-08-21 02:59 I applied valedit.sort.6.diff, but the problem still exists --- the FOCUSED cell content is not changed after sorting.  My application has no problem once I move away the focus from valuelisteditor before the sorting. BTW,  please add some documentation on TValueLsitEditor.InsertRow method^^. I cannot find it anywhere. ---------------------------------------------------------------------- Bart · 2018-08-21 07:43 Please report that in the bugtracker issue. Bart ---------------------------------------------------------------------- Bart · 2018-08-24 16:12 Fixed in r58773. From now on (trunk) use TValueListEditor.Sort, not Strings.Sort. You can use OnCompareCells to determine how the sort is done (so no need to alter all Keys before and after the sort in your example). Sort defaults to sorting based on Key column, use Sort(colValue) if you want to sort on the Value's column. Sort will hide the editor and restore it if needed afterwards. This should prevent ugly visual anomalies and your initial issue. Since this is new functionality I think this will not be merged into the 1.8 fixes branch. Bart