Hi all!

...jup.. I'm back again..

This time I seem to have troubles with my array, I want it it to do a fairly easy job, but I can't figure out the syntax of how to do this.

I have a form with several sorted TextBoxes, named accordingly:

tb_dec[1-6]
tb_bin[1-6]
tb_hex[1-6]

This is a program I'm trying to make for fellowstudents who have problems with converting decimal <-> binairy <-> hexadecimal, and it's a great way for me to learn and practice (much more learning is involved then I had originally thought!)

So I created a button and added an event, and I know how to let it compare one field to another field and check if the solution is correct and to notify the user of that fact, but that is the hardcoded way... I'd like it to figure some stuff out for itself.

if (tb_dec#.Text == Convert.ToString(FromBin(tb_dec#.Text)))
                {
                    (pb_dec#.Image = Properties.Resources.icon_correct;
                }
                else
                {
                    (pb_dec#.Image = Properties.Resources.icon_incorrect;
                }

The #'s are to indicate that it needs to go from the number 1 to the number 6 in accordance with the fieldnames.

Now I wanted to use an array for this, but then I hit trouble...
Every way imaginable to me failed at trying to construct that name in a way that C# would understand that it is still a control on my form. Each time it tells me that tb_dec or pb_dec doesn't exist, while leaving out my intended numbers...

(I can post screenshots of the exact problem if anybody would like)

Anybody have a clue or hint?

Thanks for reading all of this!

Actually, you can use control arrays, but you will have to build the TextBoxes (or any other controls) manually, rather than using the designer (unless someone out there knows a way!).

Here's some code that will construct 6 TextBoxes, put them on the form and make sure they're visible:

At the top of your form class:

TextBox[] tb;

In your Form_Load():

tb = new TextBox[6];        
    for (int i = 0; i < 6; i++)
    {
         tb[i] = new TextBox();
         tb[i].Visible = true;
         tb[i].Top = 100 + (i * 50);
         tb[i].Width = 200;
         this.Controls.Add(tb[i]);
    }

You can now interact with the TextBoxes in the array. This should get you started. Good luck!

Thanks for replying mcriscolo. (again! ^_^)

Okay... I'll try incorporating that then... Shouldn't be to hard to configure the properties I've set.

Might even be fun in the way of an exercise/learning..

I'll leave the thread open for a little while, maybe other people have more suggestions. If not within say 24 hours, then I will mark it solved.

Thanks for the help!

Back again.

Now I've incorporated all of that:

buttonDec = new Button();
            buttonDec.Name = "button_Dec";
            buttonDec.Visible = true;
            buttonDec.Text = "Alleen Decimaal";
            buttonDec.Top = 70;
            buttonDec.Left = 20;
            buttonDec.Height = 25;
            buttonDec.Width = 100;
            this.Controls.Add(buttonDec);

But...
How do I access it? I've tried going for a simple buttonDec_Click but that didn't work. Then I tried assigning it a name and using that... Also a no-go.

Anything someone can clarify?

Thanks for the support!

You can add an event handler for the button by using the following syntax:

this.buttonDec.Click += new System.EventHandler(this.buttonDec_Click);

Then, define your handler function somewhere in your form code:

private void buttonDec_Click(object sender, EventArgs e)
{
...
}

However, since you are not wanting (I think!) to control the push button via arrays, you can safely drop a button on the form using the Designer and you should be OK, avoiding all the above stuff. You would only need an array of buttons if you plan on having a button for each set of TextBoxes.

Aaah, so it was indeed the handler! After some snooping around I figured as much but I didn't manage to activate it.

Then this explains it all! ^_^

Btw, yeah the buttons aren't in an array but I would also like to be able to actually WRITE a button (or whatever control) ;) So I figured I'd start giving that a try immediately too.

Thanks for all the help! I will mark this as solved since everything works exactly the way it is supposed to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.