【问题标题】:windows forms, methods, and buttonswindows窗体、方法和按钮
【发布时间】:2013-10-06 05:51:02
【问题描述】:

我正在 Windows 窗体上测试银行帐户。容易上手的东西。在我的dep_Click 方法中,我有一种方法,如果我点击它,我会在aMtBox 中执行我想要的代码。我想进一步扩展并尝试创建一个具有一些属性等的 BankAccount 类。为此,我使用了我的withdrawl_Click 方法来尝试这样做。我看到的一切看起来都可以在控制台应用程序中使用,除了我不知道如何调用 withdrawl_Click 中的方法;也就是说,我是否能够使用我编写的代码。

代码是否完全错误并且在 Windows 窗体应用程序中有什么不同?或者是否有一个我/没有理解的概念。 请向我解释清楚。

编辑:我更新了我的代码,但它仍然给我一个错误,提示我在底部附近的 withDrawl 方法中需要返回类型。还是不确定。

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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
    BankAccount a = new BankAccount(iBa, num1);

    public Form1()
    {
        InitializeComponent();
        decimal iBa = 300.00m; // needs fixed to be more universal
        this.aMtBox.Text = iBa.ToString();
    }


    private void dep_Click(object sender, EventArgs e)
    {
        try
        {
            decimal num1 = 0.00m;
            decimal iBa = 300.00m;
            num1 = Convert.ToDecimal(this.depBox.Text);
            decimal total = num1 + iBa;
            this.aMtBox.Text = total.ToString();
        }
        catch
        {
            MessageBox.Show("ERROR", "Oops, this isn't good!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    private void withdrawl_Click(object sender, EventArgs e)
    {

    }

    public class BankAccount
    {
        decimal iBa;
        decimal num1;
        decimal withT;

        public decimal IBa
        {
            get
            {
                return iBa;
            }
        }
        public decimal Num1
        {
            get
            {
                return num1;
            }
        }
        public decimal Witht
        {
            get
            {
                return withT;
            }
        }

        public withDrawl(decimal n, decimal s )
        {
            iBa = n;
            num1 = s;
            withT = iBa - num1;
        }
    }
}
}

【问题讨论】:

  • 为了提供指示,我们需要知道您要去哪里。请说明您的最终目标是什么。
  • 我看到你已经实例化了BankAccount,但是看起来你没有在任何地方使用它?您需要像 a.IBaa.Num1 一样在 a 中调用您的属性。
  • 我编辑了 this.aMtBox.Text = a.withDrawl;
  • @DourHighArch 我的尝试,或最终目标,是因为我点击撤回它会撤回我在文本框中输入的数字
  • 我怀疑你的事件没有连接好。对于提款按钮,请确保您的构造函数中有withdrawl.Click += withdrawl_click;,如果它还没有被设计者连接起来。

标签: c# winforms


【解决方案1】:

在你的代码中,你是 instantiating 你的第二个班级,BankAccount 有这行:

BankAccount a = new BankAccount();

但是,您没有在任何地方使用它。根据您在原始帖子中所说的内容,要访问第二类中的属性,您可以这样称呼它们:

a.IBa;
a.Num1;
a.withDrawl;

下面是一个非常简单的例子:

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

      private void button1_Click(object sender, EventArgs e)
      {
         string aValue = String.Empty;
         Something a = new Something();
         a.SomeThingElse = aValue;
      }
   }

   public class Something
   {
      public string SomeThingElse
      {
         get { return SomeThingElse; }
         set { SomeThingElse = value; }
      }
   }
}

在上面的示例中,您可以看到我在button1_Click 事件中实例化了我的新类public class Something,然后使用a.SomethingElse = aValue 调用其中的属性,这是我在中定义的string最初的班级。在本例中为aValue="",因为它被初始化为String.Empty,即'""'。

另外,附带说明一下,您可能希望将第二个类的抽象级别设置为public,因为如果不声明它public,它将默认为private(这不会导致此问题案例 - 这只是要记住的事情)。

我正在添加另一个示例 - 这个示例使用您提供的一些代码:

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 WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
     public Form1()
     {
         InitializeComponent();
     }

     private void dep_Click(object sender, EventArgs e)
     {
        try
        {
            decimal newBalance;
            BankAccount bankAccount = new BankAccount();  // Instantiate your class.
            bankAccount.DepositAmount = Convert.ToDecimal(depAmt.Text); 
            newBalance = (Convert.ToDecimal(currentBalance.Text)) + bankAccount.DepositAmount;
            currentBalance.Text = newBalance.ToString();
        }

        catch
        {
            MessageBox.Show("ERROR", "Oops, this isn't good!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

public class BankAccount
{
    decimal depositAmount;

    public decimal DepositAmount
    {
        get { return depositAmount; }
        set { depositAmount = value; }
    }
  }
}

【讨论】:

  • 感谢举报,评论已被删除,但下次请不要喂巨魔。只是默默地标记帖子。它被删除。
  • 你明白了,约翰。感谢您的提示!
【解决方案2】:
private void withdrawl_Click(object sender, EventArgs e)
{
    this.aMtBox.Text = a.withDrawl().ToString();
}

这将使它编译 - 它将值 300 从 WithDrawl() 方法传递到您的 aMtBox 文本框。在 depBox 文本框中输入一个值,例如 50,然后单击 dep 按钮将在 aMtBox 文本框中产生一个值 350。

另外,如果您单击名为withdrawl 和dep 的按钮但没有任何反应:尝试在设计器中打开表单并双击每个按钮。您应该进入withdrawl_Click、dep_Click 事件处理程序。

编辑: 响应Withdrawl方法返回类型错误:

public decimal withDrawl(decimal n, decimal s )
{
    iBa = n;
    num1 = s;
    withT = iBa - num1;
    return withT;
}

【讨论】:

    猜你喜欢
    • 2013-06-05
    • 2020-09-21
    • 1970-01-01
    • 2012-08-08
    • 2010-09-16
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多