0

I've run into a bizarre problem. Given only this code (that I've cut down to the bare essentials), the form will appear but all controls on it are invisible, even though their individual Visible properties are 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;
}

Comparing between working and broken versions of the non-stripped program, the only file that changed is the form designer file.

Reinderien
  • 11,755
  • 5
  • 49
  • 77
  • 1
    Most GUI toolkits I've used require that you add your widgets to the window/canvas after creating them. Does winforms automatically find labels among the `private` members of a class and show them? Or must they also be added to the window/canvas somehow? – sarnold Apr 02 '11 at 06:51
  • Along the lines of code you have shown i can't see label being added to Form's control collection.. that means label is NOT even on the form.. its just declared. – Shekhar_Pro Apr 02 '11 at 06:52

1 Answers1

2

Add the controls to the form to make them visible. See... this.Controls.Add(this.label1); below

        // 
        // 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 forms generates the code to add all the controls to forms automatically. So whenever u face such problem, delete the old control and again drag and drop it. It must be then visible. (Edit for understanding the issue)

Marshal
  • 6,551
  • 13
  • 55
  • 91
  • we know that line should be there.. but that should automatically be done by designer... you can't be sure designer kept your changed code. – Shekhar_Pro Apr 02 '11 at 06:55
  • @Shekhar_Pro: Yes thats true, designer must do it automatically. But I think he might have tried to append the designer code. Because this has never been reported as problem of Windows Designer Code Generation – Marshal Apr 02 '11 at 06:56
  • 1
    thats what i am saying.. the usual remedy would be to delete the code (in designer code ) of label and add a new label again on the form (in Designer not ode) – Shekhar_Pro Apr 02 '11 at 06:59
  • @Shekhar_Pro: Completely agree. – Marshal Apr 02 '11 at 07:00
  • I never modified the designer code. That's strange that the designer failed to add the control. – Reinderien Apr 02 '11 at 07:06
  • @Reinderien: Yes its strange.. but I think it wouldn't be a permanent problem. Once in a year you may face this kinda problem. Its when designer gets confused about something. :) – Marshal Apr 02 '11 at 07:19
  • Unfortunately this problem is semi-permanent. I'm posting a new question about it now that I know more about the problem. – Reinderien Apr 02 '11 at 13:23