C# 如何引用自定义控件?
我制作了一个自定义的C# 如何引用自定义控件?,c#,winforms,C#,Winforms,我制作了一个自定义的文本框和一个自定义的按钮。我的按钮有一个click事件,因此当单击按钮时,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";
文本框
和一个自定义的按钮
。我的按钮有一个click事件,因此当单击按钮时,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在当前上下文中不存在”
这个文本框被明确地称为“buyPrice”,我没有输入错误或任何东西。标签buyPrice是在单击按钮时创建的
买价是这样宣布的:(希望这更清楚)
您需要将自定义控件的引用添加到您的xaml.cs和(希望您的自定义控件继承了Textbox类)中(如果是单独的项目,则需要添加引用,并且需要在使用语句的帮助下包括命名空间,就像其他系统和第三方控件集成一样).您在哪里声明
buyPrice
?我在单击eventargs时在一个私有按钮内声明buyPrice。这是Windows窗体、WPF、ASP.NET吗?这是一个c#Windows窗体在将来,请在标记中提供此类信息。如何使用语句和命名空间将引用添加到我的XAML.cs?将是您的usercontrol命名空间。
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);
}