Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8062215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:35:34+00:00 2026-06-05T10:35:34+00:00

I’m building a custom list control, similar to a list view but lighter. It

  • 0

I’m building a custom list control, similar to a list view but lighter. It has properties ItemWidth and ItemHeight for each of its items, and the items are in a TOwnedCollection. Each item is the same size. I also have properties for Margins and ItemSpacing to specify how far apart to position each item.

The problem is when it comes to calculating each item’s position to best fit them in the current control space. The control has only vertical scrolling, and no horizontal. Therefore, I need to recognize when an item cannot fit in the list and bring it to the next line.

To make this even trickier, I have to also be able to identify if a given point is within an item’s rect area, for handling mouse events. So to solve this, I decided to put a function on each item GetRect which will return that item’s Rect area on the control. But how do I make this function calculate this?

The two main implementations of this function will be in the Paint of the control:

for X := 0 to FItems.Count - 1 do begin
  Canvas.Rectangle(FItems[X].GetRect);
end;

And when identifying if a point is in this item’s area:

for X := 0 to FItems.Count - 1 do begin
  R:= FItems[X].GetRect;
  Result := (P.X > R.Left) and (P.X < R.Right) and (P.Y > R.Top) and (P.Y < R.Bottom);
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:35:36+00:00Added an answer on June 5, 2026 at 10:35 am

    Knowing the position of any cell of a grid does not require calculating all previous cells’ locations. That’s the great thing about a grid. Each cell has a predictable location.

    To start, you need to know how many cells can be arranged horizontally in a single row. Using the values from the first answer, that’s given by this equation:

    CellsPerRow := (CW - ML - MR + SH) div (IW + SH);
    

    That takes the total client width, subtracts the margins, and divides by the effective width of a single cell, given by adding the item width with the inter-item spacing. One cell of each row doesn’t have spacing (because it abuts an edge of the control), so we pretend that the client area is actually wider by SH pixels.

    Now that we know how many items fit in a row, we can calculate which (zero-based) row any item belongs in:

    ItemRow := Item.Index div CellsPerRow;
    

    The (zero-based) position within that row (the column) is also easy to calculate:

    ItemColumn := Item.Index mod CellsPerRow;
    

    Now we can calculate the position of the cell:

    LP := ML + ItemColumn * (IW + SH);
    TP := MT + ItemRow * (IH + SV);
    

    Bringing it all together, we get this:

    function TMyListItemGrid.GetCellsPerRow: Integer;
    begin
      Result := (ClientWidth - Margins.Left - Margins.Right + SpacingHorz) div (ItemWidth + SpacingHorz);
    end;
    
    function TMyListItem.GetRect: TRect;
    var
      Row, Col: Integer;
      EffectiveWidth, EffectiveHeight: Integer;
    begin
      EffectiveWidth := Owner.ItemWidth + Owner.SpacingHorz;
      EffectiveHeight := Owner.ItemHeight + Owner.SpacingVert;
    
      Row := Index div Owner.CellsPerRow;
      Result.Top := Owner.Margins.Top + Row * EffectiveHeight;
      Result.Bottom := Result.Top + Owner.ItemHeight;
    
      Col := Index mod Owner.CellsPerRow;
      Result.Left := Owner.Margins.Left + Col * EffectiveWidth;
      Result.Right := Result.Left + Owner.ItemWidth;
    end;
    

    Be careful about letting the control get too narrow, or letting the margins get too wide. If that happens, then the CellsPerRow property could become zero, and that will cause exceptions from all the GetRect calls. Things will probably also look strange if CellsPerRow becomes negative. You’ll want to enforce a certain minimum width for your control.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.