问题描述
我得到了这段代码并想对其进行测试,但是我对C#还是陌生的,我不知道为什么它没有运行。
我键入了诸如Rectangle 200,100或Rectangle 200,100,100之类的东西,但除了弹出框显示“输入有效命令”外,什么都没有显示。
namespace ASEShapeTesting1
{
public partial class Form1 : Form
{
Shape shape;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
}
private void EnterButton_Click(object sender,EventArgs e)
{
shape = null;
if (!Shape.TryParse(InputRichTextBox.Text,out shape))
{
MessageBox.Show("Enter a valid command.");
return;
}
pictureBoxOutput.Invalidate();
}
private void pictureBoxOutput_Paint(object sender,PaintEventArgs e)
{
if (shape != null)
{
e.Graphics.Clear(Color.White);
e.Graphics.SmoothingMode = System.Drawing.drawing2d.SmoothingMode.AntiAlias;
using (Pen pn = new Pen(Color.Black,5))
{
switch (shape.ThisShape)
{
case Shape.Shapes.Ellipse:
e.Graphics.DrawEllipse(pn,shape.X,shape.Y,shape.Width,shape.Height);
break;
case Shape.Shapes.Rectangle:
e.Graphics.DrawRectangle(pn,shape.Height);
break;
case Shape.Shapes.Line:
//Note: the Width and Height properties play here the X2 and Y2 roles.
e.Graphics.DrawLine(pn,shape.Height);
break;
}
}
}
}
}
}
shape类具有以下代码,这就是我在整个程序中所拥有的:
namespace ASEShapeTesting1
{
class Shape
{
#region enums.
public enum Shapes
{
Ellipse,Rectangle,Line
}
#endregion
#region Constructors
public Shape()
{ }
public Shape(Shapes shape,int x,int y,int width,int height)
{
ThisShape = shape;
X = x;
Y = y;
Width = width;
Height = height;
}
#endregion
#region Properties
public Shapes ThisShape { get; set; } = Shapes.Ellipse;
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
#endregion
#region Methods
public static bool TryParse(string input,out Shape result)
{
result = null;
if (string.IsNullOrEmpty(input))
return false;
var cmd = input.Split(' ');
var validCmds = Enum.GetNames(typeof(Shapes));
if (cmd.Length < 5 || !validCmds.Contains(cmd[0],StringComparer.OrdinalIgnoreCase))
return false;
int x,y,w,h;
if (!int.TryParse(cmd[1],out x) ||
!int.TryParse(cmd[2],out y) ||
!int.TryParse(cmd[3],out w) ||
!int.TryParse(cmd[4],out h))
return false;
if (w <= 0 || h <= 0)
return false;
var shape = (Shapes)validCmds.ToList().Findindex(a => a.Equals(cmd[0],StringComparison.OrdinalIgnoreCase));
result = new Shape(shape,x,h);
return true;
}
#endregion
}
}
我也尝试测试其他2种形状,但是什么也没有出现。我不知道为什么它不会运行。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)