C
C#2mo ago
Arata

trying to fix a bug where i cant get the click event on a buttom

hello, i have an issue with a windows form program, i have a panel where buttons gonna be placed, and i have a TableLayoutPanel that component gonna be the layout to the buttons "styles" i mean
buttonLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 70F)); // 70% for the image
buttonLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 30F)); // 30% for the text
buttonLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 70F)); // 70% for the image
buttonLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 30F)); // 30% for the text
only that, and the problem is the next
buttonLayout.Controls.Add(icono, 0, 0); // image in the fisrt row
buttonLayout.Controls.Add(texto, 0, 1); // text in the second row

// add TableLayoutPanel to the buttom
btn.Controls.Add(buttonLayout);

// add buttom to the main TableLayoutPanel
tableLayoutPanel.Controls.Add(btn, colIndex, rowIndex);
buttonLayout.Controls.Add(icono, 0, 0); // image in the fisrt row
buttonLayout.Controls.Add(texto, 0, 1); // text in the second row

// add TableLayoutPanel to the buttom
btn.Controls.Add(buttonLayout);

// add buttom to the main TableLayoutPanel
tableLayoutPanel.Controls.Add(btn, colIndex, rowIndex);
the button stay behind the "style panel" and it goes untouchable, i tried a couple of things but i cant reach the buttom click event
50 Replies
TizzyT
TizzyT2mo ago
you can subscribe to the event before adding to the layout panel?
Arata
Arata2mo ago
i dont catch u, my main languaje is spanish, but can u explain pls
TizzyT
TizzyT2mo ago
i mean you can access the buttons OnClick event before it goes "untouchable". So add your event handle logic for that event first. Then add the button to the layoutpanel
buttonLayout.Controls.Add(icono, 0, 0); // image in the fisrt row
buttonLayout.Controls.Add(texto, 0, 1); // text in the second row

// add TableLayoutPanel to the buttom
btn.Controls.Add(buttonLayout);

// add the handle here
btn.OnClick += SomeHandle();

// add buttom to the main TableLayoutPanel
tableLayoutPanel.Controls.Add(btn, colIndex, rowIndex);
buttonLayout.Controls.Add(icono, 0, 0); // image in the fisrt row
buttonLayout.Controls.Add(texto, 0, 1); // text in the second row

// add TableLayoutPanel to the buttom
btn.Controls.Add(buttonLayout);

// add the handle here
btn.OnClick += SomeHandle();

// add buttom to the main TableLayoutPanel
tableLayoutPanel.Controls.Add(btn, colIndex, rowIndex);
Arata
Arata2mo ago
lemme try it didnt work
TizzyT
TizzyT2mo ago
Whats the issue? error?
Arata
Arata2mo ago
no lemme share some pics
TizzyT
TizzyT2mo ago
sure
Arata
Arata2mo ago
foreach (Servicio servicio in servicios)
{


// Crear un botón sin texto (el texto estará en el TableLayoutPanel)
Button btn = new Button
{
//#00b8e6
Tag = servicio.id,
BackColor = ColorTranslator.FromHtml("#000000"),
FlatStyle = FlatStyle.Flat,
FlatAppearance = { BorderSize = 0 },
Font = new Font("Segoe UI", 30),
Size = new Size(botonSizeWidth, botonSizeHeight),
Margin = new Padding(10),
Anchor = AnchorStyles.None, // Centra el botón
};


btn.Click += Btn_Click;

RedondearEsquinas(btn, 30);

// Crear TableLayoutPanel interno para organizar imagen y texto
TableLayoutPanel buttonLayout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
RowCount = 2,
ColumnCount = 1,
};
buttonLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 70F)); // 70% para la imagen
buttonLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 30F)); // 30% para el texto
buttonLayout.BackColor = Color.IndianRed;

// Crear PictureBox para la imagen
PictureBox icono = new PictureBox
{
ImageLocation = api + servicio.icono, // Cargar la imagen desde el servicio
Size = new Size(150, 150), // Tamaño fijo de la imagen
SizeMode = PictureBoxSizeMode.Zoom,
Dock = DockStyle.Fill,
Margin = new Padding(0, 10, 0, 0) // Agregar margen superior
};

// Crear Label para el texto
Label texto = new Label
{
Text = servicio.servicios,
TextAlign = ContentAlignment.MiddleCenter,
Dock = DockStyle.Fill,
ForeColor = Color.White,
Font = new Font("Segoe UI", 18, FontStyle.Bold), // Texto más grande
BackColor = Color.Transparent
};

// Agregar PictureBox y Label al TableLayoutPanel interno
buttonLayout.Controls.Add(icono, 0, 0); // Imagen en la primera fila
buttonLayout.Controls.Add(texto, 0, 1); // Texto en la segunda fila


btn.Controls.Add(buttonLayout);

// Añadir botón al TableLayoutPanel principal
tableLayoutPanel.Controls.Add(btn, colIndex, rowIndex);

// Distribuir filas y columnas
colIndex++;
if (colIndex >= numColumns)
{
colIndex = 0;
rowIndex++;
}
}
foreach (Servicio servicio in servicios)
{


// Crear un botón sin texto (el texto estará en el TableLayoutPanel)
Button btn = new Button
{
//#00b8e6
Tag = servicio.id,
BackColor = ColorTranslator.FromHtml("#000000"),
FlatStyle = FlatStyle.Flat,
FlatAppearance = { BorderSize = 0 },
Font = new Font("Segoe UI", 30),
Size = new Size(botonSizeWidth, botonSizeHeight),
Margin = new Padding(10),
Anchor = AnchorStyles.None, // Centra el botón
};


btn.Click += Btn_Click;

RedondearEsquinas(btn, 30);

// Crear TableLayoutPanel interno para organizar imagen y texto
TableLayoutPanel buttonLayout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
RowCount = 2,
ColumnCount = 1,
};
buttonLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 70F)); // 70% para la imagen
buttonLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 30F)); // 30% para el texto
buttonLayout.BackColor = Color.IndianRed;

// Crear PictureBox para la imagen
PictureBox icono = new PictureBox
{
ImageLocation = api + servicio.icono, // Cargar la imagen desde el servicio
Size = new Size(150, 150), // Tamaño fijo de la imagen
SizeMode = PictureBoxSizeMode.Zoom,
Dock = DockStyle.Fill,
Margin = new Padding(0, 10, 0, 0) // Agregar margen superior
};

// Crear Label para el texto
Label texto = new Label
{
Text = servicio.servicios,
TextAlign = ContentAlignment.MiddleCenter,
Dock = DockStyle.Fill,
ForeColor = Color.White,
Font = new Font("Segoe UI", 18, FontStyle.Bold), // Texto más grande
BackColor = Color.Transparent
};

// Agregar PictureBox y Label al TableLayoutPanel interno
buttonLayout.Controls.Add(icono, 0, 0); // Imagen en la primera fila
buttonLayout.Controls.Add(texto, 0, 1); // Texto en la segunda fila


btn.Controls.Add(buttonLayout);

// Añadir botón al TableLayoutPanel principal
tableLayoutPanel.Controls.Add(btn, colIndex, rowIndex);

// Distribuir filas y columnas
colIndex++;
if (colIndex >= numColumns)
{
colIndex = 0;
rowIndex++;
}
}
this it the code sorry for the spanish comments lol
TizzyT
TizzyT2mo ago
lol no problem
Arata
Arata2mo ago
No description
Arata
Arata2mo ago
the buttomlayout is red so the buttom is behind now if i comment the next line
//btn.Controls.Add(buttonLayout);
//btn.Controls.Add(buttonLayout);
Arata
Arata2mo ago
it goes black
No description
TizzyT
TizzyT2mo ago
OH
Arata
Arata2mo ago
meaning the buttom can be reached by the mause so i need the layout but when i put it in i cant click the buttom haha
TizzyT
TizzyT2mo ago
how big is "buttonLayout" it also has a Click event... I think you are clicking that instead. So actually may want to handle that instead
Arata
Arata2mo ago
yeah i tried it but i didnt work
TizzyT
TizzyT2mo ago
O.o
Arata
Arata2mo ago
kinda crazy
TizzyT
TizzyT2mo ago
let me read the code again lol
Arata
Arata2mo ago
oke
TizzyT
TizzyT2mo ago
so let me get this right. There is a LayoutPanel, and inside that layout panel there are 6 Buttons. In each button there is a LayoutPanel. In that LayoutPanel there is an Image and a Label. You want it so that if you click anywhere in the red it will raise the Button's Click event?
Arata
Arata2mo ago
more or less panelContenedor have the 6 buttoms and exactly u wrotte every button has a panel with an image and a text and yes and want to click and do whaterever
TizzyT
TizzyT2mo ago
okay, you need to handle all controls inside of the button. This includes the image, label, layoutpanel, etc with the same event handler. I personally would make a custom control
Arata
Arata2mo ago
u mean instead to use a panel and a button only a buttom?
TizzyT
TizzyT2mo ago
I mean, if you really must have all the controls intact you must handle all their click events let me code something up
Arata
Arata2mo ago
oke
TizzyT
TizzyT2mo ago
public void RedButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Click en el botón");
}
public void RedButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Click en el botón");
}
Arata
Arata2mo ago
reading ok lemme try it
TizzyT
TizzyT2mo ago
you can also make a recursive method to go through all controls (children) and subscribe to their events, to make things easier
Arata
Arata2mo ago
i think it didnt work
TizzyT
TizzyT2mo ago
nothing at all?
Arata
Arata2mo ago
oh i see i think it works
TizzyT
TizzyT2mo ago
lol, which is it?
Arata
Arata2mo ago
k i need to change some things waitme a moment
TizzyT
TizzyT2mo ago
sure
Arata
Arata2mo ago
LMAO IT WORKS
TizzyT
TizzyT2mo ago
I very much would use the OnPaint event to just draw the image and text on the button
Arata
Arata2mo ago
k watch ]
TizzyT
TizzyT2mo ago
or place a generated image on the button
Arata
Arata2mo ago
before, on the onclick event i was getting the button information so now the other controlls calls the same fuct too so i fixed and it works
TizzyT
TizzyT2mo ago
good job
Arata
Arata2mo ago
ur the best ❤️
TizzyT
TizzyT2mo ago
aw shucks
Arata
Arata2mo ago
3 hours wasted on that thing hahaha
TizzyT
TizzyT2mo ago
Oh damn
Arata
Arata2mo ago
:kiss: ty
TizzyT
TizzyT2mo ago
you're welcome and good luck
Arata
Arata2mo ago
diner time finally
TizzyT
TizzyT2mo ago
FOOD
Want results from more Discord servers?
Add your server