473,545 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Hel lo 1")

MessageBox("Hel lo 2")

MessageBox("Hel lo 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_Ch eckedChanged(Sy stem:: Object^
sender, System::EventAr gs^ e)

{

MessageBox:: Show("Hello 1");

}

private: System:: Void radioButton2_Ch eckedChanged(Sy stem:: Object^
sender, System::EventAr gs^ e)

{
MessageBox:: Show("Hello 2");

}

private: System:: Void radioButton3_Ch eckedChanged(Sy stem:: Object^
sender, System::EventAr gs^ 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 3075

"Z.K." <no****@nospam. netwrote in message
news:ei******** ******@TK2MSFTN GP06.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("Hel lo 1")

MessageBox("Hel lo 2")

MessageBox("Hel lo 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_Ch eckedChanged(Sy stem:: Object^
sender, System::EventAr gs^ e)

{

MessageBox:: Show("Hello 1");

}

private: System:: Void radioButton2_Ch eckedChanged(Sy stem:: Object^
sender, System::EventAr gs^ e)

{
MessageBox:: Show("Hello 2");

}

private: System:: Void radioButton3_Ch eckedChanged(Sy stem:: Object^
sender, System::EventAr gs^ 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::Sho w("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::Sho w("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******** ******@TK2MSFTN GP06.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("He llo 1")

MessageBox("He llo 2")

MessageBox("He llo 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_Ch eckedChanged(Sy stem:: Object^
sender, System::EventAr gs^ e)

{

MessageBox:: Show("Hello 1");

}

private: System:: Void radioButton2_Ch eckedChanged(Sy stem:: Object^
sender, System::EventAr gs^ e)

{
MessageBox:: Show("Hello 2");

}

private: System:: Void radioButton3_Ch eckedChanged(Sy stem:: Object^
sender, System::EventAr gs^ 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::Sho w("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(radioButton 1->Checked == true)
MessageBox::Sho w("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
3592
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 the radio buttons, the form submits and a new layout is chosen. I would prefer if people can click on the icons themselves , rather than using...
1
3492
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 ? <li>ABACAVIR SULFATE</li> <INPUT NAME="ingredient0" TYPE=checkbox VALUE="ABACAVIR SULFATE"><br> <input name="ABACAVIR SULFATE AMOUNT" type="radio"...
5
1454
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 the radio button. There is not even an error message. This is a simplified version of the actual code, which is used to pass among
1
6133
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 are no instructions on how to make checkboxes and radio buttons required. I've tried adding these to my form, but I'm having no luck. Anyone know...
4
3261
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> <input type="radio" name=p2 value=2> <input type="radio" name=p2 value=3> then a text area and a button:
3
1507
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 raised. The click event for all buttons & radio buttons is handled by a single routine.
6
1542
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: I'm trying to define a series of radio objects as a large array for a survey, like this: 1) Does this make sense? <input type="radio"...
3
18573
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 toolbox called an Option Group. It could contain any number of radio buttons (which Access called Option Buttons), each of which had a "value" property. ...
6
2291
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 to answer Question one { var el = document.forms.elements; for(var i = 0 ; i < el.length ; ++i) { if(el.name == "att") { var radiogroup =...
0
7479
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7669
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.