【问题标题】:Error: Car.Form1 is inaccessible due to its protection level C#错误:Car.Form1 由于其保护级别 C# 而无法访问
【发布时间】:2014-07-07 15:02:38
【问题描述】:

我无法找出这段代码中的 2 个错误,有人可以帮帮我吗?

错误 1:Car.Form1 由于其保护级别而无法访问

错误 2:找不到类型或命名空间名称“Point”(您是否缺少 using 指令或程序集引用?

感谢您的帮助!

主类

namespace Car
{
public partial class Form1 : Form
{
    int x = 0;
    int y = 500;
    int turn = 0;

    public Form1()
    {
        InitializeComponent();
    }
    public void Form1_KeyDown(object sender, KeyEventArgs e)
    {


            if (e.KeyCode == Keys.Right)
            {
                x += 32;
                turn = 1;
            }
            else if (e.KeyCode == Keys.Left)
            {
                x -= 32;
                turn = 2;
                Wheel1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
                Wheel2.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
            }
            else
            {
                turn = 0;
            }
            if (x <= -250)
            {
                x = 1040;
            }
            else if (x >= 1041)
            {
                x = -250;
            }


    }

    public void Form1_Load(object sender, EventArgs e)
    {

    }
}
}

汽车类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Car
{
     public class Car : Form
     {
        public Car(Form1 form1)
        {
            form1.Car.Location = new Point(form1.x, form1.y);
        }
     }
}

【问题讨论】:

  • 您知道您正在向表单添加表单吗?
  • 您拼错了错误消息。你写的是你认为它所说的,而不是它真正所说的。它抱怨的是 Form1.Car,而不是 Car.Form1。选择好的标识符名称是重要的

标签: c#


【解决方案1】:

根据form1.Car.Location,我相信Car 可以控制您的表单。默认情况下,所有控件都是private,并且在表单类之外不可见。快速解决方案:在设计器中选择您的Car 控件,然后转到其属性。找到属性Modifiers 并将其更改为public。这将为您的控件生成公共字段,但它也会破坏您的表单的封装。更好地在表格上创建将汽车移动到新位置的方法。比如:

public void MoveCar(点位置) { Car.Location = 位置; }

PointSystem.Drawing 命名空间中声明。

【讨论】:

    猜你喜欢
    • 2014-08-15
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 2015-09-26
    • 2011-09-01
    相关资源
    最近更新 更多