I managed to get the cell that I am clicking on in a dynamic tablelayoutpanel that I created through code, but I would like to know if someone can help me paint that specific cell, if there is an event or a property of the table layout panel that allows it.
My code is as follows: When I click on the tablelayoutpanel:
private void TableLayoutPanelReservas_MouseClick(object? sender, MouseEventArgs e)
{
CeldaSeleccionada = obtenerFilayColumna(e.Location,pintaCelda:true);
if (CeldaSeleccionada.Count == 0)
return;
var columnProperties = tableLayoutPanelReservas.ColumnStyles.Cast<ColumnTLPReserva>().OrderBy(s => s.columName).ToList();
var rowProperties = tableLayoutPanelReservas.RowStyles.Cast<RowTLPReserva>().ToList();
var reservasLista = dtReservasAux.AsEnumerable().ToList();
var itemColumna = columnProperties.Where(s => (int)s.IndexColumna == CeldaSeleccionada["Columna"]).ToList();
var itemRow = rowProperties.Where(s => (int)s.rowIndex == CeldaSeleccionada["Fila"]).ToList();
if (itemColumna.Count == 0 || itemRow.Count == 0)
return;
if (!primerReservaSelected)
{
primerReservaSelected = true;
idPrimeraCarpaReserva = itemRow[0].propiedadId;
fechaDesdeReservaSelected = new DateTime(DateTime.Now.Year, DateTime.Now.Month, itemColumna[0].Dia);
}
else
{
segundaReservaSelected = true;
idSegundaCarpaReserva = itemRow[0].propiedadId;
fechaHastaReservaSelected = new DateTime(DateTime.Now.Year, DateTime.Now.Month, itemColumna[0].Dia);
//this.tableLayoutPanelReservas.GetControlFromPosition(itemColumna[0].IndexColumna, itemRow[0].rowIndex).BackColor = Color.Red;
if (!this.validaReserva())
{
primerReservaSelected = false;
segundaReservaSelected = false;
fechaDesdeReservaSelected = DateTime.MinValue;
fechaHastaReservaSelected = DateTime.MinValue;
return;
}
ABM.ABMReservas frm1 = new ABM.ABMReservas(carpaId: idSegundaCarpaReserva, reservaParticionId: 0, fechaDesde: fechaDesdeReservaSelected, fechaHasta: fechaHastaReservaSelected);
General.frmPopUp frmpopup = new General.frmPopUp();
frmpopup.Maximizar = false;
frmpopup.Formulario = frm1;
if (frmpopup.ShowDialog() == DialogResult.OK)
this.ComenzarCargaPanelReservasTLP();
}
if(primerReservaSelected == true && segundaReservaSelected == true)
{
primerReservaSelected = false;
segundaReservaSelected = false;
}
}
Now, I already got the cell and I did everything I wanted to do, but inside the method in which I get the cell I would like to paint that cell at that moment, or in the place that it is, but only paint the selected cell, the method that I have to get the clicked cell is the following:
private Dictionary<string,int> obtenerFilayColumna(Point puntoRecibe, bool pintaCelda = false)
{
Dictionary<string,int> DiccionarioRetorna = new Dictionary<string,int>();
int row = 0;
int verticalOffset = 0;
//var colWidths = this.tableLayoutPanelReservas.GetColumnWidths();
//var rowHeights = this.tableLayoutPanelReservas.GetRowHeights();
foreach (int h in tableLayoutPanelReservas.GetRowHeights())
{
int column = 0;
int horizontalOffset = 0;
foreach (int w in tableLayoutPanelReservas.GetColumnWidths())
{
Rectangle rectangle = new Rectangle(horizontalOffset, verticalOffset, w, h);
if (rectangle.Contains(puntoRecibe))
{
/*if (pintaCelda)
tableLayoutPanelReservas.GetControlFromPosition(column, row).BackColor = Color.Red;*/
DiccionarioRetorna.Add("Fila", row);
DiccionarioRetorna.Add("Columna", column);
return DiccionarioRetorna;
}
horizontalOffset += w;
column++;
}
verticalOffset += h;
row++;
}
return DiccionarioRetorna;
}
I wait an answer! Thank you !
The only thing I can think of that works would be to make a modified control that inherits from the table layout, overwriting the function that paints the control and inserting two properties for the selected cell:
DemoTableLayout class
Tablelayout initialization
Changes in the getRowColumn function
The only changes are inside the if to paint cell that you have commented out. Result: