The following program is actually not my project, but it makes it easier to understand what I want later in my real program.
Here a .gif to see the current functionality:
What I want? I want add a label total points. Which includes the points of the checkboxes and textboxes, this should be also calculate in real time. For each group (panel)
Where is the problem? I dont know how to manage this, because I'm working with two different event handlers.
Here is source:
namespace Test1
{
public partial class Form1 : Form
{
int chkBoxX = 10;
int chkBoxY = 30;
int txtBoxX = 10;
int txtBoxY = 50;
int panelX = 0;
int panelY = 0;
public Form1()
{
InitializeComponent();
for (int k = 1; k <= 3; k++)
{
Panel panel = new Panel();
panel.Width = 550;
panel.Height = 100;
panel.Location = new Point(panelX, panelY);
panel.BackColor = Color.RoyalBlue;
panelY += 110;
this.Controls.Add(panel);
AddElements(panel);
}
}
void AddElements(Panel panel) {
Label labelChkBoxPoints = new Label();
labelChkBoxPoints.Name = "labelChkBoxPoints";
labelChkBoxPoints.Location = new Point(400, 30);
labelChkBoxPoints.AutoSize = true;
labelChkBoxPoints.BackColor = Color.White;
labelChkBoxPoints.Font = new Font(labelChkBoxPoints.Font.FontFamily, 12, FontStyle.Bold);
panel.Controls.Add(labelChkBoxPoints);
Label labelTxtBoxPoints = new Label();
labelTxtBoxPoints.Name = "labelTxtBoxPoints";
labelTxtBoxPoints.Location = new Point(400, 50);
labelTxtBoxPoints.AutoSize = true;
labelTxtBoxPoints.BackColor = Color.White;
labelTxtBoxPoints.Font = new Font(labelChkBoxPoints.Font.FontFamily, 12, FontStyle.Bold);
panel.Controls.Add(labelTxtBoxPoints);
Label labeltotalPoints = new Label();
labeltotalPoints.Location = new Point(430, 40);
labeltotalPoints.Font = new Font(labeltotalPoints.Font.FontFamily, 14, FontStyle.Bold);
// labeltotalPoints.Text = "10XXXXXXXXXXXX0";
labeltotalPoints.AutoSize = true;
labeltotalPoints.BackColor = Color.White;
// labeltotalPoints.;
panel.Controls.Add(labeltotalPoints);
for (int i = 1; i <= 5; i++)
{
CheckBox checkBox = new CheckBox();
checkBox.Name = String.Format("checkBox{0}", i);
checkBox.Text = "";
checkBox.Width = 20;
checkBox.Height = 15;
checkBox.Location = new Point(chkBoxX, chkBoxY);
chkBoxX += 26;
checkBox.CheckedChanged += checkBox_CheckedChanged;
panel.Controls.Add(checkBox);
}
for (int j = 1; j <= 5; j++)
{
TextBox tb = new TextBox();
tb.Name = String.Format("textBox{0}", j);
tb.Width = 60;
tb.Location = new Point(txtBoxX, txtBoxY);
txtBoxX += 80;
tb.TextChanged += txtBox_CheckedChanged;
panel.Controls.Add(tb);
}
chkBoxX -= (5 * 26);
txtBoxX -= (5 * 80);
}
private void txtBox_CheckedChanged(object sender, EventArgs e)
{
int total = 0; int points = 0;
foreach (object tb in ((TextBox)sender).Parent.Controls)
{
if (tb is TextBox)
{
if (!string.IsNullOrEmpty(((TextBox)tb).Text))
{
if (!int.TryParse(((TextBox)tb).Text, out points))
{
((TextBox)tb).Text = "";
}
total += points;
}
}
}
(((TextBox)sender).Parent.Controls["labelTxtBoxPoints"]).Text = Convert.ToString(total);
}
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
int counter = 0;
foreach (object cb in ((CheckBox)sender).Parent.Controls)
{
if (cb is CheckBox)
{
if (((CheckBox)cb).Checked)
{
counter++;
}
}
}
(((CheckBox)sender).Parent.Controls["labelChkBoxPoints"]).Text = Convert.ToString(counter);
}
}
}
Why don't you just calculate the sum of your "point" labels at the end of both events?
It is nice and simple(You could do it more efficiently but i don't this there is a reason... )
Just call TotalPoints() at the end of checkBox_CheckedChanged,txtBox_CheckedChanged
Update
If you only want the sum per panel all you have to do is change the TotalPoint method to search only inside your panel
And at the end of your events