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
Please share your code at $paste so we can actually see all of it
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!
BlazeBin - msxdoavemmky
A tool for sharing your source code with the world!
and the problem is that currently your messagebox does not show when you check a checkbox in the third group?
yes
ok so lets look at your code
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 controlsI think I understand so what should I use to gain access to my checkBoxes?
foreach (var item in groupBox1.Controls)
then you can check the types
some sample code:
alternatively you can use OfType
as such
item
here is then guaranteed to be a CheckBoxIssue: 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!
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 correctlyBlazeBin - dwmuwhmlfivk
A tool for sharing your source code with the world!
only
checkBox1
has the event handler registered
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);
I'm new to WPF I don't understand why it would be unusual.
This is winforms, not WPF
sorry you are correct either wya im lost and don't know how to proceed to get my code to work
well, did you connect all the checkboxes to the correct event?
as said, only 1 of the 9 checkboxes were previously connected
BlazeBin - nrrvnweiwjcn
A tool for sharing your source code with the world!
okay
and this didnt solve your problems?
Nope same issue
gotta admit I don't fully understand what you are having problems with
can you upload your entire project somewhere like github?
Alright it'll just take me a couple of Mins
GitHub
GitHub - Themba619/Question1
Contribute to Themba619/Question1 development by creating an account on GitHub.
👍
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?
No just new to all this
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
So how should i go about fixing these things?
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 weirdYou mean this one? var checkedBoxes = 0;
foreach (var item in groupBox_CheckBoxes.Controls.OfType<CheckBox>())
{
if (item.Checked)
{
checkedBoxes++;
}
}
why are there two loops here?
your groupbox doesnt contain other groupboxes
is all you need
I renamed some of the stuff
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!
sort of. you still have a bunch of empty handlers
you really shouldnt be using any
Enter
events at allI'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
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 fineThank you for the advice and everything I look into these Events to learn more about them.:thxowo: