【问题标题】:How to add a button to a form from another form? C# winforms如何将按钮从另一个表单添加到表单? C# 窗体
【发布时间】:2020-08-30 16:05:11
【问题描述】:

我目前正在开发用于餐厅的 POS(销售点)系统。我制作了一个用户界面,您可以在其中将新菜单项添加到另一个表单上的按钮列表中。我希望在 1 个表单中输入的详细信息以另一种表单创建一个新的菜单项 Button。如果这是有道理的。我还希望用户界面添加按钮名称、类别(例如披萨、意大利面)和价格。我在 Visual Studio 上使用 c# Winforms。

【问题讨论】:

    标签: c# winforms pos


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      • 2015-09-01
      • 2014-03-06
      • 2014-07-23
      相关资源
      最近更新 更多