none
Error CS1061 with picturebox.Image RRS feed

  • Question

  • I am using the command line C# compiler only (no Visual Studio).  I can loop through all the controls on my form using the following code:

    foreach (Control c in this.Controls) {
    if (c is PictureBox) {
    xS = getX(c.Name);
    yS = getY(c.Name);
    if (xS != i) continue;
    if (yS != _staticY) continue;
    c.Image = image[int.Parse(grid[i,_staticY])];
    }
    }

    but I get the following compile error when I reference c.Image while references to c.Name are valid.  Can anyone tell me what my problem might be?  I am trying to change the image in one of 64 PictureBoxs on my form.

    C:\C#_Stuff\MyJewel>csc /target:winexe *.cs 
    Microsoft (R) Visual C# Compiler version 2.2.0.61624
    Copyright (C) Microsoft Corporation. All rights reserved.

    Jewel.cs(277,9): error CS1061: 'Control' does not contain a definition for 'Image' and no extension method 'Image' accepting a first argument of type 'Control' could be found (are you missing a using directive or an assembly reference?)

    Wednesday, August 29, 2018 2:44 AM

Answers

  • If you are interested in some news, then try this too:

       foreach( Control c in this.Controls )

       {

          if( c is PictureBox pb )

          {

             . . .

             pb.Image = . . .

          }

       }



    • Edited by Viorel_MVP Wednesday, August 29, 2018 5:32 AM
    • Marked as answer by bigsagal Wednesday, August 29, 2018 2:38 PM
    Wednesday, August 29, 2018 5:31 AM

All replies

  • As the error says, the Control class doesn't have an image. You have tested that the control is a PictureBox, but you haven't cast it, so the compiler is confused.

    foreach (Control c in this.Controls)
    {
       if (c is PictureBox)
       {
          PictureBox pic = (PictureBox)c; // This cast should be safe, because we already know c is a PictureBox.
          xS = getX(pic.Name);
          yS = getY(pic.Name);
          if (xS != i) continue;
          if (yS != _staticY) continue;
          pic.Image = image[int.Parse(grid[i,_staticY])];
       }
    }
    
    

    • Proposed as answer by Stavros Kasidis Wednesday, August 29, 2018 11:07 AM
    Wednesday, August 29, 2018 5:02 AM
  • If you are interested in some news, then try this too:

       foreach( Control c in this.Controls )

       {

          if( c is PictureBox pb )

          {

             . . .

             pb.Image = . . .

          }

       }



    • Edited by Viorel_MVP Wednesday, August 29, 2018 5:32 AM
    • Marked as answer by bigsagal Wednesday, August 29, 2018 2:38 PM
    Wednesday, August 29, 2018 5:31 AM