Tuesday, August 25, 2020

Store a String Along With a String in Delphis ListBox

Store a String Along With a String in Delphis ListBox Delphis TListBox and TComboBox show a rundown of things - strings in a selectable rundown. TListBox shows a scrollable rundown, the TComboBox shows a drop-down rundown. A typical property to all the above controls is the Items property. Things characterize a rundown of strings that will show up in the control to the client. At configuration time, when you double tap the Items property, the String List Editor lets you determine string things. The Items property is really a TStrings type relative. Two Strings Per Item in a ListBox? There are circumstances when you need to show a rundown of strings to the client, for instance in the rundown box control, yet in addition have an approach to store one progressively extra string along the one showed to the client. Whats more, you should store/join something beyond a plain string to the string, you should append an article to the thing (string). ListBox.Items - TStrings Knows Objects! Give the TStrings object one more look in the Help framework. Theres the Objects property which speaks to a lot of articles that are related with every one of the strings in the Strings property - where the Strings property references the real strings in the rundown. On the off chance that you need to dole out a subsequent string (or an article) to each string in the rundown box, you have to populate the Items property at run-time. While you can utilize the ListBox.Items.Add technique to add strings to the rundown, to connect an article with each string, you should utilize another methodology. The ListBox.Items.AddObject strategy acknowledges two boundaries. The primary boundary, Item is the content of the thing. The subsequent boundary, AObject is the article related with the thing. Note that rundown box uncovered the AddItem strategy which does likewise as Items.AddObject. Two Strings for One String Since both Items.AddObject and AddItem acknowledge a variable of type TObject for their subsequent boundary, a line like: /assemble blunder! ListBox1.Items.AddObject(zarko, gajic); will bring about an aggregate mistake: E2010 Incompatible sorts: TObject and string. You can't just flexibly a string for the item since in Delphi for Win32 string esteems are not objects. To allot a second string to the rundown box thing, you have to change a string variable into an article - you need a custom TString object. An Integer for a String On the off chance that the second worth you have to store alongside the string thing is a number worth, you really needn't bother with a custom TInteger class. ListBox1.AddItem(Zarko Gajic, TObject(1973)) ; The line above stores the whole number 1973 alongside the additional Zarko Gajic string. A direct pigeonhole from a number to an article is made previously. The AObject boundary is really the 4-byte pointer (address) of the item included. Since in Win32 a whole number possesses 4 bytes - such a hard cast is conceivable. To get back the number related with the string, you have to cast the article back to the whole number worth: /year 1973 year : Integer(ListBox1.Items.Objects[ListBox1.Items.IndexOf(Zarko Gajic)]) ; A Delphi Control for a String Why stop here? Doling out strings and whole numbers to a string in a rundown box is, as you simply encountered, a bit of cake. Since Delphi controls are really protests, you can append a control to each string showed in the rundown box. The accompanying code adds to the ListBox1 (list box) subtitles of all the TButton controls on a structure (place this in the structures OnCreate occasion handler) alongside the reference to each fasten. var  â idx : whole number; start  â for idx : 0 to - 1 ComponentCount do  â begin  â â â if Components[idx] is TButton then ListBox1.AddObject(TButton(Components[idx]).Caption, Components[idx]) ;  â end; end; To automatically tap the subsequent catch, you can utilize the following articulation: TButton(ListBox1.Items.Objects[1]).Click; I Want to Assign My Custom Objects to the String Item In a progressively nonexclusive circumstance you would include cases (objects) of your own custom classes: type   TStudent class  â private     fName: string;     fYear: whole number;  â public  â â â property Name : string read fName;  â â â property Year : whole number read fYear;  â â â constructor Create(const name : string; const year : whole number) ;  â end; ........ constructor TStudent.Create(const name : string; const year : whole number) ; start   fName : name;   fYear : year; end; start  â //include two string/objects - understudies to the rundown   ListBox1.AddItem(John, TStudent.Create(John, 1970)) ;   ListBox1.AddItem(Jack, TStudent.Create(Jack, 1982)) ;  â //get the main understudy - John  â student : ListBox1.Items.Objects[0] as TStudent;  â //show Johns year   ShowMessage(IntToStr(student.Year)) ; end; What You Create You Must Free Heres what the Help needs to state about articles in TStrings relatives: the TStrings object doesn't possess the items you include along these lines. Items added to the TStrings object despite everything exist regardless of whether the TStrings case is demolished. They should be unequivocally pulverized by the application. At the point when you add items to strings - objects that you make - you should ensure you free the memory involved, or youll have a memory spill A nonexclusive custom system FreeObjects acknowledges a variable of type TStrings as its lone boundary. FreeObjects will free any articles related with a thing in the string list In the above model, understudies (TStudent class) are connected to a string in a rundown box, when the application is going to be shut (fundamental structure OnDestroy occasion, for instance), you have to free the memory involved: FreeObjects(ListBox1.Items) ; Note: You possibly call this system when articles doled out to string things were made by you.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.