【问题标题】:No syntax errors, but program does not produce any results没有语法错误,但程序不产生任何结果
【发布时间】:2019-11-07 21:27:43
【问题描述】:

我必须为班级创建一个程序。我的应用程序需要有 OilLubeCharges()FlushCharges()MiscCharges()OtherCharges()TaxCharges()TotalCharges() 的值返回方法。

它需要有ClearOilLube()ClearFlushes()ClearMisc()ClearOther()ClearFees()的void方法。

目前我的代码没有语法错误,但是当我编译它时它不会计算任何东西。计算、清除和退出按钮也不起作用。任何有关为什么会出现这些问题的帮助将不胜感激。下面是我的代码。

public partial class Form1 : Form
{
    public Form1() 
    {
        InitializeComponent();
    }

    private void CalcButton_Click(object sender, EventArgs e)
    {
        OilLubeCharges();
        FlushCharges();
        MiscCharges();
        OtherCharges();
        TaxCharges();
        TotalCharges();
    }

    private void ClearButton_Click(object sender, EventArgs e)
    {
        oilCheckBox.Checked = false;
        lubeCheckBox.Checked = false;
        radFlushBox.Checked = false;
        tranFlushBox.Checked = false;
        insCheckBox.Checked = false;
        mufCheckBox.Checked = false;
        tireCheckBox.Checked = false;
        partsTextBox.Text = "";
        laborTextBox.Text = "";
        serLabTotalTextBox.Text = "";
        partsTotalTextBox.Text = "";
        taxPartsTextBox.Text = "";
        totalFeesTextBox.Text = "";
    }

    private void ExitButton_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private int OilLubeCharges()
    {
        int total = 0;

        if (oilCheckBox.Checked)
        {
            total += 26;
            serLabTotalTextBox.Text = total.ToString("c");
        }

        if (lubeCheckBox.Checked)
        {
            total += 18;
            serLabTotalTextBox.Text = total.ToString("c");
            return total;
        }
        else
        {
            return total;
        }
    }

    private int FlushCharges()
    {
        int total = 0;

        if (radFlushBox.Checked)
        {
            total += 30;
            serLabTotalTextBox.Text = total.ToString("c");
        }

        if (tranFlushBox.Checked)
        {
            total += 80;
            serLabTotalTextBox.Text = total.ToString("c");
            return total;
        }
        else
        {
            return total;
        }
    }

    private int MiscCharges()
    {
        int total = 0;

        if (insCheckBox.Checked)
        {
            total += 15;
            serLabTotalTextBox.Text = total.ToString("c");
        }

        if (mufCheckBox.Checked)
        {
            total += 100;
            serLabTotalTextBox.Text = total.ToString("c");
        }

        if (tireCheckBox.Checked)
        {
            total += 20;
            serLabTotalTextBox.Text = total.ToString("c");
            return total;
        }
        else
        {
            return total;
        }
    }

    private int OtherCharges()
    {
        int total = 0;
        int parts = 0;
        int labor = 0;

        if (int.TryParse(partsTextBox.Text, out parts))
        {
            partsTextBox.Text = parts.ToString("c");
            total = parts;
        }

        if (int.TryParse(laborTextBox.Text, out labor))
        {
            laborTextBox.Text = labor.ToString("c");
            return total;
        }
        else
        {
            return total;
        }
    }

    private decimal TaxCharges()
    {
        decimal parts = 0;
        decimal partsTax;

        partsTax = parts * .06m;
        taxPartsTextBox.Text = partsTax.ToString("c");
        return partsTax;
    }

    private decimal TotalCharges()
    {
        decimal total = 0;

        total = OilLubeCharges() + FlushCharges() + MiscCharges() + TaxCharges() + OtherCharges();
        totalFeesTextBox.Text = total.ToString("c");
        return total;
    }
}

【问题讨论】:

  • 我会确保计算、清除和退出按钮事件方法(CalcButton_Click、ClearButton_Click、ExitButton_Click)正确连接到 UI 上按钮上的相应点击事件。如果是,在事件中设置断点将帮助您调试实际发生的情况。
  • 通过添加断点 (F9) 和单步调试代码 (F11) 来调试代码。您可能没有获得所有代码,如果没有更多信息,我们将无法提供帮助。
  • 尝试使用断点或旧的 MessageBox.Show() 来验证事件是否被调用。
  • 我应该把这些放在哪里?我尝试了几次不同的时间并弹出一个窗口,但没有任何反应。
  • 特殊的“计算、清除、退出按钮也不起作用”表示事件没有在设计器端注册。

标签: c# visual-studio methods


【解决方案1】:

如上面的 cmets 所述,您的事件很可能没有与您的按钮相关联。您可以通过多种方式做到这一点;通常在设计器中完成。

要确定这是不是这个原因,试试这个:

public Form1() 
{
    InitializeComponent();
    CalcButton.Click += CalcButton_Click;
}

请注意,这假定您的按钮名为“CalcButton”。您也可以在设计器中看到这是否正确。

如果可行,您需要以相同方式或通过在设计器中为按钮选择方法来对其余按钮执行相同操作。

【讨论】:

  • 我相信我找到了部分原因,我有一个大括号关闭公共部分类 Form1 :代码结束前的表格。现在退出按钮有效,但计算和清除按钮无效。
  • 我能够为每个事件重新创建事件处理程序,并且程序似乎按预期工作。感谢您的意见。
猜你喜欢
  • 1970-01-01
  • 2012-01-25
  • 1970-01-01
  • 2014-03-18
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
相关资源
最近更新 更多