2014-10-01 30 views
0

我做了一個自定義textBox和一個自定義Button。我爲我的按鈕設置了一個單擊事件,以便單擊此按鈕時,textBox條目將存儲爲一個int值。如何引用自定義控件?

我已經這樣做了,像這樣:

//Save item button 
    Button saveItem = new Button(); 
    saveItem.Size = new Size(135,23); 
    saveItem.Location = new Point(20, 169); 
    saveItem.Text = "Save to items"; 
    saveItem.Name = "saveItem"; 
    Controls.Add(saveItem); 
    // Add a Button Click Event handler 
    saveItem.Click += new EventHandler(saveItem_Click); 
} 


private void saveItem_Click(object sender, EventArgs e) 
{ 
    itemBuy1 = Int32.Parse(buyPrice.Text); //buyPrice is the custom textBox I created 
} 

當我嘗試這樣做,我得到buyPrice.Text出現錯誤消息下紅色的錯誤線「這個名字buyPrice不會在當前的背景下存在的。」

textBox絕對稱爲「buyPrice」,我還沒有輸入錯字或任何內容。標籤buyPrice是在單擊按鈕時創建的。

buyPrice聲明像這樣:(希望這是更清晰)

private void itemToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     TextBox buyPrice = new TextBox(); 
     buyPrice.Size = new Size(100,20); 
     buyPrice.Location = new Point(65,86); 
     buyPrice.Name = "buyPrice"; 
     Controls.Add(buyPrice); 
    } 
+1

你在哪裏聲明'buyPrice'? – 2014-10-01 05:17:35

+0

我在私人按鈕onClick eventargs內聲明買入價格。 – 2014-10-01 05:19:08

+0

這是Windows窗體,WPF,ASP.NET嗎? – 2014-10-01 05:45:23

回答

0

您需要將您的自定義控件的添加引用(需要如果單獨的項目添加引用,並需要包含命名空間的幫助下像其他系統和第三方控件一樣使用語句)集成到您的xaml.cs和您的(希望您的自定義控件繼承了Textbox類)。

+0

如何添加對我的XAML.cs的引用? – 2014-10-01 05:21:09

+0

通過使用語句和命名空間將是您的usercontrol的命名空間。 – 2014-10-01 05:24:32

+0

仍然沒有得到然後發佈您的自定義控制代碼 – 2014-10-01 05:27:27