Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a TableLayoutPanel of absolute row heights that represent equal 15 minute time intervals throughout a 24 hour day. I have a time-scale object in the first column spanning all rows. When I click on the time-scale in the first column 0 it returns correctly the x/y mouse coordinates relative to the time-scale object irrespective of the area of the time-scale object visible in the area of the TableLayoutPanel that is scrolled into view, so I am correctly able to add new objects spanning various times in column 1, 2 etc.

The problem I have is how to correctly read the mouse-click position on an unfilled background row of the TableLayoutPanel, so as to determine the row? Everything I have tried works fine in a table that is not scrolled, or a table with controls in every clicked cell. As soon as the empty tablelayoutpanel has sufficient rows to require scrolling to select a time in column 0 the mouse coordinates returned by clicking on the background un-filled row/column appears to be the X/Y position relative to the VISIBLE area of the TableLayoutPanel, not the ACTUAL X/Y coordinates relative to the total TableLayoutPanel itself for some reason?

As far as I have been able to ascertain the TableLayoutPanel does not appear to have a way of finding which rows are clicked on if they are not filled with a control when the total panel is larger that the visible scrolled area, or to determine the row index of which empty rows are actually in the visible area in a scrolled scenario?

How do I determine the first visible scrolled (filled or empty) row, or that X/Y point relative to the top of the total TableLayoutPanel, so as to calculate the correct x/y mouse click coordinates relative to the top of the full table instead of just the visible scrolled area?

Any help very much appreciated?
Thanks
Rob
Posted

1 solution

For a scrollable control the ClientRectangle contains just the visible portion of the control. The DisplayRectangle is the total area, accessible by scrolling, and the AutoScrollPosition gives the offset of the top left corner of the DisplayRectangle relative to the ClientRectangle.

That's easy to say but probably better understood with a short code fragment which does the required transformation.
C#
private void Panel_MouseClick(object sender, MouseEventArgs e) {
  TableLayoutPanel tlp = (TableLayoutPanel)sender;  
  // Calculate location in the DisplayRectangle    
  Point correctedPos = Point.Subtract(e.Location, new Size(tlp.AutoScrollPosition));
  
  Debug.Print("DisplayRectangle{0} ClientRectangle{1} AutoScrollPosition{2}", 
                  tlp.DisplayRectangle, tlp.ClientRectangle, tlp.AutoScrollPosition);
  Debug.Print("MouseClick Location{0} Corrected{1}", e.Location, correctedPos);
}


I think that's what you need.

Alan.
 
Share this answer
 
Comments
Member 10599903 20-Jun-14 21:40pm    
Thanks Alan, much appreciated. I've had an issue or two with using the scroll position as I have established the resizing event of the TableLayoutPanel after adding rows ignores my manually set scroll maximum & resets it to what appears to be the new size in pixels, causing considerable confusion! However I should be able to come up with a workaround based on your example. Thanks once again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900