【问题标题】:How to make a form visible or invisible in C# [duplicate]如何在 C# 中使表单可见或不可见 [重复]
【发布时间】:2014-03-06 12:00:39
【问题描述】:

所以我有一个名为 Settings.CS 的 Windows 窗体

什么都没有..代码就这么多

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 SteamBot
{
    public partial class Settings : Form
    {
        public Settings()
        {
            InitializeComponent();
        }
    }
}

然后我有另一个名为 Bot.CS 的 CLASS。我想要做的是,在特定功能之后,我希望设置表单变得可见。在 VB 中它只是 Settings.Visible = true

如何在 C# 中执行此操作?请帮帮我。

【问题讨论】:

  • 怀疑这个问题实际上与使表单可见有关。当然,真正的问题是在需要对象引用的地方使用 type name。 Settings 是 type,而不是对象引用。您不能在 C# 程序中使用 Settings.Visible = true。忘记 VB.NET 语法可能非常困难。

标签: c#


【解决方案1】:

解决方案一:如果要首次显示Settings表单,则需要创建该Form的实例变量并调用ShowDialog()方法显示。 p>

试试这个:

void SpecificFunction()
{
    //--at the end of MyFunction call settings form

    Settings frmSettings=new Settings();
    frmSettings.ShowDialog();    
}

解决方案2:如果要显示之前隐藏的Settings表单,需要按照以下步骤将其设为Visible

第 1 步: 使用 Application.OpenForms[] 集合识别/获取 Settings 表单。
第 2 步: 创建 @987654329 的新实例变量@ 通过将上述步骤中获得的Settings 表单进行强制转换。
步骤 3: 将创建的实例变量上的Show() 方法调用为Show/Display Settings 表单。

试试这个:

void SpecificFunction()
{
    //--at the end of MyFunction call settings form

    Form frmSettings = (Settings) Application.OpenForms["Settings"];
    frmSettings.Show();    
}

【讨论】:

    【解决方案2】:

    我刚刚创建了这个新表单,单击按钮时会显示该表单:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form f2 = new Form();
                f2.Visible = true;
                //f2.Show(); also works
            }
        }
    }
    

    如果你想操作实例,你也可以使用 'this' 试试看

    【讨论】:

      【解决方案3】:

      假设特定功能按钮点击

      private void Button1_Click(Object sender, EventArgs e ) 
      {
         Form1 myForm = new Form1();
         myForm.Show();
      }
      

      将 Form1 替换为您的设置表单的表单名称!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-19
        • 2016-08-15
        • 2012-01-06
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        相关资源
        最近更新 更多