473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3059

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ken Fine | last post by:
I've made a content managment system that uses icons to represent page layouts. To choose a different layout, the user clicks on a radio button associated with each layout icon. On click of one of...
1
by: John Mullen | last post by:
I want to take the following HTLM and use javascript to turn on radio buttons if checkbox is checked, can I do this with javascript (maybe onClick or an array) or do i need a server side script ?...
5
by: Gerard van Wilgen | last post by:
I noticed that the following line of code works in IE 6 but not in Netscape 7.1: <INPUT TYPE="radio" NAME="selComb" VALUE="0" ONCLICK="alert(selComb.value)"> Nothing happens when I click on...
1
by: sman | last post by:
Hi, I recently read this article on About.com on how to create required fields for a form: http://javascript.about.com/library/scripts/blformvalidate.htm Everything works great except that there...
4
by: Oscar Monteiro | last post by:
I Have to sets of Radio buttons like so: <input type="radio" name=p1 value=1> <input type="radio" name=p1 value=2> <input type="radio" name=p1 value=3> <br> <input type="radio" name=p2 value=1>...
3
by: G Namx | last post by:
Hi, We have a usercontrol with a number of controls on it. One being a radio button and one being a button. When we set button.enabled = false the click event of the radio button is being...
6
by: Tom | last post by:
I have a problem, to which I have been unable to find a solution for days now, after checking numerous references (both in books and online). Perhaps someone here can help. Here's my problem: ...
3
by: teddysnips | last post by:
Back in the dim mists of antiquity I used to program in VBA for Microsoft Access (hey, don't knock it - very useful tool for the right application). This had a really handy control in the...
6
by: lolodede | last post by:
i want to dont let the user submit the form unless they answer these two radio buttons questions but i dont why isnot working the code unction checkForm(obj) //This fuction shows that the user need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.