使代码块在c#winform中多次运行
我正在用c#编写一种简单的编程语言,允许用户在文本框中输入命令并在屏幕上绘制形状,我编写了一种方法来实现这一点。我遇到的麻烦是,该方法一次只能绘制一个形状,即使我给它两个命令使代码块在c#winform中多次运行,c#,winforms,C#,Winforms,我正在用c#编写一种简单的编程语言,允许用户在文本框中输入命令并在屏幕上绘制形状,我编写了一种方法来实现这一点。我遇到的麻烦是,该方法一次只能绘制一个形状,即使我给它两个命令 public void RunProgram() { try { string[] spilt = cp.UserCMD(textInput).Split('(', ')', ','); // spilts on brackets to get numbs i
public void RunProgram()
{
try
{
string[] spilt = cp.UserCMD(textInput).Split('(', ')', ','); // spilts on brackets to get numbs inside of brackets
foreach (string cmd in spilt) //spilt string on brackets and comma
{
if (cp.ValidCommand(textInput) == true) //checks if command is valid
{
if (cp.FunctionCMD(textInput) == "reset".ToLower())
{
x = 0;
y = 0;
}
else if (cp.FunctionCMD(textInput) == "moveTo".ToLower())
{
x = Int32.Parse(spilt[1]); //gets x and y from 2nd and 3rd element of spilt array. 1st item is command
y = Int32.Parse(spilt[2]);
}
else if (cmdInput == "clear".ToLower())
{
ClearForm();
}
}
if (cp.FunctionCMD(textInput) == "circle".ToLower())
{
radius = Int32.Parse(spilt[1]); ////gets radius from 2nd element of spilt array. 1st item is command
}
if (cp.FunctionCMD(textInput) == "rectangle".ToLower())
{
width = Int32.Parse(spilt[1]); //gets width and height from 2nd and 3rd element of spilt array. 1st item is command
height = Int32.Parse(spilt[2]);
}
if (cp.FunctionCMD(textInput) == "triangle".ToLower())
{
side1 = Int32.Parse(spilt[1]); //gets sides 2nd and 3rd and 4th element of spilt array. 1st item is command
side2 = Int32.Parse(spilt[2]);
side3 = Int32.Parse(spilt[3]);
}
}
Graphics g;
g = Graphics.FromImage(drawOutput);
Pen pen = new Pen(Color.Black, 5);
if (cp.ValidCommand(textInput) == true) // check if command is valid
{
if (cp.FunctionCMD(textInput) == "circle".ToLower())
{
circle.drawCircle(radius, pen, g, x, y); // draw circle using x and y from spilt array
setImage(g);
}
if (cp.FunctionCMD(textInput) == "rectangle".ToLower())
{
rect.drawRectangle(width, height, pen, g, x, y); // draw rect from width and height from spilt array
setImage(g);
}
if (cp.FunctionCMD(textInput) == "triangle".ToLower())
{
tri.drawTriangle(side1, side2, side3, pen, g); // draw triangle from 3 different sides from spilt array
setImage(g);
}
}
else
{
MessageBox.Show("You have a syntax error, Please check your command"); //if command is not valid then throw
}
}
catch (IndexOutOfRangeException e1)
{
MessageBox.Show("You have a syntax error, Please check your parameters \nError: \n" + e1); // displays messagebox if correct parameters are not given
}
}
上面的代码就是正在调用的方法。代码是当我在文本框中的行中循环时
if (cmdInput == "run".ToLower())
{
foreach (var line in InputTxtBox.Lines)
{
System.Diagnostics.Debug.WriteLine(line);
RunProgram();
}
}
if (cmdInput != "run".ToLower())
{
textInput = CMDBox.Text;
RunProgram();
}
如果用户键入:
圆圈(100)
矩形(100100)
预期的结果应该是在屏幕上画一个圆和一个矩形,但目前只画一个圆,而不是矩形。我认为问题在于每个循环的问题。首先确保文本框已启用多行
foreach(inputXtbox.Lines.Count()中的var line)
应循环查看文本框中输入的行数
foreach (var line in InputTxtBox.Lines.Count())
{
System.Diagnostics.Debug.WriteLine(line);
RunProgram();
}
它是在画第一个形状还是第二个形状?它只是在画第一个形状在你的
foreach
中,你的line
是如何分配给你的RunProgram
方法中引用的每个命令的?不是,我的印象是foreach会对文本框中的每一行运行该方法,该命令来自不同的方法