473,786 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# foreach loop with radio buttons

24 New Member
VS2005, C# Windows Form

I can't quite figure out how to do this, but i think I'm close. I have a groupbox which contains 8 radio buttons, and I need to figure out which one is checked for entry into a database.

The button field looks like this:

()Assigned ()Working ()In Testing and so on.

In the DB, the values of the checkboxes are treated as an int, 0, 1, 2, 3 etc.

I've written this foreach statement to go through and get the text value of the radio button that's checked:

foreach(RadioBu tton RadioButton_X in groupBox9.Contr ols)
{
if (RadioButton_X. Checked == true)
{
string PATesting = RadioButton_X.T ext;
break;
}
}

and then I was planning to do a switch statement to assign the variable a numerical value based on the text:

switch(PATestin g)
{
case "Assigned":
PATesting = "0";
break;
etc...

but what I get is this error on the switch statement:
"The name 'PATesting' does not exist in the current context."


Basically, once I'm out of the foreach loop it dumps the variable. I'm thinking "duh, it's a loop" but I can't figure out a better way to go about this.

Thoughts?

Mike
Apr 2 '08 #1
2 21500
nateraaaa
663 Recognized Expert Contributor
Define string PATesting outside of your foreach loop and that will fix your problem. It is currently only in the scope of the foreach loop and is not accessible outside of the loop.

Expand|Select|Wrap|Line Numbers
  1.  string PATesting = string.Empty; 
  2. foreach(RadioButton RadioButton_X in groupBox9.Controls)
  3. {
  4. if (RadioButton_X.Checked == true)
  5. {
  6. PATesting = RadioButton_X.Text;
  7. switch(PATesting)
  8. {
  9. case "Assigned":
  10. PATesting = "0";
  11. break;
  12. }
  13. }
  14. }
  15.  
Nathan
Apr 2 '08 #2
somacore
24 New Member
Thanks!

That worked great. I had previously tried to define it outside the loop by doing:

string PATesting;

but that didn't work. Figures I needed it to equal something.

Thanks again!
Apr 2 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3282
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:
6
3293
by: Craig Keightley | last post by:
I have a page that has n number of radio groups (yes/No) how can i prevent the form being submitted if more than one radio group is not selected? By default all radio groups are unchecked The problem i am facing is that i do not know how many yes/no radio groups will be generated
13
2000
by: cody | last post by:
foreach does implicitly cast every object in the collection to the specified taget type without warning. Without generics this behaviour had the advantage of less typing for us since casting was neccessary in nearly every collection. But with generics I consider this feature of foreach as dangerous. Casting is now almost always unwanted. interface IFoo{} class Foo:IFoo{} class FooBar:IFoo{}
33
3663
by: Brian | last post by:
I have a list of plain HTML radio buttons that I want to be able to loop through, get the values from them and insert them into a db. each one should be a separate record... Can anyone please give me some kind of example on how to do this???? I am doing this in asp.net VB. Please let me know if you need any additional information. Thanks
2
11982
by: James P. | last post by:
Help, I need to display radio buttons on a form. The data is from SQL table: each row in each table is displayed as a radio button. I have multiple SQL tables so I understand I need to put them each in a GroupBox. All the examples I saw from the books or from the web show me how to add static radio buttons at design, or dynamically at run time but with fixed radio buttons (like from an array). I need to create radio buttons based...
4
4620
by: IWP506 | last post by:
Hey Is there a way to go through each element in the $_POST supervariable? I'm making a page with dynamic forms, so I don't know for sure how many $_POST variables there will be (the number of textboxes, radio buttons, etc changes). How can I either count them or do a foreach loop to do this?
9
9305
by: wreed | last post by:
I have a for loop seen below.... var the_form = document.getElementById(formName); for(var i=0; i<the_form.length; i++) { var temp = the_form.elements.type; if (temp == "radio") { for (x = 0; x < the_form.elements.length - 1; x++) {
2
5893
by: dpazza | last post by:
Hi, I'm creating a quiz on using a form in VB 2005 express. I have four sets of questions and answers (labels and radio buttons) and I change between which set of questions is currently shown on the form by changing the visible state of the radio buttons and labels utilising back and next buttons. E.g. Next button makes current radio buttons and labels invisible and
3
11388
AHayes
by: AHayes | last post by:
_Background I'm attempting to build a C# Windows application for work where I need to dynamically create and remove menu items (buttons). I've figured out how to dynamically create and (I think) remove these items, but I noticed that not all of my buttons are getting removed from the screen. _The Code This is a sample of how I'm CREATING the buttons: for (int i = 0; i < arrButtonData.GetLength(0); i++) { //Make new button
0
9650
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7515
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6748
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.