简体   繁体   中英

How do get the text from specific line in listbox delphi

I want to get the text from a specific line.

Ex.

ListBox Contents:

Line 0 String 1

Line 1 String 2

Line 3 String 3

How do get the text from line 1 per example?

i try with this:

(listbox1.Items.Objects[1] as TText).Text

But generates a random access violation...

There is a another way?

Assuming they exist,

  • ListBox.Items[0] is the first string in the list box.
  • ListBox.Items[1] is the second string in the list box.
  • ...
  • ListBox.Items[ListBox.Items.Count - 1] is the last string in the list box.

Instead of

ListBox.Items[i]

you can also use

ListBox.Items.Strings[i]

but that's more to type.

(The TStrings.Strings property is clearly an array property, and because it is declared as" default " , it can be accessed by using the brackets on the object itself.)


Bonus material: The Objects array property you tried is used occasionally to associate an object (pointer) with each item in the list. (And sometimes it is abused to store native-size integers which aren't objects, still one per item.)

Items.Lines[0] - Access element 0 (first string) of Lines array -- for Kylix 

Items.Strings[0] - Access element 0 (first string) of Strings array -- for VCL/FMX

AV generates when you're trying to Access memory, that is not available, so, in other words, delphi have no type-checking for accessing program memory (binary data), and it results in a wide range of unhandled errors, such as wrong type error and NullPointer exception, which is, however can be handled without modifying memory routines (writing other typedefinitions to provide checking), pointer can be checked by comparing with nil in Pascal implementation or by CMP func in Assembler.
More about this:
Every variable, constant and, of course, method is stored in computer memory (RAM) during execution process, in its own field, which is supplied by the processor (CPU) to program, and limited by integrated on the motherboard logic, so it's cannot write to the other programs memory.
Most data types has its own byte-alignment (specific of the storing 0 and 1, but, when type is more than just a one byte, it's widened to contain type information) to know, that it is have correct binary data in it, such as Strings, Objects and Records.
String stores it's own length and encoding in byte representation of its content, when the class stores the actual type name, which is used to control functions and procedures calling.
But, the delphi Pascal seems to access memory right after specification of the data adress, without any checking of its content, so it will result in returning random values in case of accessing unspecified types -- types, that have no widened type info (such as different integer types) and AV in case of acessing specified types.
Just remember to check all of the Pointer types (as usual they starts from P letter, such as PAnsiChar) for containing something in it:
Ways:

// Assigned function
if Assigned(pvar) then 
DoSomething();
// = nil comparision / equation
if pvar <> nil then
DoSomething()
Else
Exit;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM