C
C#4mo ago
Themba

Hello everyone,

I'm working on a Windows Forms application and I'm having trouble with my CheckBox controls. I have three GroupBoxes, and I want the CheckBoxes inside the third GroupBox to display a MessageBox when clicked. However, the MessageBox only appears after I click the RadioButtons in the other two GroupBoxes first. I've attached my code to a text file for reference. Could someone help me figure out how to make the MessageBox appear immediately when I click the CheckBoxes inside the third GroupBox? Thank you!
35 Replies
Pobiega
Pobiega4mo ago
Please share your code at $paste so we can actually see all of it
MODiX
MODiX4mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Themba
Themba4mo ago
BlazeBin - msxdoavemmky
A tool for sharing your source code with the world!
Pobiega
Pobiega4mo ago
and the problem is that currently your messagebox does not show when you check a checkbox in the third group?
Themba
Themba4mo ago
yes
Pobiega
Pobiega4mo ago
ok so lets look at your code
// Buttons in third group
private void groupBox_CheckBoxes_CheckedChanged(object sender, EventArgs e)
{
// Loop through GroupBoxes in groupBox
// CheckBox c in groupBox_CheckBoxes.Controls.OfType<CheckBox>()

// Access checkBox Controls in groupBox
foreach (CheckBox cb in groupBox_CheckBoxes.Controls)
{
// Check and see if cb is a CheckBox
if (cb is CheckBox checkBox)
{
// Check if box is clicked
if (checkBox.Checked)
{
MessageBox.Show("Hello World");
}
}
}
Update_Total_Amount_Due();
}
// Buttons in third group
private void groupBox_CheckBoxes_CheckedChanged(object sender, EventArgs e)
{
// Loop through GroupBoxes in groupBox
// CheckBox c in groupBox_CheckBoxes.Controls.OfType<CheckBox>()

// Access checkBox Controls in groupBox
foreach (CheckBox cb in groupBox_CheckBoxes.Controls)
{
// Check and see if cb is a CheckBox
if (cb is CheckBox checkBox)
{
// Check if box is clicked
if (checkBox.Checked)
{
MessageBox.Show("Hello World");
}
}
}
Update_Total_Amount_Due();
}
immediately I see a big red flag foreach (CheckBox cb in groupBox_CheckBoxes.Controls) might seem like a good thing, but its not Controls is not a list of checkboxes, its a list of controls
Themba
Themba4mo ago
I think I understand so what should I use to gain access to my checkBoxes?
Pobiega
Pobiega4mo ago
foreach (var item in groupBox1.Controls) then you can check the types some sample code:
var checkedBoxes = 0;
foreach (var item in groupBox1.Controls)
{
if(item is not CheckBox cb)
{
continue;
}

if (cb.Checked)
{
checkedBoxes++;
}
}

MessageBox.Show($"{checkedBoxes} boxes are checked.");
var checkedBoxes = 0;
foreach (var item in groupBox1.Controls)
{
if(item is not CheckBox cb)
{
continue;
}

if (cb.Checked)
{
checkedBoxes++;
}
}

MessageBox.Show($"{checkedBoxes} boxes are checked.");
alternatively you can use OfType as such
var checkedBoxes = 0;
foreach (var item in groupBox1.Controls.OfType<CheckBox>())
{
if (item.Checked)
{
checkedBoxes++;
}
}

MessageBox.Show($"{checkedBoxes} boxes are checked.");
var checkedBoxes = 0;
foreach (var item in groupBox1.Controls.OfType<CheckBox>())
{
if (item.Checked)
{
checkedBoxes++;
}
}

