0

Right now I use 2 arrays one of TImage the other of TMemo to draw an image next to text data row after row inside a scroll box.

I am looking to replace the TMemo with several components on a Panel. So probably some static text a button and some labels. But the way I'm doing it now it seems as if this will become messy.

Is there a better way of going about this without writing a component or class. Should I be using some sort of multi dimensional array or can I use a record structure to declare the components and have an array of that?

Brian Holloway
  • 205
  • 4
  • 15
  • Will all the panels contain the same sub-components? – jachguate Jan 28 '13 at 01:58
  • 1
    You should write a component that is a panel that contains the other controls and the image. Delphi makes this extremely simple, and since you're always using the controls and image together this would mean you only needed to keep one array (and instantiate one class and set it's properties). – Ken White Jan 28 '13 at 02:12
  • Jach yes the panels will all be identical. Ken I'm just not so sure I want to do that at this time. The app I'm writing is much of a learning experience as it is. I will take the horrible sloppy method I'm about to do if there isn't a middle method over that at the moment. – Brian Holloway Jan 28 '13 at 02:17
  • If I do it as a class/component are there any complications in naming/addressing? I'm at work at the moment thinking it over. It doesn't seem like it would be too hard and if I can do it its only benefitial to do so. – Brian Holloway Jan 28 '13 at 02:48
  • 2
    None. :-) Other than needing to reference the image as `MyPanel.Image` instead of `Image1`, which shouldn't be an issue. Just remember to create all of the sub-components of the panel in the panel's constructor, and to make the panel the `Owner` (and the `Parent` of any visual components it contains). It really isn't difficult; that's one of the strongest features of Delphi. – Ken White Jan 28 '13 at 03:24
  • Alright and I found this q and a searching from my phone which seems to cover that sort of thinghttp://stackoverflow.com/questions/9479872/component-creation-joining-components-together – Brian Holloway Jan 28 '13 at 03:41

1 Answers1

8

A record would at least relieve you of the burden of managing lots of parallel arrays, but you can do better than a record, and better than an array.

First, you can design a frame to represent one "row" of your form. Give it an image, button, label, and whatever else you need. Then create an instance of that frame class each time you need one. It will create the components for you automatically. You said you didn't want a custom component, and that's essentially what a frame is, but by getting to design it visually like you would a form, much of the burden of creating a composite control is lifted, so you just get the benefit of a group of related controls that can interact with each other as a unit.

Instead of an array, you might find better success with a different collection object, especially TComponentList which can grow and shrink more easily than an array, and also helps manage ownership of its contents.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • The nice part of your suggestion is that Brian can make the frame at design time, which makes it easy to build and maintain. +1 (You can that do it with any combinations of components, of course, but TFrame seems especially suited for that) – Jan Doggen Jan 28 '13 at 09:30
  • Thanks for the suggestion Rob. Not 100% sure I am going to try it this go around but I will certainly keep it in mind in the future. Nice to see you and other pro's still around helping people at every level. Even noobs like me. – Brian Holloway Jan 28 '13 at 18:03