2011-04-02 34 views
0

我遇到了一個奇怪的問題。由於僅此代碼(即我砍倒在最基本)的形式出現,但其上的所有控件都看不見,即使他們的個人Visible性質爲真:窗體上的所有控件都不可見

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; 
} 

的工作和破碎之間的比較版本的非剝離程序,唯一更改的文件是表單設計器文件。

+1

我使用的大多數GUI工具包都要求您在創建窗口/畫布後將窗口小部件添加到窗口/畫布。 winforms會自動在班級的「私人」成員中找到標籤並顯示它們嗎?或者必須將它們以某種方式添加到窗口/畫布中? – sarnold 2011-04-02 06:51:12

+0

沿着代碼的行,你已經顯示我看不到標籤被添加到窗體的控件集合..這意味着標籤甚至不在窗體上......它剛剛聲明。 – 2011-04-02 06:52:32

回答

2

將控件添加到窗體以使其可見。 請參閱... 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窗體生成的代碼自動將所有控件添加到窗體。所以,當你面對這樣的問題時,刪除舊的控件並再次拖放它。它必須可見。 (編輯理解問題)

+0

我們知道這條線應該在那裏..但是這應該由設計師自動完成......您不能確定設計師是否保留了您更改的代碼。 – 2011-04-02 06:55:03

+0

@Shekhar_Pro:是的,設計師必須自動完成。但我認爲他可能試圖追加設計師代碼。因爲這從來沒有被報告爲Windows設計器代碼生成問題 – Marshal 2011-04-02 06:56:52

+1

多數民衆贊成在說什麼..通常的補救措施是刪除代碼(在設計器代碼中)的標籤,並再次在窗體上添加一個新的標籤(在設計器中不ode) – 2011-04-02 06:59:04

相關問題