2012-04-19 132 views
0

我試圖讓投資的名稱出現在股票的列表框中,但代碼(我之前在程序中使用過)似乎沒有工作。從文本框填充列表框

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(); 
} 
    } 

這是給我找麻煩的代碼,它口口聲聲說「新股票」不包含一個構造函數四個參數。

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

沒有人知道它爲什麼不讓我用股票類?

這就是你要求的股票類。 public class Stock:投資 { //屬性 private double 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; 
    } 
+0

您的庫存對象不需要四個參數,並且您使用四個參數。嘗試發佈您的Stock類的構造函數。 – LarsTech 2012-04-19 23:42:06

+0

您的庫存對象需要六個參數或零個參數。看起來你的基礎類投資有四個參數。 – LarsTech 2012-04-19 23:45:59

+0

好吧,我知道我做了什麼。我在Stock類中設置了「stockPrice」,然後在該按鈕下添加了它。謝謝! – 2012-04-19 23:50:36

回答

5

請確實閱讀您收到的錯誤消息;它使編程更容易。

的錯誤信息是很清楚的:

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

您不必構造在Stock類,它四個參數

看看您在Stock課中發佈的三個構造函數。計算您需要傳遞給每個構造函數的參數數量。有沒有隻接受四個參數?我看到一個不需要論據的人,兩個人都有六個論點。

試圖瞭解錯誤消息的實際文本將在未來編寫代碼將幫助你很多。大多數(並非全部,但大多數)錯誤消息具有有意義的內容,指出您實際的問題。 :)