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 additionally have an approach to store one increasingly extra string along the one showed to the client. Whats more, you should store/join something other than a plain string to the string, you should connect 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 genuine strings in the rundown. On the off chance that you need to appoint 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 strategy to add strings to the rundown, to connect an item with each string, you should utilize another methodology. The ListBox.Items.AddObject technique acknowledges two boundaries. The main 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 technique 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: /order mistake! ListBox1.Items.AddObject(zarko, gajic); will bring about an assemble blunder: 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 whole 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 article included. Since in Win32 a number possesses 4 bytes - such a hard cast is conceivable. To get back the whole number related with the string, you have to cast the item 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) inscriptions 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 conventional circumstance you would include occasions (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 items in TStrings relatives: the TStrings object doesn't claim the articles you include along these lines. Items added to the TStrings object despite everything exist regardless of whether the TStrings occurrence is decimated. They should be expressly devastated 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 conventional 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 appended 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 strategy when articles alloted to string things were made by you.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.