CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    130

    [RESOLVED] Get selected row?

    On clicking a certain cell or row in my table panel layout, the background of that particular row should be highlighted. How do I determine which row has currently been clicked?

    Code:
             protected void CreateTblPnl(object sender, EventArgs e)
            {
                (...)
                tblPnl.Click += new EventHandler(tblPnl_Click);
            }
    
            protected void tblPnl_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < tblPnl.ColumnCount; i++)
                {
                    for (int j = 0; j < tblPnl.RowCount; j++)
                    {
                        Control control = tblPnl.GetControlFromPosition(i, j);
    
                        if (control.Focus) // Doesnt work!!                    
                       {
                           tblPnl.BackColor = ColorTranslator.FromHtml("#3A9DCB"); // Ensure only selected row is coloured!!
                        }
                    }
                }
    
                // OR: 
                //TableLayoutPanelCellPosition pos = this.tblPnl.GetCellPosition(e);
                //Point loc = this.tblPnl.PointToClient(new Point(e.X, e.Y));
                //int row = pos.Row;
                //int col = pos.Column;
            }

  2. #2
    Join Date
    Apr 2010
    Posts
    131

    Re: Get selected row?

    Add an onclick event handler to each cell pointing to a single handler method. In that method, extract the name of the firing cell and change its color:

    I'm not certain what type of control your cells are, so in this example, I'm using a TextBox. Just change the type of control to suit your purpose.

    private void genericOnClickMethod(object sender, EventArgs e){
    TextBox t = (TextBox)sender;
    ...(act on "t" to change its style)
    t.BackgroundColor, etc....

    }

  3. #3
    Join Date
    Jan 2010
    Posts
    130

    Re: Get selected row?

    That would however only change the colour of the current cell clicked. I would however like the whole row (consisting of 4 cells) to be selected so that I can perform further functions such as highlighting that whole row or deleting it.

  4. #4
    Join Date
    Jan 2010
    Posts
    130

    Re: Get selected row?

    I resolved it using a for loop to go through every cell in the table and then using a variable "rowIndex" and another "colIndex" to determine the index of the cell that had been selected. I then changed the background color of that cell as well as the rest of the cells on the row (by determining in which column the cell was located). Not the best way but it more or less works....

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured