[英]How to add a button to a form from another form? C# winforms
我目前正在开发用于餐厅的 POS(销售点)系统。 我制作了一个用户界面,您可以在其中将新菜单项添加到另一个表单上的按钮列表中。 我希望在 1 个表单中输入的详细信息以另一种表单创建一个新的菜单项 Button。 如果这是有道理的。 我还希望用户界面添加按钮名称、类别(例如披萨、意大利面)和价格。 我在 Visual Studio 上使用 c# Winforms。
如果FormA会直接改变FormB的布局,那将是不合适的设计。 最好让 FormA 请求 FormB 添加按钮,或者更简单:显示按钮。
class FormB
{
private Button myButton;
public bool MyButtonVisible
{
get => this.myButton.Visible;
set => this.myButton.Visible = value;
}
...
如果显示按钮需要更多代码:
public void MakeMyButtonVisible()
{
... // thing you have to do before my button becomes visible
this.myButton.Visible = true;
}
用法:
class FormA
{
// apparently FormA knows about FormB:
private FormB { get => ...}
private void ShowButtonOnFormB()
{
// maybe some code to make sure FormB is shown
this.FormB.MakeMyButtonVisible();
}
private void OnMenuItem_ShowMyButton_Clicked(object sender, ...)
{
this.ShowBuffonFormB();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.