MessageBox.Show($"{checkedBoxes} boxes are checked.");
item here is then guaranteed to be a CheckBox
Themba
Themba4mo ago
Issue: It works for the first time but not after. After the first MessageBox, when I click on my other checkBoxes, the MessageBox will not show until I click on the radioButtons in my other groupBoxes. Once I've clicked on the other radio buttons, when I go back to click on my checkboxes, only then will the new MessageBox work or render on the screen. Updated code: https://paste.mod.gg/ujazbufowwyc/0
BlazeBin - ujazbufowwyc
A tool for sharing your source code with the world!
Pobiega
Pobiega4mo ago
hm there is a second file of code called Form1.Designer.cs can you paste the code for that one too? it sounds like some of your controls dont have their events wired up correctly
Themba
Themba4mo ago
BlazeBin - dwmuwhmlfivk
A tool for sharing your source code with the world!
Pobiega
Pobiega4mo ago
only checkBox1 has the event handler registered
//
// checkBox2
//
this.checkBox2.AutoSize = true;
this.checkBox2.Location = new System.Drawing.Point(23, 117);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(170, 30);
this.checkBox2.TabIndex = 1;
this.checkBox2.Text = "Extra Cheese";
this.checkBox2.UseVisualStyleBackColor = true;
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(23, 56);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(137, 30);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "Pepperoni";
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// checkBox2
//
this.checkBox2.AutoSize = true;
this.checkBox2.Location = new System.Drawing.Point(23, 117);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(170, 30);
this.checkBox2.TabIndex = 1;
this.checkBox2.Text = "Extra Cheese";
this.checkBox2.UseVisualStyleBackColor = true;
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(23, 56);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(137, 30);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "Pepperoni";
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
2 to 9 do not so only the first checkbox actually runs any code this one is also highly unexpected: this.groupBox_CheckBoxes.Enter += new System.EventHandler(this.groupBox_CheckBoxes_CheckedChanged);
Themba
Themba4mo ago
I'm new to WPF I don't understand why it would be unusual.
Pobiega
Pobiega4mo ago
This is winforms, not WPF
Themba
Themba4mo ago
sorry you are correct either wya im lost and don't know how to proceed to get my code to work
Pobiega
Pobiega4mo ago
well, did you connect all the checkboxes to the correct event? as said, only 1 of the 9 checkboxes were previously connected
Themba
Themba4mo ago
BlazeBin - nrrvnweiwjcn
A tool for sharing your source code with the world!
Pobiega
Pobiega4mo ago
okay and this didnt solve your problems?
Themba
Themba4mo ago
Nope same issue
Pobiega
Pobiega4mo ago
gotta admit I don't fully understand what you are having problems with can you upload your entire project somewhere like github?
Themba
Themba4mo ago
Alright it'll just take me a couple of Mins
Themba
Themba4mo ago
GitHub
GitHub - Themba619/Question1
Contribute to Themba619/Question1 development by creating an account on GitHub.
Pobiega
Pobiega4mo ago
👍 oh, its .NET Framework O_o Thats not a huge problem, but you should be aware that its an 8+ year old version that will no longer be updated and no new development should use without extremely good reasons ok, gotta ask, are you trolling?
Pobiega
Pobiega4mo ago
No description
Themba
Themba4mo ago
No just new to all this
Pobiega
Pobiega4mo ago
those are all empty you've wired your checkboxes up to empty handler methods that contain no code and the actual code that you want to run when a checkbox is checked is written in the wrong handler you've put that in the "Enter" event for the groupbox itself
Themba
Themba4mo ago
So how should i go about fixing these things?
Pobiega
Pobiega4mo ago
first, remove all empty handlers second, remove all event connections for pretty much everything third, connect your checkboxes CheckedChanged event to the code I helped you write before also, you edited that code and made it really weird
Themba
Themba4mo ago
You mean this one? var checkedBoxes = 0; foreach (var item in groupBox_CheckBoxes.Controls.OfType<CheckBox>()) { if (item.Checked) { checkedBoxes++; } }
Pobiega
Pobiega4mo ago
foreach ( var gb in groupBox_CheckBoxes.Controls.OfType<GroupBox>() )
{
foreach (var item in gb.Controls.OfType<CheckBox>() )
foreach ( var gb in groupBox_CheckBoxes.Controls.OfType<GroupBox>() )
{
foreach (var item in gb.Controls.OfType<CheckBox>() )
why are there two loops here? your groupbox doesnt contain other groupboxes
private void OnToppingsChanged(object sender, EventArgs e)
{
var checkedBoxes = 0;
foreach (var cb in toppingsGroup.Controls.OfType<CheckBox>())
{
if (cb.Checked)
{
checkedBoxes++;
}
}
MessageBox.Show($"{checkedBoxes} boxes are checked.");
Update_Total_Amount_Due();
}
private void OnToppingsChanged(object sender, EventArgs e)
{
var checkedBoxes = 0;
foreach (var cb in toppingsGroup.Controls.OfType<CheckBox>())
{
if (cb.Checked)
{
checkedBoxes++;
}
}
MessageBox.Show($"{checkedBoxes} boxes are checked.");
Update_Total_Amount_Due();
}
is all you need I renamed some of the stuff
Themba
Themba4mo ago
I got it to work but im not sure if i folled your instruction correctly https://paste.mod.gg/kiiiaeaupyrl/0
BlazeBin - kiiiaeaupyrl
A tool for sharing your source code with the world!
Pobiega
Pobiega4mo ago
sort of. you still have a bunch of empty handlers
private void firstGroupBox_Enter(object sender, EventArgs e)
{
}

private void groupBox2_Enter(object sender, EventArgs e)
{

}

private void groupBox3_Enter(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{

}
private void firstGroupBox_Enter(object sender, EventArgs e)
{
}

private void groupBox2_Enter(object sender, EventArgs e)
{

}

private void groupBox3_Enter(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{

}
you really shouldnt be using any Enterevents at all
Themba
Themba4mo ago
I'll get rid of those, Why should i not use Enter events? and also for some1 like me what would you recommend to learn more? and THANK YOU FOR HELPING ME
Pobiega
Pobiega4mo ago
So, each type of event has a special trigger "Enter" events trigger as part of the focus event chain and are honestly just very confusing and especially considering what you are trying to do, its just not the right event for you to be doing anything on if you want to update the price when the user updates the checkboxes - use the CheckedChanged event on the boxes you should not be using any events on the groupboxes at all regarding learning, my most important tip will be to STOP DOING .NET FRAMEWORK and move over to modern .NET there is no point in learning an almost 10 year old defunct runtime modern .NET supports winforms just fine
Themba
Themba4mo ago
Thank you for the advice and everything I look into these Events to learn more about them.:thxowo: