Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 窗体上的所有控件都不可见_C#_.net_Winforms - Fatal编程技术网

C# 窗体上的所有控件都不可见

C# 窗体上的所有控件都不可见,c#,.net,winforms,C#,.net,Winforms,我遇到了一个奇怪的问题。仅给出这段代码(我将其简化为基本要素),表单将出现,但其上的所有控件都不可见,即使它们各自的Visible属性为true: static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false);

我遇到了一个奇怪的问题。仅给出这段代码(我将其简化为基本要素),表单将出现,但其上的所有控件都不可见,即使它们各自的
Visible
属性为true:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new WaveformWindow());
    }
}

// ...

public partial class WaveformWindow : Form
{
    public WaveformWindow()
    {
        InitializeComponent();
    }
}

// ...

partial class WaveformWindow
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(27, 27);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(35, 13);
        this.label1.TabIndex = 4;
        this.label1.Text = "label1";
        // 
        // WaveformWindow
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(398, 373);
        this.Name = "WaveformWindow";
        this.ShowInTaskbar = false;
        this.Text = "WaveformWindow";
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.Label label1;
}
静态类程序
{
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(新的WaveformWindow());
}
}
// ...
公共部分类WaveOrmWindow:表单
{
公共波弗姆文多()
{
初始化组件();
}
}
// ...
部分类波窗
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.label1=new System.Windows.Forms.Label();
这个.SuspendLayout();
// 
//标签1
// 
this.label1.AutoSize=true;
this.label1.Location=新系统图纸点(27,27);
this.label1.Name=“label1”;
this.label1.Size=新系统图纸尺寸(35,13);
this.label1.TabIndex=4;
this.label1.Text=“label1”;
// 
//波弗姆温多
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(398373);
this.Name=“WaveformWindow”;
this.ShowInTaskbar=false;
this.Text=“WaveformWindow”;
此选项为.resume布局(false);
}
#端区
private System.Windows.Forms.label1;
}

比较非剥离程序的工作版本和损坏版本,唯一更改的文件是表单设计器文件。

将控件添加到表单以使其可见。 看this.Controls.Add(this.label1)下面

        // 
        // WaveformWindow
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(398, 373);
        this.Name = "WaveformWindow";
        this.ShowInTaskbar = false;
        this.Text = "WaveformWindow";
      **this.Controls.Add(this.label1);**
        this.ResumeLayout(false);

Windows窗体生成自动将所有控件添加到窗体的代码。因此,每当你遇到这样的问题时,删除旧控件并再次拖放它。它必须是可见的。(编辑以了解问题)

我使用的大多数GUI工具包都要求您在创建小部件后将其添加到窗口/画布中。winforms是否会自动在类的
private
成员中查找标签并显示它们?还是必须以某种方式将它们添加到窗口/画布中?按照您显示的代码行,我看不到标签被添加到窗体的控件集合中。。这意味着表格上甚至没有标签。。我们知道那条线应该在那里。。但这应该由设计师自动完成。。。您不能确定设计器是否保留了更改后的代码。@Shekhar_Pro:是的,确实如此,设计器必须自动执行。但我认为他可能试图附加设计器代码。因为这从来没有被报道为Windows Designer代码生成的问题,这就是我要说的。。通常的补救办法是删除标签的代码(在设计器代码中),然后在表单(在设计器而非ode中)上再次添加新标签@Shekhar_Pro:完全同意。我从未修改过设计器代码。奇怪的是,设计者未能添加控件。