【问题标题】:missing partial modern on declaration声明中缺少部分现代
【发布时间】:2013-03-12 19:24:54
【问题描述】:

当我尝试运行我的程序时,我如何纠正我得到的错误,我在网上找到了这个程序,它似乎是用 Visual c# 2005 编译的,我使用的是 Visual c# 2010 我在编译之前遇到了这两个错误

“RecursiveSearchCS.Form1.components”和“RecursiveSearchCS.Form1.components”之间的错误 2 歧义 'RecursiveSearchCS.Form1.components' C:\Users\jacr\AppData\Local\Temporary 项目\WindowsFormsApplication1\Form1.cs 46 21 WindowsFormsApplication1

错误 1 ​​以下方法之间的调用不明确或 属性:'RecursiveSearchCS.Form1.InitializeComponent()' 和 'RecursiveSearchCS.Form1.InitializeComponent()' C:\Users\jacr\AppData\Local\Temporary 项目\WindowsFormsApplication1\Form1.cs

32 13 WindowsFormsApplication1

当我尝试编译它时出现错误,我得到了这个

错误 1 ​​类型声明中缺少部分修饰符 '递归搜索CS.Form1';这种类型的另一个部分声明 存在 C:\Users\jacr\AppData\Local\Temporary 项目\WindowsFormsApplication1t\Form1.cs 14 18 WindowsFormsApplication1t

