为什么出现此消息:不包含“控件”的定义,也没有接受第一个参数的扩展方法“控件”

本文关键字:控件 第一个 方法 扩展 参数 包含 定义 为什么 消息 | 更新日期: 2023-09-27 18:32:40

嗨,

我正在做的是一种显示特定文件夹中的图像的方法,但是当我调试时,我在最后一行代码上收到此错误,我不知道为什么。

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?)

感谢您的任何帮助。

这是代码:

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);
        }   
    }
}

为什么出现此消息:不包含“控件”的定义,也没有接受第一个参数的扩展方法“控件”

复制和粘贴代码时可能会发生这种情况。 这。控件希望类"classTools"具有成员控件。 将预期的成员变量添加到"classTools"或从另一个类派生它。

您导入了System.Windows.Forms但实际上并未使用它。将类定义更改为class classTools : Forms,然后您就可以使用 Controls 类了。

而且似乎Controls类中没有Add方法,只要不将Add扩展方法添加到Controls,就会给你一个错误。

就我而言,我正在复制/粘贴其他页面的用户控件,但我没有检查页面顶部 Register 标记处的src。它似乎不是从根目录(~/)开始的,所以它只能找到在同一地图上的文件:

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

因此,它将其视为用户控件,而不是自己的类。

我将控件复制到的页面位于不同的地图上,因此更正该路径并从根目录开始已解决此问题。