在宣言上缺少部分现代

时间:2013-03-12 06:45:16

标签: c#

我怎样才能纠正我在尝试运行程序时遇到的错误,我在网上找到了这个程序,它似乎是在visual c#2005中编译的,我使用visual c#2010 我在编译之前得到了这两个错误

  

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

     

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

     

32 13 WindowsFormsApplication1

当我尝试使用错误编译它时我得到了这个

  

错误1在声明类型时缺少部分修饰符   'RecursiveSearchCS.Form1';另一种这种类型的部分声明   存在C:\ Users \ jacr \ AppData \ Local \ Temporary   Projects \ 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);
            }
        }
    }
}

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

您提供的代码实际上编译得非常好。

然而,看看错误,你似乎有两份副本:

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文件...)

答案 2 :(得分:1)

尝试使用partial关键字;

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

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

  

C:\用户\ jacr \应用程序数据\本地\临时   项目\ WindowsFormsApplication1 \ Form1.cs中

  

C:\用户\ jacr \应用程序数据\本地\临时   项目\ WindowsFormsApplication1t \ Form1.cs中

只是摆脱其中一个项目,因为你在同一个问题上问他们。可能这就是原因。

答案 3 :(得分:1)

正如大多数人已经告诉过你的那样,你需要在你的班级声明中使用partial - 关键字。

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