Problem with radio buttons in .NET forms application

Z

Z.K.

I started a forms application and I put on the form three buttons. In
the functions for the radio buttons I put in a single messagebox like:



MessageBox("Hello 1")

MessageBox("Hello 2")

MessageBox("Hello 3")



The first radio button works fine and displays "Hello 1". The other
ones do not work as I expected though and I am not sure why. When I
click on the second radio button, I get Hello1 and after clicking okay I
get Hello 2. The same for the third radio button though it starts with
radio 2 and then radio 3. This is my first experience with the new .NET
forms application and I would have expected it to act somewhat like a
radio button in MFC which only displays that MessageBox in the radio
button that was selected.



These are the functions that the design application created for me and I
only added the MessageBox statements.



private: System:: Void radioButton1_CheckedChanged(System:: Object^
sender, System::EventArgs^ e)

{

MessageBox:: Show("Hello 1");



}

private: System:: Void radioButton2_CheckedChanged(System:: Object^
sender, System::EventArgs^ e)

{


MessageBox:: Show("Hello 2");



}

private: System:: Void radioButton3_CheckedChanged(System:: Object^
sender, System::EventArgs^ e)

{

MessageBox:: Show("Hello 3");


}



Can someone tell me what I am doing wrong? Also, I was wondering why
visual studio puts the functions for the form in the forms.h file
instead of in a *.cpp file.

Z.K.
 
P

PvdG42

Z.K. said:
I started a forms application and I put on the form three buttons. In the
functions for the radio buttons I put in a single messagebox like:



MessageBox("Hello 1")

MessageBox("Hello 2")

MessageBox("Hello 3")



The first radio button works fine and displays "Hello 1". The other ones
do not work as I expected though and I am not sure why. When I click on
the second radio button, I get Hello1 and after clicking okay I get Hello
2. The same for the third radio button though it starts with radio 2 and
then radio 3. This is my first experience with the new .NET forms
application and I would have expected it to act somewhat like a radio
button in MFC which only displays that MessageBox in the radio button that
was selected.



These are the functions that the design application created for me and I
only added the MessageBox statements.



private: System:: Void radioButton1_CheckedChanged(System:: Object^
sender, System::EventArgs^ e)

{

MessageBox:: Show("Hello 1");



}

private: System:: Void radioButton2_CheckedChanged(System:: Object^
sender, System::EventArgs^ e)

{


MessageBox:: Show("Hello 2");



}

private: System:: Void radioButton3_CheckedChanged(System:: Object^
sender, System::EventArgs^ e)

{

MessageBox:: Show("Hello 3");


}



Can someone tell me what I am doing wrong? Also, I was wondering why
visual studio puts the functions for the form in the forms.h file instead
of in a *.cpp file.

Z.K.

I'm sure there's a better way to do this, but to address your immediate
problem, the behavior you observe is because of the event you've chosen. The
CheckedChanged event fires each time the button is selected *or* deselected,
thus when you select button 2, button 1 is deselected, then button 2 is
selected. Both cause the CheckedChanged event to fire, resulting in the
message boxes you see. To avoid this, use the following in your Checked
Changed event procedures:

if(radioButton1->Checked == true)
MessageBox::Show("Button 1 selected");

Now the message box will only appear when a button is selected.

I'm sorry, but I've forgotten the rationale for the placement of code in the
..h rather than in the .cpp file, but it appears unlikely to change (in Orcas
as well), so we'll have to get used to it.
 
B

Ben Voigt [C++ MVP]

I'm sure there's a better way to do this, but to address your immediate
problem, the behavior you observe is because of the event you've chosen.
The CheckedChanged event fires each time the button is selected *or*
deselected, thus when you select button 2, button 1 is deselected, then
button 2 is selected. Both cause the CheckedChanged event to fire,
resulting in the message boxes you see. To avoid this, use the following
in your Checked Changed event procedures:

Yup, I agree with all of that. Checked going from true -> false is indeed a
"change".
if(radioButton1->Checked == true)
MessageBox::Show("Button 1 selected");

Now the message box will only appear when a button is selected.

I'm sorry, but I've forgotten the rationale for the placement of code in
the .h rather than in the .cpp file, but it appears unlikely to change (in
Orcas as well), so we'll have to get used to it.

This is just Microsoft not bothering to get the C++ designer right. C#
doesn't have the concept of separate header and implementation files, and
they just reused the C# code for C++. The C++ designer is most likely going
away in the future, C++/CLI is positioned for interfacing to native code
cleanly and efficiently, not for rapid GUI programming. Any of the unique
C++ features like templates weren't recognized by the designers anyway.

You should definitely move the functions to the .cpp file, and after you do,
the designer should still be able to find them there.
 
Z

Z.K.

PvdG42 said:
I'm sure there's a better way to do this, but to address your immediate
problem, the behavior you observe is because of the event you've chosen.
The CheckedChanged event fires each time the button is selected *or*
deselected, thus when you select button 2, button 1 is deselected, then
button 2 is selected. Both cause the CheckedChanged event to fire,
resulting in the message boxes you see. To avoid this, use the following
in your Checked Changed event procedures:

if(radioButton1->Checked == true)
MessageBox::Show("Button 1 selected");

Now the message box will only appear when a button is selected.

I'm sorry, but I've forgotten the rationale for the placement of code in
the .h rather than in the .cpp file, but it appears unlikely to change
(in Orcas as well), so we'll have to get used to it.

Thanks for the info. I finally figured it out though, by looking at the
events and noticing that there were functions listed in both the click
and Check Changed events. In MFC I never really had to worry about this
as it was usually done for me as soon as the function was created.
Anyway, now that I think I finally got a handle on the events I managed
to get it working and without adding the extra code that you suggested.
Thanks again though as it did point me in the right direction.

Z.K.
 
Z

Z.K.

Ben said:
Yup, I agree with all of that. Checked going from true -> false is
indeed a "change".


This is just Microsoft not bothering to get the C++ designer right. C#
doesn't have the concept of separate header and implementation files,
and they just reused the C# code for C++. The C++ designer is most
likely going away in the future, C++/CLI is positioned for interfacing
to native code cleanly and efficiently, not for rapid GUI programming.
Any of the unique C++ features like templates weren't recognized by the
designers anyway.

You should definitely move the functions to the .cpp file, and after you
do, the designer should still be able to find them there.

Thanks. That did work as long as I put in in the main file, but I will
have to read up on namespaces as I have not used that C++ ability for a
long time and I am not really sure how to split the form1.h file up into
a form1.h and a form1.cpp file since a namespace is involved. Yes, I
know I could just use the forms1.h file, but I find it much easier to
understand and more appealing to have the function declarations in a
separate file than the code. Again thanks for the info, that was very
helpful.

Z.K.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top