无限PaintEvent循环

时间:2019-02-18 17:24:28

标签: c# winforms

我有一个空的绘画事件,有时会开始无限循环。我不知道为什么。我允许用户单击按钮来输入表达式。用户单击确认后,我将开始简化他们输入的表达式,因为我要完成的程序是布尔代数简化程序。确认单击后,将调用某些子例程并完成一些验证。验证以条件语句的形式完成,例如if和else,并且如果表达式无效,则会显示一个消息框,否则将开始进行简化。我在下面显示了确认按钮单击事件和无限循环绘制事件。我不确定为什么有时会无限循环以及为什么有时会起作用。何时无限循环的一个例子是表达式中的第一个字符是'(',但是它没有空值。

确认Btn点击事件:

    private void ConfirmBtn_Click(object sender, EventArgs e)
        {
            string s = "";
            char C = Expression.Text[Expression.Text.Length - 1];
            if (!IsBalanced(Expression.Text))
            {
                MessageBox.Show("Invalid Expression. Sort out brackets", "Error!");
            }
            else if (C == '⊕' || C == '•' || C == '+')
            {
                MessageBox.Show("Invalid Expression. Cannot end on an operator", "Error!");
            }
            else if (UnconnectedOperatorErrorPresent(Expression.Text, NOTs))
            {
                MessageBox.Show("An operator cannot have more Nots over it than any of its adjacent variables.", "Error!");
            }
            else if (UnconnectedBracketError(Expression.Text, NOTs))
            {
                MessageBox.Show("A bracket cannot have more Nots over it than any of its adjacent variables.", "Error!");
            }
            else if (Expression.Text[0] == '(')
            {
                if (NOTs[0] > 0)
                {
                        MessageBox.Show("A bracket at the beginning of the expression cannot have NOTs over it.", "Error!");
                }
            }
            else if (Expression.Text[Expression.Text.Length - 1] == ')')
            {
                if (NOTs[Expression.Text.Length - 1] > 0)
                {
                    MessageBox.Show("A bracket at the end of the expression cannot have NOTs over it.", "Error!");
                }
            }
            else if (Expression.Text.Length > 20)
            {
                MessageBox.Show("Expression has a maximum length of 20 characters.", "Error!");
            }
            else
            {
                for (int i = 0; i < overLines.Length; i++)
                {
                    overLines[i].Text = "";
                }
                int n = 0;
                for (int i = 0; i < NOTs.Count; i++)
                {
                    if (n < NOTs[i])
                    {
                        n = NOTs[i];
                    }
                    for (int y = 0; y < overLines.Length; y++)
                    {
                        if (y < NOTs[i])
                        {
                            overLines[y].Text += "_";
                        }
                        else
                        {
                            overLines[y].Text += " ";
                        }

                    }
                }
                Subroutines m = new Subroutines();
                m.ExpressionLocation(n, Expression, 311, 132);
                SimplifiedExpression = Simplify();
                if (SimplifiedExpression == "0")
                {
                    SimplifiedExpression = "0 or False";
                    NOTsNew.Clear();
                }
                if (SimplifiedExpression == "1")
                {
                    SimplifiedExpression = "1 or True";
                    NOTsNew.Clear();
                }
                for (int i = 0; i < NOTsNew.Count; i++)
                {
                    if (i > SimplifiedExpression.Length)
                    {
                        NOTsNew.RemoveAt(i);
                    }
                }

                SimplifiedExpressionForm SEF = new SimplifiedExpressionForm(Expression, NOTsNew, SimplifiedExpression);
                SEF.Show();
                ResetVariables();

            }
        }

按钮和输出表达式的标签在面板顶部。 这是绘画事件:

            private void BackgroundPanel_Paint(object sender, PaintEventArgs e)
            {

            }

我认为,与其在其中包含一个if if然后一个if,不如在一个if条件中包含初始条件和if包含在if内部的条件,这会更好,例如:

        else if (Expression.Text[0] == '(' && NOTs[0] > 0)
        {
               MessageBox.Show("A bracket at the beginning of the expression cannot have NOTs over it.", "Error!");

        }

而不是:

        else if (Expression.Text[0] == '(')
        {
            if (NOTs[0] > 0)
            {
                    MessageBox.Show("A bracket at the beginning of the expression cannot have NOTs over it.", "Error!");
            }
        }

但是为什么在else if中的if如果不起作用/会导致绘画事件无限循环?

0 个答案:

没有答案