Recent

Author Topic: TListBox and TStringList and LoadFromFile Data Display Issue  (Read 1858 times)

Aruna

  • Full Member
  • ***
  • Posts: 119
TListBox and TStringList and LoadFromFile Data Display Issue
« on: November 26, 2022, 02:07:21 am »
Hello, I am trying to load a TListbox with data from a disk file, this is working. The TlistBox displays each line of data with a space or some sort of padding I am still trying to figure that one out. In the attached image on the right side you can see Form1 and the data displayed has a padding/space between each line. On the bottom left you can see in the shell/terminal how the same output is rendered and this is what I need to happen in my TListbox as well. Does someone have a fix or workaround, please?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     ListBox1: TListBox;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.   strList : TStringList;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Button1Click(Sender: TObject);
  36.   begin
  37.    // Creates the string list
  38.    strList := TStringList.Create;
  39.  
  40.    // Reads the whole file into the string list
  41.    strList.LoadFromFile('/media/aruna/linux-next/home/lsblk.ajh');
  42.  
  43.   // Load TListBox
  44.    Listbox1.Items:=strList;
  45.  
  46.    // Releases the string list memory
  47.    strList.Free;
  48. end;
  49.  
  50. end.
  51.                                        

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #1 on: November 26, 2022, 02:12:54 am »
Does someone have a fix or workaround, please?
I suggest that you do use same Font and Charset and it should do what you want.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

dseligo

  • Hero Member
  • *****
  • Posts: 1180
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #2 on: November 26, 2022, 03:00:04 am »
Set ListBox1.Font.Name to some monospace font, e.g. 'Courier New'.
In monospaced fonts all characters have the same width and that will do what you want.

P.S.: In Debian I have 'Liberation Mono' and 'Monospace' e.g.
« Last Edit: November 26, 2022, 03:06:31 am by dseligo »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #3 on: November 26, 2022, 03:18:11 am »
On Windows, Fixedsys comes most close to his terminal/bash font.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Aruna

  • Full Member
  • ***
  • Posts: 119
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #4 on: November 26, 2022, 10:12:45 pm »
Hi @KodeZwerg and @dseligo thank you both for the suggestions. I tried what you had advised but nothing changed. I started increasing the font size and then it suddenly rendered the way I wanted it to. This was using the monospace font in Debian. I have attached a screenshot. Also, I do not think it is the 'width' that is the problem it is the 'height' of the displayed item but I am unable to adjust this through the Object Inspector or through code so I am going to ask on the Lazarus mailing list and see. It works now but my concern is what if the end-user does not have a monospace font? I want this to work and display exactly the same way across all platforms Lazarus and FPC run on.

Aruna

  • Full Member
  • ***
  • Posts: 119
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #5 on: November 26, 2022, 10:15:13 pm »
I also want it to work with any font we decide to use not just Monospace and not be dependent on a given font size but work for any font size we decide to use.
« Last Edit: November 26, 2022, 10:20:07 pm by aruna.hewapathirane »

wp

  • Hero Member
  • *****
  • Posts: 11833
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #6 on: November 27, 2022, 12:47:57 pm »
Please post a sample file so that I can play with it. The solution is to use Listbox style lbOwnerDrawFixed and to paint the items yourself in the OnDrawItem event.

Aruna

  • Full Member
  • ***
  • Posts: 119
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #7 on: November 27, 2022, 03:21:52 pm »
Please post a sample file so that I can play with it. The solution is to use Listbox style lbOwnerDrawFixed and to paint the items yourself in the OnDrawItem event.

Absolutely, I will be happy to do so. Here you go, the sample code is attached below this post. I saw lbOwnerDrawFixed and lbOwnerDrawVariable but did not fully understand how to use these ( yet ) If you figure out how to run/execute a command through code that runs in a bash shell and then capture the output of that command either to memory or disk file do let me know. I saw the TProcess example on the wiki and again it's over my head.

So I need to figure out how to do this through code --> lsusb -t > lscpu.txt 

How do I do this through code, please? Oh the sample code tarball simply extract to a folder then open the project1.lpi in the IDE then run/F9 and things should work..
« Last Edit: November 27, 2022, 03:48:24 pm by Aruna »

