Elatesummer
Elatesummer

Reputation: 313

create an object using a string for the name in C#

I may be going about this the wrong way, so I'll set out the full scenario...

I have a DataTable which holds a number of items - like stock items. The data in this table can change, but it's populated from a database so that it's a distinct list. I want users to be able to select a number of them and I want to do this by creating a new checkBox object for each item in my DataTable.

So far I have the following (which I know is wrong, but illustrates what I'm trying to get at!):

string cbName = "cbNewTest";
int cbPosition = 24;
int cbTab = 1;

foreach (DataRow row in tblAllTests.Rows)
{
    string cbNewName = cbName + cbTab.ToString();
    this.(cbNewName) = new System.Windows.Forms.CheckBox();
    this.testInfoSplitContainer.Panel2.Controls.Add(this.(cbNewName));

    this.(cbNewName).AutoSize = true;
    this.(cbNewName).Location = new System.Drawing.Point(6, cbPosition);
    this.(cbNewName).Name = cbNewName;
    this.(cbNewName).Size = new System.Drawing.Size(15, 14);
    this.(cbNewName).TabIndex = cbTab;
    this.(cbNewName).Text = row["itemDesc"].ToString();

    cbPosition = cbPosition + 22;
    cbTab = cbTab + 1;
}

So of course the problem is the stuff in the brackets. Essentially, I want this to be whatever is in my string 'cbNewName' but I really don't know how to do this...I'm used to SQL as I'm a database gal, so this probably means I've coded this all wrong...

Any help would be very much appreciated...I'm very new to C# (or for that matter, any programming outside a database) so simple terms would be appreciated!

Upvotes: 1

Views: 347

Answers (4)

dgvid
dgvid

Reputation: 26633

I'm not entirely sure I understand your question, but if your intent is to 1) add a new checkbox control to the panel and 2) keep a reference to that new control object, which you can find later based on a string value. If that's right, then:

  1. Add a Dictionary to your class, something like:

    using System.Collections.Generic; using System.Windows.Forms;

    ...

    IDictionary checkboxes = new Dictionary();

  2. Create your new checkbox and assign it as an ordinary variable, e.g.:

    CheckBox cb = new CheckBox(); this.testInfoSplitContainer.Panel2.Controls.Add(cb); cb.AutoSize = true; // etc...

  3. Store a reference to the variable in the dictionary, like so:

    lock (((System.Collections.ICollection)checkboxes).SyncRoot) { checkboxes[cbNewName] = cb; }

  4. Clear out the dictionary in your form's overriden Dispose method, e.g., checkboxes.Clear().

Upvotes: 0

codemaster
codemaster

Reputation: 572

you can create a check box object and set the name of the check box like

CheckBox c = new CheckBox();
c.Name = "CheckBoxName";

and when you need to access this check box you can differentiate between then using the name like :

// Loop through all controls 
foreach (Control tempCtrl in this.Controls)
{
    // Determine whether the control is CheckBoxName,
    // and if it is, do what ever you want
    if (tempCtrl.Name == "CheckBoxName")
    {
        // ...
    }
}

Upvotes: 0

d--b
d--b

Reputation: 5779

Just create a Dictionary of checkboxes:

     var mycbs = new Dictionary<string,<System.Windows.Forms.CheckBox>>();

     foreach (DataRow row in tblAllTests.Rows)
     {
        string cbNewName = cbName + cbTab.ToString();

        mycbs[cbNewName] = new System.Windows.Forms.CheckBox();
        this.testInfoSplitContainer.Panel2.Controls.Add(mycbs[cbNewName]);

        mycbs[cbNewName].AutoSize = true;
        mycbs[cbNewName].Location = new System.Drawing.Point(6, cbPosition);
        mycbs[cbNewName].Name = cbNewName;
        mycbs[cbNewName].Size = new System.Drawing.Size(15, 14);
        mycbs[cbNewName].TabIndex = cbTab;
        mycbs[cbNewName].Text = row["itemDesc"].ToString();

        cbPosition = cbPosition + 22;
        cbTab = cbTab + 1;
    }

Upvotes: 0

Ry-
Ry-

Reputation: 225164

You can create a CheckBox as a variable, just like anything else. No need to assign it to one of the Form's properties, which are impossible to generate dynamically regardless:

CheckBox newCheckBox = new CheckBox();

// (Initialize your new CheckBox here, basically exactly as you're
// already doing except instead of this.(cbNewName) you use newCheckBox)

this.testInfoSplitContainer.Panel2.Controls.Add(newCheckBox);

If you need to access it later, since you're already setting the name, just do:

(CheckBox)this.testInfoSplitContainer.Panel2.Controls["theName"]

Upvotes: 4

Related Questions