Checking that whether a user control in present in the specific cell on mouse click in c# [closed]

StackOverflow https://stackoverflow.com/questions/20948142

Pregunta

How can I check that whether a usercontrol exists in the cell where the user has clicked. I have created a user control Rack.cs , just want to know that how can I check that the Rack() is present in that location or not?
If yes then do something

    private void tableLayoutPanel1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            click++;
            var pt = new Point(e.X, e.Y);


            var colWidths = this.tableLayoutPanel1.GetColumnWidths();
            var rowHeights = this.tableLayoutPanel1.GetRowHeights();

            //tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 150));
            //tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 150F));

            int col = -1, row = -1;
            int offset = 0;
            for (int iCol = 0; iCol < this.tableLayoutPanel1.ColumnCount; ++iCol)
            {
                if (pt.X >= offset && pt.X <= (offset + colWidths[iCol]))
                {
                    col = iCol;
                    break;
                }

                offset += colWidths[iCol];
            }

            offset = 0;
            for (int iRow = 0; iRow < this.tableLayoutPanel1.RowCount; ++iRow)
            {
                if (pt.Y >= offset && pt.Y <= (offset + rowHeights[iRow]))
                {
                    row = iRow;
                    break;
                }

                offset += rowHeights[iRow];
            }



            tableLayoutPanel1.Controls.Add(racking[click], col, row);
            racking[click].setposition(row, col);
            racking[click].SetChannel(click.ToString());
            tableLayoutPanel1.ColumnStyles[col].SizeType = SizeType.AutoSize;
            Adapter.insertposition(RackID, row, col, click);


        }
        else if (e.Button == MouseButtons.Right)
        {
            int[] colWidths = tableLayoutPanel1.GetColumnWidths();
            int[] rowHeights = tableLayoutPanel1.GetRowHeights();
            int top = tableLayoutPanel1.Parent.PointToScreen(tableLayoutPanel1.Location).Y;
            for (int y = 0; y < rowHeights.Length; ++y)
            {
                int left = tableLayoutPanel1.Parent.PointToScreen(tableLayoutPanel1.Location).X;
                for (int x = 0; x < colWidths.Length; ++x)
                {
                    if (new Rectangle(left, top, colWidths[x], rowHeights[y])
                                      .Contains(MousePosition))
                    {
                        Control c = tableLayoutPanel1.GetControlFromPosition(x, y);
                        if (c != null)
                        {
                            MessageBox.Show("Yes");
                        }
                    }
                    left += colWidths[x];
                }
                top += rowHeights[y];
            }
        }

        }

Now I want to check my Rack control instead of the Rectangle control that where it is present or not? And my Rack control is a mixture of text boxes and button

¿Fue útil?

Solución

After seeing some code it is still not entirely clear what you are doing (for example, what is racking?), but this should help anyway...

TableLayoutPanel has a method called GetControlFromPosition which will get the control within a given cell (column and row), so firstly you can get that control like so:

var myCellControl = tableLayoutPanel1.GetControlFromPosition(col, row);

What you do next will depend on how you are adding your controls. If you are directly adding a Rack control to each cell, then you can just test it like so:

if(myCellControl is Rack)
{
    //is Rack control, so do someting
}

otherwise, if the Rack control is nested within a container control (e.g. a Panel), then you should loop the children controls and test for a Rack control:

bool hasRack = false;
foreach(Control child in myCellControl.Controls)
{
    if(child is Rack)
    {
        //Rack control found
        hasRack = true;
        break;
    }
}

Hope that helps

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top