ithieme1
ithieme1

Reputation: 45

Why this message: does not contain a definition for 'Controls' and no extension method 'Controls' accepting a first argument

Hi,

What I'm doing is a method to display images from a specific folder but when I debug I'm getting this error on the last line of code and I have no idea why.**

Error 3 'MBKiosk.classTools' does not contain a definition for 'Controls' and no extension    
method 'Controls' accepting a first argument of type 'MBKiosk.classTools' could be found (are you 
missing a using directive or an assembly reference?)

Thanks for any help.

Here is the code:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Collections.ObjectModel;
using MBKiosk;

namespace MBKiosk
{
    class classTools
    {
        public void ShowImages(string path)
        {
            FlowLayoutPanel imagePanel = new FlowLayoutPanel();
            imagPanel.FlowDirection = FlowDirection.LeftToRight;
            imagePanel.Size = new Size(1240, 630);
            imagePanel.Location = new Point(12, 344);
            imagePanel.WrapContents = true;
            imagePanel.AutoScroll = false;
            DirectoryInfo dInfo = new DirectoryInfo(path);
            foreach (FileInfo file in dInfo.GetFiles())
            {
                System.Diagnostics.Debug.Print(file.Extension);
                if ((file.Extension == ".jpg") || (file.Extension == ".gif") || (file.Extension == 
                    ".png"))
                {
                    PictureBox image = new PictureBox();
                    image.Image = Image.FromFile(file.FullName);
                    image.SizeMode = PictureBoxSizeMode.Normal;
                    image.Size = new Size(180, 108);
                    imagePanel.Controls.Add(image);
                    imagePanel.Refresh();
                }
            }
            this.Controls.Add(imagePanel);
        }   
    }
}

Upvotes: 1

Views: 6767

Answers (3)

Steven
Steven

Reputation: 2123

In my case, I was copy/pasting the user controls for an other page, but I havn't checked the src at the Register tag on top of the page. It appears it didn't started at the root (~/), so it could only find files that were on the same map:

<%@ Register Src="MyPopUp.ascx" TagName="MyPopup" TagPrefix="uc3" %>

Due to this, it saw it as an UserControl rather than it's own class.

The page I copied the control to was on a different map, so correcting that path, and starting from the root have fixed the issue.

Upvotes: 0

Dylan Czenski
Dylan Czenski

Reputation: 1365

You imported System.Windows.Forms but are not actually using it. Change you class definition to class classTools : Forms, and then you will be able to use the Controls class.

And seems like there is no Add method in the Controls class, as long as you don't add the Add extension method to Controls, it is going to give you an error.

Upvotes: 0

tzerb
tzerb

Reputation: 1433

This can happen when you copy and paste code. this.Controls expects your class 'classTools' to have a member Controls. Either add the intended member variable to 'classTools' or derive it from another class.

Upvotes: 1

Related Questions