wp

  • Hero Member
  • *****
  • Posts: 11833
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #8 on: November 27, 2022, 06:21:59 pm »
I switched the listbox to lbOwnerDrawFixed mode so that the lineheight can be controlled. It is adjusted to the font height defined in the font metric, so that the box-drawing characters should extend over the full line height. But this is valid only for a few mono-spaced fonts, all others leave a few pixels empty between the lines. On the other hand you cannot use proportional fonts anyway because you get a good column alignment of your data only with fixed-width fonts.

I added a combobox which lists all available fonts and you can scroll through them with the arrow keys - the listbox will update immediately. There is also a spinedit to select the font height.

There are lots of comments in the main unit of the project.

Have fun.

Aruna

  • Full Member
  • ***
  • Posts: 119
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #9 on: November 27, 2022, 06:43:56 pm »
I added a combobox which lists all available fonts and you can scroll through them with the arrow keys - the listbox will update immediately. There is also a spinedit to select the font height.
This is very nice. Many Thanks.

There are lots of comments in the main unit of the project.
Have fun.
Yes I saw the comments. Thank you very much for taking your valuable time to do this. I am thinking maybe I will write a set of examples using your technique that demonstrates how to access and control specific properties of a given component and this will definitely help newbies. I must talk to @JuhaManninen and @dbannon and see what they feel about this.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #10 on: November 27, 2022, 08:04:42 pm »
I also want it to work with any font we decide to use not just Monospace and not be dependent on a given font size but work for any font size we decide to use.
On Windows this would mean to measure character "X" or "0" (a character that used most space on screen for x and y resolution) and draw each character seperate inside of a control.
I have not downloaded what wp made for you, but on its own it is impossible with standard way of doing, it has to be done custom.
Only a fixed width font will ensure that the space for one character is always same whatever character you are using.
So what I try to say, your "any font" is needing love to be fully showing exact same due to size/padding differences.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Aruna

  • Full Member
  • ***
  • Posts: 119
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #11 on: November 28, 2022, 04:36:20 am »
I also want it to work with any font we decide to use not just Monospace and not be dependent on a given font size but work for any font size we decide to use.
On Windows this would mean to measure character "X" or "0" (a character that used most space on screen for x and y resolution) and draw each character seperate inside of a control.
I have not downloaded what wp made for you, but on its own it is impossible with standard way of doing, it has to be done custom.
Only a fixed width font will ensure that the space for one character is always same whatever character you are using.
So what I try to say, your "any font" is needing love to be fully showing exact same due to size/padding differences.
Well so be it then fixed width fonts we will stick with. When you have some free time please download what @wp made and click  'Hard Disk FS' then try increasing the font size and you will see when you hit font size 15 the tree lines fit perfectly. So my question is since we have properties lbOwnerDrawVariable and lbOwnerDrawFixed we should be able to make things work at smaller font sizes? I could be wrong but that is what I feel :-)

Thank you KodeZwerg.

Aruna

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #12 on: November 28, 2022, 10:27:11 am »
I do not really know what error you are getting....
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #13 on: November 28, 2022, 10:27:46 am »
Ps: Tested the "Linux only" on Windows  :P
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Aruna

  • Full Member
  • ***
  • Posts: 119
Re: TListBox and TStringList and LoadFromFile Data Display Issue
« Reply #14 on: November 28, 2022, 06:54:39 pm »
I do not really know what error you are getting....
This is very interesting in your system things seem to work without any issues. I noticed the font you used is Lucida Console so that tells me you are on a Windows machine. Maybe this is a problem with my hardware? I have an ancient Dell monitor and I am running a custom kernel that I compiled but that should not affect font rendering.

I am happy it works on your system. Now I have to figure out a fix for mine or find out why this happens. If you look at the screenshot I have attached you will see what I see. And it is not a 'error' but the font does not fill the complete item height. It only does so at a font size of 15 using Monospace in my system.

I also found I can run two instances of Lazarus IDE with zero issues. That was a pleasant surprise :-)
« Last Edit: November 28, 2022, 06:58:07 pm by Aruna »

 

TinyPortal © 2005-2018