Hello Everyone,
I would just like some advice to make sure i am doing what i want to
do the right way because i don't think i am.
I have a form that contains checkboxes. The checkboxes are created
dynamically from a database. When the user presses the submit button i
want to loop through all of the checkboxes and fill an IList with the
text from the checkboxes that are checked. Here is how i do it.. it
looks a bit convoluted , is there a better way of doing this?
Thanks in advance,
foreach (Object obj in this.Controls)
{
if (obj.GetType() == typeof(GroupBox))
{
GroupBox tempGroupBox = (GroupBox)obj;
foreach (Object tmpObj in
tempGroupBox.Controls)
{
if (tmpObj.GetType() == typeof(CheckBox))
{
CheckBox chk = (CheckBox)tmpObj;
if (chk.Checked)
{
imageList.Add(chk.Text);
}
}
}
}
} 4 1583
On Feb 14, 9:09 am, esebast...@esolutionsgroup.ca wrote:
Hello Everyone,
I would just like some advice to make sure i am doing what i want to
do the right way because i don't think i am.
I have a form that contains checkboxes. The checkboxes are created
dynamically from a database. When the user presses the submit button i
want to loop through all of the checkboxes and fill an IList with the
text from the checkboxes that are checked. Here is how i do it.. it
looks a bit convoluted , is there a better way of doing this?
Thanks in advance,
foreach (Object obj in this.Controls)
{
if (obj.GetType() == typeof(GroupBox))
{
GroupBox tempGroupBox = (GroupBox)obj;
foreach (Object tmpObj in
tempGroupBox.Controls)
{
if (tmpObj.GetType() == typeof(CheckBox))
{
CheckBox chk = (CheckBox)tmpObj;
if (chk.Checked)
{
imageList.Add(chk.Text);
}
}
}
}
}
How about :
ArrayList checkBoxText = new ArrayList();
GetCheckBoxText(this, checkBoxText);
where
private void GetCheckBoxText(Control ctl, ArrayList list)
{
CheckBox chk = ctl as CheckBox;
if (chk == null)
{
foreach (Control containedControl in ctl.Controls)
{
GetCheckBoxText(containedControl, list);
}
}
else
{
list.Add(chk.Text);
}
}
That worked beautifully, thank you so much for the code example! I
love asking for opinions as i feel it helps me grow as a developer.
I did add a small section in the else:
if(chk.checked)
{
}
around the list.add because i only want the text of the check boxes
whose checked values are checked.
Thanks so much!
Erin
<es********@esolutionsgroup.cawrote:
Hello Everyone,
I would just like some advice to make sure i am doing what i want to
do the right way because i don't think i am.
I have a form that contains checkboxes. The checkboxes are created
dynamically from a database. When the user presses the submit button i
want to loop through all of the checkboxes and fill an IList with the
text from the checkboxes that are checked. Here is how i do it.. it
looks a bit convoluted , is there a better way of doing this?
Well, every time you're calling:
if (foo.GetType()==typeof(Bar))
it would be better to use
if (foo is Bar)
or
Bar bar = foo as Bar;
if (bar != null)
Reasons:
1) It's faster
2) It's more idiomatic
3) It works with polymorphism (eg for CheckBox, it'll still pick up the
case when you've got WhizzyDerivedCheckBox instead of just CheckBox).
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
if you are using CDull you are doing it wrong
On Feb 14, 9:09 am, esebast...@esolutionsgroup.ca wrote:
Hello Everyone,
I would just like some advice to make sure i am doing what i want to
do the right way because i don't think i am.
I have a form that contains checkboxes. The checkboxes are created
dynamically from a database. When the user presses the submit button i
want to loop through all of the checkboxes and fill an IList with the
text from the checkboxes that are checked. Here is how i do it.. it
looks a bit convoluted , is there a better way of doing this?
Thanks in advance,
foreach (Object obj in this.Controls)
{
if (obj.GetType() == typeof(GroupBox))
{
GroupBox tempGroupBox = (GroupBox)obj;
foreach (Object tmpObj in
tempGroupBox.Controls)
{
if (tmpObj.GetType() == typeof(CheckBox))
{
CheckBox chk = (CheckBox)tmpObj;
if (chk.Checked)
{
imageList.Add(chk.Text);
}
}
}
}
}
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tung Wai Yip |
last post by:
I've build the following utility. It works so far but I want to make
sure I'm doing the right thing. I am running Python 2.3 on windows
2000.
def execute(cmd):
"""
execute cmd in sub-process,...
|
by: Steven Bethard |
last post by:
How do I make sure that my entire string was parsed when I call a
pyparsing element's parseString method? Here's a dramatically
simplified version of my problem:
py> import pyparsing as pp
py>...
|
by: AC |
last post by:
I have a page class that inherits the System.Web.UI.Page class. Essentially what it does is load a user profile from a session (if it isn't loaded, it loads it into the session) and exposes it via a...
|
by: Nathan Sokalski |
last post by:
I have a form that allows the user to upload a file. Even though <input
type="file" runat="server"> is intended to have the user choose the file
using the browse button, it still allows them to...
|
by: BookerW |
last post by:
Ok interesting, on my local machine
And i did not do a good job of keeping track of the exact order....
but on my local machine
in which i have vis studio 2003 installed and IIS for win XP,...
|
by: robinsand |
last post by:
Header File: car.h #if !defined CAR_H
#define CAR_H
enum TCarType { ctEconomy = 1, ctCompact, ctStandard, ctFullSize,
ctMiniVan, ctSUV };
class Car
{
public:
Car();
|
by: lanoak |
last post by:
Hi, new hear and just starting out in CSS so I'm sure this is something simple.
I'm trying to build a horizontal menu bar with 5 different headings: home, process, policies and proceedures,...
|
by: ARC |
last post by:
I'm wondering if this is only happening to me. I'm running Win XP home
edition, and have Access 97 on my computer, and have had Access 2000 on my
pc. I have 512MB RAM, and an AMD 2700 CPU.
With...
|
by: RubyRue |
last post by:
New to developing. We are working on a project using asp.net and c# where data is pulled out of an sql database via multiple tables and displayed via a details view. The user can then update the...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |