471,316 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,316 software developers and data experts.

Problem with radio buttons in .NET forms application

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.

Jun 24 '07 #1
4 2921

"Z.K." <no****@nospam.netwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl...
>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.

Jun 25 '07 #2
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.

Jun 25 '07 #3
PvdG42 wrote:
>
"Z.K." <no****@nospam.netwrote in message
news:ei**************@TK2MSFTNGP06.phx.gbl...
>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.
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.
Jul 1 '07 #4
Ben Voigt [C++ MVP] wrote:
>
>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.
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.
Jul 1 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by John Mullen | last post: by
5 posts views Thread by Gerard van Wilgen | last post: by
4 posts views Thread by Oscar Monteiro | last post: by
6 posts views Thread by Tom | last post: by
6 posts views Thread by lolodede | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.