AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Delphi-PRAXiS - Lounge Delphi-News aus aller Welt Classic Tips: VCL Multi Column ListBox
Thema durchsuchen
Ansicht
Themen-Optionen

Classic Tips: VCL Multi Column ListBox

Ein Thema von DP News-Robot · begonnen am 26. Mär 2019
Antwort Antwort
Benutzerbild von DP News-Robot
DP News-Robot

Registriert seit: 4. Jun 2010
15.408 Beiträge
 
#1

Classic Tips: VCL Multi Column ListBox

  Alt 26. Mär 2019, 10:50
I might be starting a new series of blog posts, going back to classic tips. This one was triggered by a Quality Portal report, which refers to an imprecise (but not fully incorrect) page of the RAD Studio Doc Wiki -- same works in C++Builder. I did a quick Google search and didn't find a quick answer, but rather many incorrect and old ones. So here it goes.

The Windows List box control (one of the primitive and classic user.dll controls) generally displays a single list of strings. They can have associated data, but that's a separate topic. Since early versions of Windows, the operating system offers some special configurations to allow multi-column list boxes. These are made available using specific styles when creating the control, and specific Windows messages (like LB_SETCOLUMNWIDTH and LB_SETTABSTOP) to pass data. The good news is that the VCL TListBox control wraps these features making them easy to use. The bad news is documentation is not that precise and even long time Delphi developers might not know how to achieve this.

The first option is building a list box with content flowing on multiple columns. The list box still have a list of strings, once at the end of the available space, rather than scrolling vertically the data is added to a new colum and possibly scrolled horizontally if it doesn't fit. For this, you use the Columns property and the VCL will make the width of each colum equal to the width of the control divided by the number of columns. This is a sample code snippet:

ListBox1.Columns := 3; for var I := 0 to 30 do ListBox1.Items.Add('hi'); If you press the button once (with the control size in the first image below) it fills the first two columns and part of the third. By pressing the button again, you get more content with a horizontal scrollbar (second image).



Another completely different approach, is to set the TabWidth property to obtain a list box in which individual lines can be displayed in columns, as long as they use tabs in their text. This is displayed in the third image above, and can be obtained with code like the following (notice #9 is the tab character):

ListBox1.TabWidth := 50; ListBox1.Items.Add('hi' + #9 + 'lo'); ListBox1.Items.Add('foo' + #9 + 'bar'); ListBox1.Items.Add('ki' + #9 + 'mo'); Now you might wonder why this isn't used more, and the reason is if*the text doesn't fit the tab, the formatting becomes*totally inconsistent -- unlike a ListView with report configuration or a grid. Still, having a set of strings you could compute the display width of each of them with the given font, and set the tab width accordingly...

That's all for this small classic tip. Let me know if you find it interesting, I can come up with many more ;-)



Weiterlesen...
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:40 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz