GUI与方法

时间:2013-04-15 01:37:01

标签: c# methods

我从我的控制台应用程序中获取此代码,并且我正在尝试使用GUI。我不是说方法正确或什么?我想单击“确定”按钮,并在3个标签上分别显示数字的总和,差异和乘积。困惑。请帮忙。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

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

    private void okButton_Click(object sender, EventArgs e)
    {
        int num1 = 5;
        int num2 = 3;

        Sum(num1, num2);
        Difference(num1, num2);
        Product(num1, num2);

    }

    public static void Sum(int num1, int num2)

    {
        addLabel.Text = ("The sum of the numbers is {0}.", num1 + num2);
    }

    public static void Difference(int num1, int num2)
    {
        differenceLabel.Text = ("The difference of the numbers is {0}.", num1 - num2);
    }
    public static void Product(int num1, int num2)
    {
        double multiply = num1 * num2;
        productLabel.Text = ("The product of the numbers is {0}.", multiply);
    }
}

}

1 个答案:

答案 0 :(得分:3)

我看到两个大问题:

  1. 使用string.Format方法格式化标签中的结果。
  2. 如果您要更新表单上的元素,则 {/ 1}}和Sum方法应该声明为Difference
  3. 试试这个:

    Product