473,238 Members | 1,924 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,238 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 3052

"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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.