【发布时间】: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.IBa或a.Num1一样在a中调用您的属性。 -
我编辑了 this.aMtBox.Text = a.withDrawl;
-
@DourHighArch 我的尝试,或最终目标,是因为我点击撤回它会撤回我在文本框中输入的数字
-
我怀疑你的事件没有连接好。对于提款按钮,请确保您的构造函数中有
withdrawl.Click += withdrawl_click;,如果它还没有被设计者连接起来。