我正在尝试从用户那里获取输入,以将x,y坐标设置为DrawEllipse函数,当我将值硬编码到其中时,它会在屏幕上绘制一个圆圈,但是当我尝试传递一个int来自用户的输入是无效的。
我正在尝试制作一个程序来响应用户的命令,例如用户可以输入“绘制形状x y”。 当检测到空格时,将字符串拆分为一个字符串。
public partial class Form1 : Form
{
String input;
Bitmap drawOutput;
String command, command2, command3;
int x, y;
public Form1()
{
InitializeComponent();
drawOutput = new Bitmap(OutputBox.Size.Width, OutputBox.Size.Height);
OutputBox.Image = drawOutput;
}
private void Form1_Load(object sender, EventArgs e)
{
Graphics g;
g = Graphics.FromImage(drawOutput);
Pen mypen = new Pen(Color.Black);
g.Clear(Color.White);
g.Dispose();
}
private void CMDBox_TextChanged(object sender, EventArgs e)
{
input = CMDBox.Text;
}
private void ExecuteBtn_Click(object sender, EventArgs e)
{
String[] spilt = input.Split(' ');
foreach (String words in spilt)
{
command = spilt[0];
command2 = spilt[1];
command3 = spilt[2];
x = Int32.Parse(command2);
y = Int32.Parse(command3);
Graphics g;
g = Graphics.FromImage(drawOutput);
Pen pen = new Pen(Color.Black, 5);
if (input == command)
{
g.DrawEllipse(pen, 0, 0, x, y);
OutputBox.Image = drawOutput;
g.Dispose();
}
}
}
}
有人可以帮忙吗?