C# 带方法的GUI

C# 带方法的GUI,c#,methods,C#,Methods,我从我的控制台应用程序中获取了这段代码,并试图使其与GUI一起工作。我调用的方法不对吗?我想单击OK按钮,在3个标签上分别显示数字的和、差和积。困惑的请帮忙 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using

我从我的控制台应用程序中获取了这段代码,并试图使其与GUI一起工作。我调用的方法不对吗?我想单击OK按钮,在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);
    }
}

}我看到两个大问题:

  • 使用该方法在标签中格式化结果
  • 如果要更新表单上的元素,则不应将
    产品
    方法声明为
    静态
    试试这个:

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

    我看到两大问题:

  • 使用该方法在标签中格式化结果
  • 如果要更新表单上的元素,则不应将
    产品
    方法声明为
    静态
    试试这个:

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

    看起来您想调用
    string.Format
    ,但实际上并没有这样做。尝试
    addLabel.Text=string.Format(“数字之和为{0}.”,num1+num2)同样,我们可以从您的代码中猜出错误,但是如果您将实际的错误消息放在代码中,或者详细描述错误,那么就容易多了!该字符串修复了大多数错误。但是现在我得到了-非静态字段、方法或属性的Numbers2GUI.Form1.addLabel需要一个对象引用这些
    static
    方法都不应该是静态的-删除这三个方法上的
    static
    关键字。这就成功了,今晚看了太多代码。非常感谢我的朋友。看起来你想调用
    string.Format
    ,但实际上你并没有这么做。尝试
    addLabel.Text=string.Format(“数字之和为{0}.”,num1+num2)同样,我们可以从您的代码中猜出错误,但是如果您将实际的错误消息放在代码中,或者详细描述错误,那么就容易多了!该字符串修复了大多数错误。但是现在我得到了-非静态字段、方法或属性的Numbers2GUI.Form1.addLabel需要一个对象引用这些
    static
    方法都不应该是静态的-删除这三个方法上的
    static
    关键字。这就成功了,今晚看了太多代码。非常感谢我的朋友。这个错误对于所有3.text值都是存在的。非静态字段、方法或属性“Numbers2GUI.Form1.addLabel”需要对象引用注意我的第二点,“您的方法不应该是静态的”。从
    Sum
    Difference
    Product
    中删除
    static
    关键字。尝试解决方案的第一部分太快,我忽略了另一半。很抱歉这就成功了,今晚看代码太多了。非常感谢我的朋友。@user2089370没问题,编码快乐!对于所有3.text值,此错误仍然存在。非静态字段、方法或属性“Numbers2GUI.Form1.addLabel”需要对象引用注意我的第二点,“您的方法不应该是静态的”。从
    Sum
    Difference
    Product
    中删除
    static
    关键字。尝试解决方案的第一部分太快,我忽略了另一半。很抱歉这就成功了,今晚看代码太多了。非常感谢我的朋友。@user2089370没问题,编码快乐!