about Checkbox !!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,
i have a question about checkbox.
i made a form, with some textboxes, and buttons..
the idea is this..
i want to enter some information (like name, address..) and some hobbies !
i made the hobbies as a checkboxes.. so the user check the ones apply..
then i made a "SAVE" button, to add this whole info to a arraylist,
i put a "back" and "next" buttons to navigate records..
it worked perfectly for the names and addresses ! (when i click back, it
shows the previous data object with its info)
but, the problem is.. i cant retrieve the checkbox condition !
i used this code to stor data :

private ArrayList GetHobbies()
{
if (mCheckBoxWalking.Checked)
Hobbies.Add("Walking");
if (mCheckBoxSwimming.Checked)
Hobbies.Add("Swimming");
if (mCheckBoxReading.Checked)
Hobbies.Add("Reading");
return Hobbies;
}

and i tried to retrive the use information by this code :

void GetUser(UserInformation user)
{
mTextboxName.Text = user.Name; // to get the name
mTextboxAddress.Text = user.Address; // to get the adderss
if (user.Hobbies.ToString() == "Walking")
mCheckBoxWalking.Checked = true;
if (user.Hobbies.ToString() == "Swimming")
mCheckBoxSwimming.Checked = true;
if (user.Hobbies.ToString() == "Reading")
mCheckBoxReading.Checked = true;
}
as i said, the name and address worked.
but the checkbox is not !
plz some help here ! :)
and thanx in advance..
 
Hi,

How are you storing the hobbies?

That is the key to solve your problem.
 
at first thnx for ur reply,
2nd, i made a new ArrayList:
ArrayList Hobbies = new ArrayList();
then i made a function (Type arraylist) called GetHobbies, and i used it to
get the user hobbies, the function :

private ArrayList GetHobbies()
{
if (mCheckBoxWalking.Checked)
Hobbies.Add("Walking");
if (mCheckBoxSwimming.Checked)
Hobbies.Add("Swimming");
if (mCheckBoxReading.Checked)
Hobbies.Add("Reading");
return Hobbies;
}

so, when u click "save" in form.. it calls the GetHobbies function:
user.Hobbies = GetHobbies();

so all, of the hobbies Which was selected, will be added to the Hobbies
ArrayList by using GetHobbies() (ADD)..

this is how i store the hobbies..
but still, when i retrieve them, it do nothing !
i debugged the code step by step, and when it reaches the lines (in
GetUser() function) :

if (user.Hobbies.ToString() == "Walking")
mCheckBoxWalking.Checked = true;
if (user.Hobbies.ToString() == "Swimming")
mCheckBoxSwimming.Checked = true;
if (user.Hobbies.ToString() == "Reading")
mCheckBoxReading.Checked = true;

it just do nothing (as if the "IF" condition is false !) but its true!
im working on this code since 4 days !
i just don't know whats wrong :/
 
rushd said:
Hi all,
i have a question about checkbox.
i made a form, with some textboxes, and buttons..
the idea is this..
i want to enter some information (like name, address..) and some hobbies !
i made the hobbies as a checkboxes.. so the user check the ones apply..
then i made a "SAVE" button, to add this whole info to a arraylist,
i put a "back" and "next" buttons to navigate records..
it worked perfectly for the names and addresses ! (when i click back, it
shows the previous data object with its info)
but, the problem is.. i cant retrieve the checkbox condition !
i used this code to stor data :

private ArrayList GetHobbies()
{
if (mCheckBoxWalking.Checked)
Hobbies.Add("Walking");
if (mCheckBoxSwimming.Checked)
Hobbies.Add("Swimming");
if (mCheckBoxReading.Checked)
Hobbies.Add("Reading");
return Hobbies;
}

and i tried to retrive the use information by this code :

void GetUser(UserInformation user)
{
mTextboxName.Text = user.Name; // to get the name
mTextboxAddress.Text = user.Address; // to get the adderss
if (user.Hobbies.ToString() == "Walking")
mCheckBoxWalking.Checked = true;
if (user.Hobbies.ToString() == "Swimming")
mCheckBoxSwimming.Checked = true;
if (user.Hobbies.ToString() == "Reading")
mCheckBoxReading.Checked = true;
}
as i said, the name and address worked.
but the checkbox is not !
plz some help here ! :)
and thanx in advance..

Hi,

Your problem is you're performing the tests wrong. You're looking at the
result of calling ToString on the arraylist, which is not what you want.

The following code should work:

///
if (user.Hobbies.Contains("Walking"))
mCheckBoxWalking.Checked = true;
///

And you can easily extend it to your other cases.

What's going on here is that you're *interrogating* the list to check for
the presence of an item, what you were doing before wasn't checking the
list.
 
Tom Spink said:
Hi,

Your problem is you're performing the tests wrong. You're looking at the
result of calling ToString on the arraylist, which is not what you want.

The following code should work:

///
if (user.Hobbies.Contains("Walking"))
mCheckBoxWalking.Checked = true;
///

And you can easily extend it to your other cases.

What's going on here is that you're *interrogating* the list to check for
the presence of an item, what you were doing before wasn't checking the
list.

thanks :) it worked (partially) !
but thanks any way! its now like 80% done.. :)
when i save like 10 records, and i press "Back".. it shows me the last
checkbox was checked (which is right).. or if i press "First" (which takes me
to the 1st data record) it shows me the checkbox for the 1st Data record
(which is also right)
but, the problem now is this.. when i press "back" again.. the checkbox
state remains as is ! (showing the 10th data record) so, i retrieve only one
checkbox state.
any ideas !?
 
Rushd said:
thanks :) it worked (partially) !
but thanks any way! its now like 80% done.. :)
when i save like 10 records, and i press "Back".. it shows me the last
checkbox was checked (which is right).. or if i press "First" (which takes
me to the 1st data record) it shows me the checkbox for the 1st Data
record (which is also right)
but, the problem now is this.. when i press "back" again.. the checkbox
state remains as is ! (showing the 10th data record) so, i retrieve only
one checkbox state.
any ideas !?

Hi,

Without further implementation details, I can only speculate as to what's
happening. Can you provide more information about how you're storing the
hobbies in the array list, and how you're storing them for each user?
 
ok !sorry.. it works now :)
i had one arraylist for all of the users :s
i put the arraylist definition inside the "save" method, so now it makes new
arraylist for each new data record.. it works 100% now :)
thanks a lot
 
Rushd said:
ok !sorry.. it works now :)
i had one arraylist for all of the users :s
i put the arraylist definition inside the "save" method, so now it makes
new arraylist for each new data record.. it works 100% now :)
thanks a lot

Excellent. I'm glad you got it sorted.
 
Back
Top