C# 错误:Car.Form1由于其保护级别为C而无法访问#
我无法找出这段代码中的两个错误。有人能帮我吗 错误1:Car.Form1由于其保护级别而无法访问 错误2:找不到类型或命名空间名称“Point”(是否缺少using指令或程序集引用 谢谢你的帮助 主类C# 错误:Car.Form1由于其保护级别为C而无法访问#,c#,C#,我无法找出这段代码中的两个错误。有人能帮我吗 错误1:Car.Form1由于其保护级别而无法访问 错误2:找不到类型或命名空间名称“Point”(是否缺少using指令或程序集引用 谢谢你的帮助 主类 namespace Car { public partial class Form1 : Form { int x = 0; int y = 500; int turn = 0; public Form1() { InitializeCom
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)
{
}
}
}
根据
form1.Car.Location
我相信Car
是表单上的一些控件。默认情况下,所有控件都是private
,在表单类之外不可见。快速解决方案:在designer中选择Car
控件,并转到其属性。找到属性修饰符
并将其更改为public
。这将为控件生成公共字段,但也会破坏表单的封装。更好的方法是在表单上创建方法,以便将汽车移动到新位置。类似于:
公共车辆(点位置)
{
汽车。位置=位置;
}
是在
系统中声明的。绘图命名空间。您知道您正在向表单添加表单吗?您拼写错误了错误消息。您写的是您认为它说的内容,而不是它真正说的内容。它抱怨Form1.Car,而不是Car.Form1。选择好的标识符名称很重要。
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);
}
}
}