# int Convert.Tolnt32(string value) (+ 18過載) 將數字的指定字串表示形式轉換為等效的32位有符號整數。 異常: ## 標題System.FormatExce
int Convert.Tolnt32(string value) (+ 18過載) 將數字的指定字串表示形式轉換為等效的32位有符號整數。 異常: ## 標題System.FormatExceptionSystem.OverflowException 求解答
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 PFD { public partial class 股票 : Form { private Stock myStock; Random r; private EventHandler myStock_OnStockFall; public 股票() { InitializeComponent(); int price= Convert.ToInt32(txtStartPrice.Text);//編譯說我此處錯誤 myStock = new Stock(price); r = new Random(); } class StockEventArgs : EventArgs { int stockPrice; public StockEventArgs(int stockPrice) { this.stockPrice = stockPrice; } public int StockPrice { get { return StockPrice; } } } class Stock { public event EventHandler OnStockChange; public event EventHandler OnStockRise; public event EventHandler OnStockFall; private int stockPrice; public Stock(int price) { stockPrice = price; } //當股票 價格改變 觸發事件; public int Price { set { decimal oldPrice = stockPrice; stockPrice = value;
if (OnStockChange != null && (oldPrice != value)) { OnStockChange(this, new StockEventArgs(stockPrice)); } if (OnStockFall != null && (oldPrice > value)) { OnStockChange(this, new StockEventArgs(stockPrice)); } else if (OnStockRise != null && (oldPrice < value)) { OnStockChange(this, new StockEventArgs(stockPrice)); } } get { return stockPrice; } } } private void btnOpened_Click(object sender, EventArgs e) { myStock.OnStockChange += new EventHandler<StockEventArgs>(Change); timer1.Enabled = true; } private void btnBuying_Click(object sender, EventArgs e) { myStock.OnStockFall += new EventHandler<StockEventArgs>(Fall); } private void btnSelling_Click(object sender, EventArgs e) { myStock.OnStockRise += new EventHandler<StockEventArgs>(Rise); } void Change(object sender, StockEventArgs e) { this.txtCurPrice.Text = myStock.Price.ToString(); rtbMessages.AppendText("\n當前股份:" + myStock.Price); } void Fall(object sender, StockEventArgs e) { int buy = Convert.ToInt32(txtBuy.Text); if (myStock.Price < buy) { rtbMessages.Text += "\n+買人,賣入價:" + myStock.Price; myStock.OnStockFall -= new EventHandler<StockEventArgs>(Fall); txtBuy.Text = ""; } void Rise(object sender, StockEventArgs e) { int sell = Convert.ToInt32(txtSell.Text); if (myStock.Price > sell) { rtbMessages.Text += "\n-買出,賣出價:" + myStock.Price; myStock.OnStockRise -= new EventHandler<StockEventArgs>(Rise); txtSell.Text = ""; } } private void timer1_Tick(object sender, EventArgs e) { int nowPrice; if (txtCurPrice .Text == "") nowPrice = Convert.ToInt32(txtStartPrice .Text ); else nowPrice = Convert.ToInt32(txtCurPrice .Text ); int change = r.Next(-3, 4); myStock.Price = nowPrice + change; } }
} } void Rise(object sender, StockEventArgs e) { int sell = Convert.ToInt32(txtSell.Text); if (myStock.Price > sell) { rtbMessages.Text += “\n-買出,賣出價:” + myStock.Price; myStock.OnStockRise -= new EventHandler(Rise); txtSell.Text = “”; } }
private void timer1_Tick(object sender, EventArgs e)
{
int nowPrice;
if (txtCurPrice .Text == "") nowPrice = Convert.ToInt32(txtStartPrice .Text );
else nowPrice = Convert.ToInt32(txtCurPrice .Text );
int change = r.Next(-3, 4);
myStock.Price = nowPrice + change;
}
}
}