【问题标题】:populating listbox from textbox从文本框填充列表框
【发布时间】:2012-04-19 23:39:22
【问题描述】:

我试图让投资的名称出现在股票列表框中,但代码(我在程序前面使用的)似乎不起作用。

   namespace JamesClemens_FinalProject
 {
public partial class Form1 : Form
{
    ArrayList account;

    public Form1()
    {
        InitializeComponent();
        account = new ArrayList();
    }

    //here we set up our add customer button from the first tab
    //when the information is filled in and the button is clicked
    //the name on the account will be put in the combobox on the second tab
    private void btnAddCustomer_Click(object sender, EventArgs e)
    {
        try
        {
            CustomerAccount aCustomerAccount = new CustomerAccount(txtAccountNumber.Text, txtCustomerName.Text,
            txtCustomerAddress.Text, txtPhoneNumber.Text);
            account.Add(aCustomerAccount);

            cboClients.Items.Add(aCustomerAccount.GetCustomerName());
            ClearText();
        }
        catch (Exception)
        {
            MessageBox.Show("Make sure every text box is filled in!", "Error", MessageBoxButtons.OK);
        }
    }


    private void ClearText()
    {
        txtAccountNumber.Clear();
        txtCustomerName.Clear();
        txtCustomerAddress.Clear();
        txtPhoneNumber.Clear();
    }


    private void cboClients_SelectedIndexChanged(object sender, EventArgs e)
    {

        CustomerAccount custAccount = account[cboClients.SelectedIndex] as CustomerAccount;
 if(custAccount != null)
 {
     txtAccountNumberTab2.Text = custAccount.GetAccountNumber();
     txtCustomerNameTab2.Text = custAccount.GetCustomerName();
     txtCustomerAddressTab2.Text = custAccount.GetCustomerAddress();
     txtCustomerPhoneNumberTab2.Text = custAccount.GetCustomerPhoneNo();
}
    }

这是给我带来麻烦的代码,它一直说“new Stock”不包含带有四个参数的构造函数。

  private void btnAddStock_Click(object sender, EventArgs e)
    {
        try
        {
            Stock aStock = new Stock(txtInvestmentID.Text, txtInvestmentName.Text,      txtInvestmentSymbol.Text,
                int.Parse(txtInvestmentShares.Text));
            account.Add(aStock);
            lstStock.Items.Add(aStock.GetInvestmentName());
            ClearText();
        }
        catch (Exception)
        {
            MessageBox.Show("Make sure every text box is filled in!", "Error", MessageBoxButtons.OK);
        }
    }

有人知道为什么它不让我使用 Stock 类吗?

这是您要求的股票类别。 公共类股票:投资 { //属性 私人双股价格;

    //constructors
    public Stock()
    {
    }

    public Stock(string anInvestmentID, string anInvestmentName, string anInvestmentSymbol,
        int anInvestmentShare, CustomerAccount aCustomer, double aStockPrice)
        : base(anInvestmentID, anInvestmentName, anInvestmentSymbol, anInvestmentShare)
    {
        SetStockPrice(aStockPrice);
    }

   //
    public Stock(string anInvestmentID, string anInvestmentName, string anInvestmentSymbol,
        int anInvestmentShare, CustomerAccount aCustomer, double aStockPrice)
    {
        SetInvestmentID(anInvestmentID);
        SetInvestmentName(anInvestmentName);
        SetInvestmentSymbol(anInvestmentSymbol);
        SetInvestmentShare(anInvestmentShare);
        SetStockPrice(aStockPrice);
    }

    //set accessors
    public void SetStockPrice(double aStockPrice)
    {
        stockPrice = aStockPrice;
    }

    //get accessors
    public double GetStockPrice()
    {
        return stockPrice;
    }

【问题讨论】:

  • 您的 Stock 对象不接受四个参数,而您正在使用四个参数。尝试发布 Stock 类的构造函数。
  • 您的 Stock 对象接受六个参数或零个参数。看起来您的基类 Investment 采用了四个参数。
  • 好吧,我知道我做了什么。我在 Stock 类中设置了一个“stockPrice”,然后在按钮下添加了它。谢谢!

标签: c# listbox


【解决方案1】:

请实际阅读您收到的错误消息;它使编程变得更加容易。

错误信息很清楚:

"new Stock" does not contain a constructor that takes four arguments. 

您的Stock 类中没有一个接受四个参数 的构造函数。

查看您在Stock 类中发布的三个构造函数。计算需要传递给每个构造函数的参数数量。有没有只接受四个参数的?我看到一个不带参数,两个每个都带六个参数。

尝试理解错误消息的实际文本将对您以后编写代码有很大帮助。大多数(不是全部,但大多数)错误消息都有有意义的内容,可以指出实际问题。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多