Solved

Drag & Drop Question

Posted on 2006-07-09
7
1,446 Views
Last Modified: 2012-08-13
How to determine which column and row the mouse cursor is in the TableLayoutPanel in the DragDrop event?
0
Comment
Question by:michellechan57
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17068905
Here is an extended custom control showing one possibility:

using System;
using System.Drawing;

/// <summary>
/// Extend the TableLayoutPanel, to provide
/// functionality to determine cell bounds,
/// and to perform hit-test information (row, column)
/// based on (x,y) coordinates passed into the
/// HitTest method.
/// </summary>
public class TableLayout : System.Windows.Forms.TableLayoutPanel
{

  /// <summary>
  /// Get the bounds for a cell at (row, column)
  /// </summary>
  /// <param name="row">The row for the cell</param>
  /// <param name="column">The column for the cell</param>
  /// <returns>A rectangle showing the cell bounds</returns>
  public Rectangle GetCellBounds(int row, int column)
  {

    // Make sure that row and column are valid.
    if (row < 0 || row > this.RowCount - 1)
      throw new ArgumentOutOfRangeException("Row", "Row out of range");

    if (column < 0 || column > this.ColumnCount - 1)
      throw new ArgumentOutOfRangeException("Column", "Column out of range");

    // Calculate cell dimensions.
    int cellWidth = this.ClientRectangle.Width / this.ColumnCount;
    int cellHeight = this.ClientRectangle.Height / this.RowCount;

    // Calculate cell location.
    int x = column * cellWidth;
    int y = row * cellHeight;

    return new Rectangle(x, y, cellWidth, cellHeight);
  }

  public TableLayoutHitTest HitTest(int x, int y)
  {

    for (int row = 0; row < this.RowCount; row++)
    {
      for (int col = 0; col < this.ColumnCount; col++)
      {
        if (this.GetCellBounds(row, col).Contains(x, y))
        {
          return new TableLayoutHitTest(row, col);
        }
      }
    }
    return null;
  }
}

/// <summary>
/// Provides row, column information for where
/// the mouse is on a TableLayoutPanel.
/// The constructor takes row, column information
/// which can be accessed through the corresponding
/// properties.
/// </summary>
public class TableLayoutHitTest
{

  // Constructor accepting row and column
  public TableLayoutHitTest(int row, int column)
  {
    _row = row;
    _column = column;
  }

  int _row;
  public int Row
  {
    get { return _row; }
  }

  int _column;
    public int Column
    {
    get { return _column; }
    }
}

Bob
0
 

Author Comment

by:michellechan57
ID: 17069682
Bob, thanks.
Will try the solution and come back.
0
 

Author Comment

by:michellechan57
ID: 17077343
Bob,
1) could you explain a little more about extended custom control? specifically - how to use the code you have posted?
2) is there a simpler way to achieve the same?
Thanks
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 17078399
1) Custom controls can extend any behavior of a base control to enhance.  When you build a custom control, it should appear on the ToolBox, so that you can drag and drop to the form

2) That code is an example of how you can determine cell bounds.  If you want to combine, try something like this from the DragDrop method:

  private void TableLayoutPanel1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)

    TableLayoutPanel panel = (TableLayoutPanel) sender;

    // Calculate cell dimensions.
    int cellWidth = panel.ClientRectangle.Width / panel.ColumnCount;
    int cellHeight = panel.ClientRectangle.Height / panel.RowCount;

   for (int row = 0; row < panel.RowCount; row++)
    {
      for (int col = 0; col < panel.ColumnCount; col++)
      {

         // Calculate cell location.
         int x = column * cellWidth;
         int y = row * cellHeight;

         Rectangle bounds = new Rectangle(x, y, cellWidth, cellHeight);

        if (bound.Contains(e.X, e.Y))
        {
          // Do something here
        }
      }
    }

Bob
0
 

Author Comment

by:michellechan57
ID: 17085889
Hi Bob,

I have modified your code a little so that my source control landed close to where the mouse pointed (drop point, not exact spot yet):

                        Point newPoint = tableLayoutPanel1.PointToClient(new Point(e.X, e.Y));
                        ...
                        if (bounds.Contains(newPoint.X, newPoint.Y))
                        {
                            // Do something here
                            Label label = (Label)e.Data.GetData(typeof(Label));
                            label.Dock = DockStyle.Fill;

                            tableLayoutPanel1.Controls.Add(label, col, row);
                        }

I will increase the point to 500 and ask (sorry for being idle): my rows height and columns width are not fixed, so how  can I determine where the mouse cursor in this case?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17086007
How did you set the height and width?

Bob
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17086293
Try this:

    private Rectangle GetCellBounds(int row, int column)
    {

        // Build a sum of the row height to get the y-coordinate.
        int sumRow = 0;

        // Look at all the rows.
        for (int i = 0; i < this.tableLayoutPanel1.RowCount; i++)
        {

            // Calculate the row height based on the size type.
            RowStyle rowStyle = this.tableLayoutPanel1.RowStyles[i];
            int rowHeight = 0;

            switch (rowStyle.SizeType)
            {
                case SizeType.Absolute:
                        rowHeight = Convert.ToInt32(rowStyle.Height);
                        break;
                case SizeType.Percent:
                        rowHeight = Convert.ToInt32(this.tableLayoutPanel1.DisplayRectangle.Height * (rowStyle.Height / 100));
                        break;
                default:
                    break;
            }

            int sumColumn = 0;

            // Look at all the column for each row, since they may be different sizes.
            for (int j = 0; j < this.tableLayoutPanel1.ColumnCount; j++)
            {

                // Calculate the column width based on the size type.
                ColumnStyle columnStyle = this.tableLayoutPanel1.ColumnStyles[i];
                int columnWidth = 0;

                switch (columnStyle.SizeType)
                {
                    case SizeType.Absolute:
                        columnWidth = Convert.ToInt32(columnStyle.Width);
                        break;
                    case SizeType.Percent:
                        columnWidth = Convert.ToInt32(this.tableLayoutPanel1.DisplayRectangle.Width * (columnStyle.Width / 100));
                        break;
                }

                // Build a rectangle indicating the bounds for selected cell.
                if (row == i && column == j)
                    return new Rectangle(sumColumn, sumRow, columnWidth, rowHeight);

                // Set the x-coordinate for the cell on the next column.
                sumColumn += columnWidth;

            }

            // Set the y-coordinate for the cell on the next row.
            sumRow += rowHeight;

        }
        return Rectangle.Empty;
    }

Test code:
   Console.WriteLine("1, 1:  " + this.GetCellBounds(0, 0).ToString());
   Console.WriteLine("1, 2:  " + this.GetCellBounds(0, 1).ToString());
   Console.WriteLine("2, 1:  " + this.GetCellBounds(1, 0).ToString());
   Console.WriteLine("2, 2:  " + this.GetCellBounds(1, 1).ToString());

TableLayoutPanel configuration:
        this.tableLayoutPanel1.ColumnCount = 2;
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
        this.tableLayoutPanel1.Location = new System.Drawing.Point(68, 76);
        this.tableLayoutPanel1.Name = "tableLayoutPanel1";
        this.tableLayoutPanel1.RowCount = 2;
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
        this.tableLayoutPanel1.Size = new System.Drawing.Size(200, 100);
        this.tableLayoutPanel1.TabIndex = 1;

Bob
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question