0

I am new to C# so sorry if this is a silly question!

I currently have 3 forms in my code, I want to change the location of a label that is on the third form. Here is part of the code where i tried to specify this:

Form ThirdForm = new Form();
int horizontalCount;

ThirdForm.label[horizontalCount - 1].Top = label[16].Top;
ThirdForm.label[horizontalCount - 1].Left = label[16].Left;

This returns the error:

Error CS1061 'Form' does not contain a definition for 'label' and no extension method 'label' accepting a first argument of type 'Form' could be found (are you missing a using directive or an assembly reference?) HID PnP Demo

The labels are added to the form using this method:

Label[] createlabels(Image[] tiles, Bitmap Img, int[] tileOrder, Form form)
{
    int i = 0; int val;
    for (int j = 0; j < ROW_COUNT; j++)
        for (int k = 0; k < COLUMN_COUNT; k++)
        {
            //val = gridVal[j, k];
            label[i] = new Label();
            label[i].Font = new Font("Microsoft Sans Serif", 15F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));
            label[i].Width = Img.Width / COLUMN_COUNT;
            label[i].Height = Img.Height / ROW_COUNT;
            label[i].Left = k * (int)label[i].Width;
            label[i].Top = j * (int)label[i].Height;
            label[i].Tag = 0;
            label[i].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            if (i != 15)
            {
                string LabelNumber = i.ToString();
                string TileNumber = tileOrder[i].ToString();
                label[i].Image = tiles[tileOrder[i]];
                label[i].BorderStyle = BorderStyle.FixedSingle;
                label[i].Text = TileNumber;
            }
            else
            {
                label[i].BackColor = Color.Black;
                label[i].BorderStyle = BorderStyle.None;
                label[i].Text = "";
                label[i].Visible = false;
                z = i;

                whiteTileLeftLocation = label[i].Left;
                whiteTileTopLocation = label[i].Top;
                verticalCount = 4;
                horizontalCount = 4;
             }
            form.Controls.Add(label[i]);
            i++;
        }
    return label;
}

My Forms

What am I doing wrong?

Any help is greatly appreciated!

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
  • Type `Form` doesn't contains `label`. Declare `ThirdForm` as type of your derived type and make sure that `label` is public. – Alessandro D'Andria Nov 28 '17 at 10:42
  • use something like this here: https://stackoverflow.com/questions/4630391/get-all-controls-of-a-specific-type You need to iterate the controls of your form. – Ric Nov 28 '17 at 10:44
  • You mean, you call `createlabels()` inside Form class constructor?, otherwise, it doesnt have initialized labels, as @Alessandro D'Andria said, create them public, and you should call each of them by the `Name` property, or iterating Controls, but you didnt create a **label** called `label` – Ferus7 Nov 28 '17 at 10:45

0 Answers0