我到底应该做什么?我的程序在目录中搜索文件文本文件,但似乎我得到了这个错误......这是form1.cs上的代码

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace RecursiveSearchCS
{
    public class Form1 : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Button btnSearch;
        internal System.Windows.Forms.TextBox txtFile;
        internal System.Windows.Forms.Label lblFile;
        internal System.Windows.Forms.Label lblDirectory;
        internal System.Windows.Forms.ListBox lstFilesFound;
        internal System.Windows.Forms.ComboBox cboDirectory;
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        private void InitializeComponent()
        {
            this.btnSearch = new System.Windows.Forms.Button();
            this.txtFile = new System.Windows.Forms.TextBox();
            this.lblFile = new System.Windows.Forms.Label();
            this.lblDirectory = new System.Windows.Forms.Label();
            this.lstFilesFound = new System.Windows.Forms.ListBox();
            this.cboDirectory = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();

            this.btnSearch.Location = new System.Drawing.Point(608, 248);
            this.btnSearch.Name = "btnSearch";
            this.btnSearch.TabIndex = 0;
            this.btnSearch.Text = "Search";
            this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);

            this.txtFile.Location = new System.Drawing.Point(8, 40);
            this.txtFile.Name = "txtFile";
            this.txtFile.Size = new System.Drawing.Size(120, 20);
            this.txtFile.TabIndex = 4;
            this.txtFile.Text = "*.dll";

            this.lblFile.Location = new System.Drawing.Point(8, 16);
            this.lblFile.Name = "lblFile";
            this.lblFile.Size = new System.Drawing.Size(144, 16);
            this.lblFile.TabIndex = 5;
            this.lblFile.Text = "Search for files containing:";

            this.lblDirectory.Location = new System.Drawing.Point(8, 96);
            this.lblDirectory.Name = "lblDirectory";
            this.lblDirectory.Size = new System.Drawing.Size(120, 23);
            this.lblDirectory.TabIndex = 3;
            this.lblDirectory.Text = "Look In:";
            // 
            // lstFilesFound
            // 
            this.lstFilesFound.Location = new System.Drawing.Point(152, 8);
            this.lstFilesFound.Name = "lstFilesFound";
            this.lstFilesFound.Size = new System.Drawing.Size(528, 225);
            this.lstFilesFound.TabIndex = 1;

            this.cboDirectory.DropDownWidth = 112;
            this.cboDirectory.Location = new System.Drawing.Point(8, 128);
            this.cboDirectory.Name = "cboDirectory";
            this.cboDirectory.Size = new System.Drawing.Size(120, 21);
            this.cboDirectory.TabIndex = 2;
            this.cboDirectory.Text = "ComboBox1";

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(688, 277);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {

            this.btnSearch,
            this.txtFile,
            this.lblFile,
            this.lblDirectory,
            this.lstFilesFound,
            this.cboDirectory});

            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion

        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void btnSearch_Click(object sender, System.EventArgs e)
        {
            lstFilesFound.Items.Clear();
            txtFile.Enabled = false;
            cboDirectory.Enabled = false;
            btnSearch.Text = "Searching...";
            this.Cursor = Cursors.WaitCursor;
            Application.DoEvents();
            DirSearch(cboDirectory.Text);
            btnSearch.Text = "Search";
            this.Cursor = Cursors.Default;
            txtFile.Enabled = true;
            cboDirectory.Enabled = true;
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            cboDirectory.Items.Clear();
            foreach (string s in Directory.GetLogicalDrives())
            {
                cboDirectory.Items.Add(s);
            }
            cboDirectory.Text = "C:\\";
        }

        void DirSearch(string sDir)
        {
            try
            {
                foreach (string d in Directory.GetDirectories(sDir))
                {
                    foreach (string f in Directory.GetFiles(d, txtFile.Text))
                    {
                        lstFilesFound.Items.Add(f);
                    }
                    DirSearch(d);
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
        }
    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    你给出的代码实际上编译得很好。

    但是,查看错误,您似乎有两个副本:

    C:\Users\jacr\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs
    
    C:\Users\jacr\AppData\Local\Temporary Projects\WindowsFormsApplication1t\Form1.cs
    

    注意第二个目录名称末尾的“t”。

    摆脱其中一个副本,应该没问题。 (您应该可以在 Visual Studio 中将其删除 - 我怀疑您可以同时看到 Form1.cs 文件...)

    【讨论】:

    • 我确实得到了它,现在这个错误正在出现....错误 1 ​​类型“RecursiveSearchCS.Form1”已经包含“组件”C:\Users\jacr\documents 的定义\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 25 49 WindowsFormsApplication1
    • @hanna:老实说,我们不知道这是哪条线路,或者你的项目中发生了什么。我建议你从头开始。正如我所说,我刚刚编译了代码,它很好。听起来您在创建项目或复制文件时可能做了一些奇怪的事情。
    • 我打开了一个新项目,进入解决方案资源管理器并单击 form1.cs,然后将我的代码粘贴到查看代码中......但我仍然收到这些错误,你是如何编译它的? ?
    • @hanna:我刚刚将代码保存到文件中并运行csc Test.cs。问题可能是您将其放入已经存在Form1.Designer.cs 的现有代码中。重新开始,这次只需将文件复制到位并使用“添加现有项目”添加类。恐怕在这里引导您完成这一切会很痛苦,但基本上您不希望 任何 正常的 Visual Studio 提供的东西。启动一个新项目,删除 所有 它启动时使用的源文件,然后按原样添加文件,而不是通过“添加新表单”。
    【解决方案2】:

    使用 partial 关键字扩展您的 class 声明:

    public partial class Form1 : System.Windows.Forms.Form
    

    【讨论】:

      【解决方案3】:

      试试partial关键字like;

      public partial class Form1 : System.Windows.Forms.Form
      

      编辑:看起来你有这个项目的两个副本;

      C:\Users\jacr\AppData\Local\Temporary 项目\WindowsFormsApplication1\Form1.cs

      C:\Users\jacr\AppData\Local\Temporary 项目\WindowsFormsApplication1t\Form1.cs

      只需摆脱其中一个项目,因为您在同一个问题中询问它们。大概是这个原因吧。

      【讨论】:

      • @hanna:“它不工作”是永远足够的信息。请提供更多详细信息。
      • 它显示此代码存在错误 public Form1() { InitializeComponent(); }
      • @Habib 错误 3 'RecursiveSearchCS.Form1.components' 和 'RecursiveSearchCS.Form1.components' C:\Users\jacr\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1 之间的歧义。 cs 46 21 WindowsFormsApplication1
      • @SonerGönül 我删除了另一个副本,我现在得到了这个...错误 1 ​​类型“RecursiveSearchCS.Form1”已经包含“组件”C:\Users\jacr\documents\visual 的定义工作室 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 25 49 WindowsFormsApplication1
      【解决方案4】:

      正如大多数人已经告诉你的那样,你需要在你的类声明中使用partial-keyword。

      public partial class Form1 : System.Windows.Forms.Form
      

      当您使用 Windows 窗体时,Visal Studio 会将您的窗体拆分为多个文件(MyForm.cs 用于您的代码,MyForm.Designer.cs 用于您的 UI 元素的自动生成代码,有时还有一个与之配套的资源文件)。由于该类被拆分为多个文件,因此需要partial-关键字来告诉编译器在最终确定该类之前继续查找更多文件。

      更多关于partial-关键字的信息可以在这里找到:http://msdn.microsoft.com/en-us/library/wa80x488%28v=vs.80%29.aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-02
        • 1970-01-01
        • 2020-08-21
        • 1970-01-01
        相关资源
        最近更新 更多