tags:

views:

55

answers:

5

Hi

I have some code that create a few components on the click of a button. Something like this.

CheckBox chk = new CheckBox();
chk.Top = 50;
chk.Left = 50;
chk.Text = "Check Box Test";
chk.Name = "chkTest"
this.Controls.Add(chk);

So how do I use this component. For example I tried this but got and error saying the component didn't exist. I just want to get their values.

if(chkTest.Checked)
{
//Do this
}

Please help.

Thanks in adv.

A: 

You're referencing chkTest but you created chk.

TrueWill
It doesn't matter what name i call it. I put chkTest because that is what name a gave it.
Ash Burlaczenko
@Ash - where are you declaring and assigning a value to the variable chkTest?
TrueWill
A: 

You could declare the Checkbox as a member variable of your page. Then you would be able to access it more easily.

Class MyPage { CheckBox chkTest;

// then in page load // chkTest = new CheckBox(); ...

}

MCain
Doing that thats up memory which is what I'm trying to avoid.
Ash Burlaczenko
@Ash Burlaczenko: that's a micro-optimization that won't really save you that much and may in fact result in you using more memory and CPU time to find the control from the collection.
siride
How do you expect that this saves memory?
Dave White
I thought the question was about how to help you reference the control. I don't see anything about saving memory.
MCain
@Dave White: it saves 4 bytes in an object that is likely to have very few instances.
siride
@siride I understand why and how much memory will be saved. I was asking the question so that Ash could answer with his thoughts. :)
Dave White
And I was pointing out the absurdity of it.
siride
The example was shorted to fit on OS. In my actual application i have 100 componant of which only 10 may be used depending which option they choose. Therefore i dont want to be creating something which takes up memory that wont be used.
Ash Burlaczenko
It's not that much memory. The variables are only references, that is, 4 bytes each. Say you had 10 instances of this form class, you'd save 4 * 10 * 100 = 4000 bytes. That's less than a page and certainly insignificant compared to the amount of memory on a system likely to run winforms apps.
siride
Interesting post on the memory optimization topic! http://stackoverflow.com/questions/3378536/does-a-variable-itself-consume-memory/3380361#3380361Maybe we don't know what we're talking about and we should leave it to experimentation and the compiler.
Dave White
+2  A: 

Either create a member variable in your class called chkTest that you can use later, or retrieve it on the fly from the Controls collection when needed, like so:

CheckBox chkTest = (CheckBox)Controls["chkTest"];
if(chkTest.Checked) {
    // ...
}
siride
Use `CheckBox chkTest = Controls["chkTest"] as CheckBox;` - there is no implicit conversion from `Control` to `CheckBox`.
Jaroslav Jandek
Worked perfect. Thanks.
Ash Burlaczenko
@Jaroslav Jandek thanks, I've updated the post for that. Also, since we expect it to be a CheckBox, there's no need to use the as operator -- a direct cast will suffice.
siride
@siride: unless you make a typo and add `chkTest` as `ComboBox` or whatever, in that case, it should fail anyway. So direct cast is OK.
Jaroslav Jandek
Why don't we code a bit more defensively. It takes all of 10 seconds to add code (null check with as operator) that protects use from typos.
Dave White
Right, but what are you going to do if it's null? Better to throw an exception (which is a big warning flag) instead of just eating it and having the program silently behave incorrectly.
siride
A: 
if ((Controls.Items["chkTest"] as CheckBox).Checked)
{
// Do this
}

should work, but it's not pretty to look at. :)

You could declare it as a variable, then use it like you did:

CheckBox chkTest = Controls.Items["chkTest"] as Checkbox;
if (chkTest.Checked)
{
// Do this
}

Look on this handy page for ways to manipulate and access your Control's collection of items: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection_members.aspx

Duracell
You should ensure that chkTest is not null before using it when you are using the as operator.
Dave White
+1  A: 

If you only care about the control when it is checked or unchecked, use an event.

chk.Checked += new RoutedEventHandler(CheckBox_Checked);
chk.Unchecked += new RoutedEventHandler(CheckBox_Checked);

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    CheckBox chkBox = sender as CheckBox;
    if (chkBox.IsChecked.Value)
    {
        // Do this...
    }
}

Make sure to unsubscribe from the event handlers when you are finished with them.

Quenton Jones