簡體   English   中英

從文本框填充列表框

[英]populating listbox from textbox

我試圖將投資的名稱顯示在股票列表框中,但該代碼(我在程序中較早使用的代碼)似乎不起作用。

   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類?

這是您要求的股票類別。 public class Stock:Investment {//將私人雙重stockPrice屬性;

    //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;
    }

請實際閱讀您收到的錯誤消息。 它使編程容易得多。

錯誤消息非常清楚:

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

Stock類中沒有帶四個參數的構造函數

看一下您在Stock類中發布的三個構造函數。 計算需要傳遞給每個構造函數的參數數量。 是否只有一個接受四個參數? 我看到一個不帶參數的,兩個不帶參數的兩個。

嘗試理解錯誤消息的實際文本將在將來編寫代碼時為您提供很多幫助。 大多數(不是全部,但大多數 )錯誤消息都包含有意義的內容,這些內容可以使您指出實際問題。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM