在解决方案中找不到自定义用户控件?

时间:2012-06-22 17:13:38

标签: c# winforms

我创建了一个简单的Sudoku应用程序,其中每个3x3正方形都是一个用户控件,这个框架代码在CellBlock.Designer.cs中,只有CellBlock.cs中自动生成的代码:

namespace Sudoku
{
    partial class CellBlock
    {

        private System.ComponentModel.IContainer components = null;

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


        private void InitializeComponent()
        {
            this.CellOne = new System.Windows.Forms.MaskedTextBox();
            this.CellFour = new System.Windows.Forms.MaskedTextBox();
            this.CellFive = new System.Windows.Forms.MaskedTextBox();
            this.CellSix = new System.Windows.Forms.MaskedTextBox();
            this.CellTwo = new System.Windows.Forms.MaskedTextBox();
            this.CellThree = new System.Windows.Forms.MaskedTextBox();
            this.CellSeven = new System.Windows.Forms.MaskedTextBox();
            this.CellEight = new System.Windows.Forms.MaskedTextBox();
            this.CellNine = new System.Windows.Forms.MaskedTextBox();
            this.SuspendLayout();
            // 
            // CellOne
            // 
            this.CellOne.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.CellOne.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.CellOne.Location = new System.Drawing.Point(8, 8);
            this.CellOne.Mask = "0";
            this.CellOne.Name = "CellOne";
            this.CellOne.PromptChar = ' ';
            this.CellOne.Size = new System.Drawing.Size(26, 26);
            this.CellOne.TabIndex = 0;
            this.CellOne.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;

            //CellTwo through CellNine omitted for brevity

        // 
        // CellBlock
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.Controls.Add(this.CellNine);
        this.Controls.Add(this.CellEight);
        this.Controls.Add(this.CellSeven);
        this.Controls.Add(this.CellThree);
        this.Controls.Add(this.CellTwo);
        this.Controls.Add(this.CellSix);
        this.Controls.Add(this.CellFive);
        this.Controls.Add(this.CellFour);
        this.Controls.Add(this.CellOne);
        this.Name = "CellBlock";
        this.Size = new System.Drawing.Size(107, 107);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private System.Windows.Forms.MaskedTextBox CellOne;
    private System.Windows.Forms.MaskedTextBox CellFour;
    private System.Windows.Forms.MaskedTextBox CellFive;
    private System.Windows.Forms.MaskedTextBox CellSix;
    private System.Windows.Forms.MaskedTextBox CellTwo;
    private System.Windows.Forms.MaskedTextBox CellThree;
    private System.Windows.Forms.MaskedTextBox CellSeven;
    private System.Windows.Forms.MaskedTextBox CellEight;
    private System.Windows.Forms.MaskedTextBox CellNine;
}

}

这些文件与主文件Sudoku.cs位于同一解决方案中。我只是通过项目菜单向解决方案添加了一个用户控件。这是Sudoku.Designer.cs中的代码,再次由Visual Studio自动生成。

namespace Sudoku
{
    partial class Sudoku
    {

        private System.ComponentModel.IContainer components = null;

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

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Sudoku));
            this.cellBlock1 = new Sudoku.CellBlock();
            this.cellBlock2 = new Sudoku.CellBlock();
            this.cellBlock3 = new Sudoku.CellBlock();
            this.cellBlock4 = new Sudoku.CellBlock();
            this.cellBlock5 = new Sudoku.CellBlock();
            this.cellBlock6 = new Sudoku.CellBlock();
            this.cellBlock7 = new Sudoku.CellBlock();
            this.cellBlock8 = new Sudoku.CellBlock();
            this.cellBlock9 = new Sudoku.CellBlock(); //errors occur at these lines

}


        private CellBlock cellBlock1;
        private CellBlock cellBlock2;
        private CellBlock cellBlock3;
        private CellBlock cellBlock4;
        private CellBlock cellBlock5;
        private CellBlock cellBlock6;
        private CellBlock cellBlock7;
        private CellBlock cellBlock8;
        private CellBlock cellBlock9;

    }
}

我认为这一切都是正确的,即使为了简洁我省略了一些自动生成的代码。当我构建解决方案时,我得到9个错误,如下所示:     “Sudoku.Sudoku”类型中不存在类型名称“CellBlock”

引用读取的行:this.cellBlock1 = new Sudoku.CellBlock();等。我想也许我需要添加对CellBlock, even though it's within the same solution, but when I click Add Reference`的引用,项目下没有列出任何内容。

2 个答案:

答案 0 :(得分:2)

将您的命名空间重命名为Sudoku以外的新命名空间,然后清理解决方案并重建它。

删除当前的CellBlock控件并重新添加它们。

答案 1 :(得分:1)

我认为这可能是因为您的类Sudoku与您的命名空间Sudoku相同,因此编译器认为类CellBlock中应该有一个名为Sudoku的内部类}。我没有c#运行时和编译器,所以我不能尝试重现。

尝试重新分解命名空间或类名,以便它拥有自己的标识符。