2014-01-08 33 views
0

我正在調試應用程序,我正在建立一個窗體(基於System.Windows.Form的一個非常小的擴展),這是打算在一個構造函數,並創建一個新的參數控制每個參數在構造函數。爲什麼我的控件中只有一個被添加到此表單中?

我現在的問題是,由於某種原因,我的ParameterControls被添加到窗體中,但只有第一個添加在操作結束時可見。

有問題的代碼和支持方法;如下:

class ConstructorDialog : Namespace.Forms.Form 
{ 
    protected void InitializeInterface() 
    { 
     if (this.TargetType == null) 
     { 
      throw new InvalidOperationException("Cannot GenerateFields for ConstructorDialog. ConstructorDialog TargetType is null."); 
     } 
     else if (this.TargetConstructor == null) 
     { 
     } 
     else 
     { 
      foreach(ParameterInfo Parameter in this.TargetConstructor.GetParameters()) 
      { 
       try 
       { 
        ParameterControl NewParameterControl = new ParameterControl(Parameter); 
        NewParameterControl.Location = new Point(0, 30 + (30 * Parameter.Position)); 
        this.AddControl(NewParameterControl); 
        continue; 
       } 
       catch (Exception e) 
       { 
       } 
      } 
      return; 
     } 
    } 
} 

class Namespace.Forms.Form : System.Windows.Forms.Form 
{ 
    public Control AddControl(Control Control) 
    { 
     if (Control == null) 
      throw new InvalidOperationException("Form cannot AddControl. Control is null."); 
     else 
     { 
      this.Controls.Add(Control); 
      return Control; 
     } 
    } 
} 


class Namespace.Debugging.ParameterControl : Namespace.Forms.UserControl 
{ 
    protected void InitializeInterface() 
    { 
     if (this.TargetParameter == null) 
     { 
      throw new InvalidOperationException("Cannot InitializeInterface for ConstructorParameterControl. ConstructorParameterControl TargetParameter is null."); 
     } 
     else 
     { 
      this.Controls.Clear(); 

      this.AddLabel(this.TargetParameter.Name + "_Label", this.TargetParameter.Name, new Point(25,0)); 

      return; 
     } 
    } 
} 

class Namespace.Forms.UserControl : System.Windows.Forms.UserControl 
{ 
    public Label AddLabel(String LabelName, String LabelText, Point Location) 
    { 
     if (String.IsNullOrEmpty(LabelName)) 
      throw new ArgumentNullException(); 
     else if (String.IsNullOrEmpty(LabelText)) 
      throw new ArgumentNullException(); 
     else 
     { 
      Label NewLabel = new Label(); 

      NewLabel.Name = LabelName; 
      NewLabel.Text = LabelText; 
      NewLabel.Location = Location; 

      return this.AddLabel(NewLabel); 
     } 
    } 

    public Label AddLabel(Label Label) 
    { 
     if (Label == null) 
      throw new ArgumentNullException(); 
     else 
     { 
      this.Controls.Add(Label); 
      return Label; 
     } 
    } 
} 

我的形式擴展仍處於起步階段,所以它很可能是我忽略了一些(尤其是因爲我的知識形式是唯一的徒弟,值得),但這個操作起來很簡單,在我評估應該工作。

一些調試信息:

The controls are being added to the base 'Controls' collection. 
The positions of the controls are being set to what they ought to, so it is not a matter of them overlapping. 
No exceptions are encountered during execution. 
+2

控件的寬度和高度是否設置爲正值?他們的可見屬性是否正確? –

+0

我沒有手動更改後續方法中這些字段的值。在這些控件的構建中發生的所有事情都是創建一個新標籤並設置文本...我想我可以強制這些字段上的值,儘管... – DigitalJedi805

+0

在沒有進入太遠之後,我遇到了這個問題,如你可以評估。 – DigitalJedi805

回答

1

至於建議的@sysexpand,我手動設置ParameterControl對象的高度和寬度,以及設置爲「Visible」屬性設置爲true,似乎已經解決了問題。

我對此的評估是,通過在ParameterControl是其父代成員之前設置這些變量,當控件添加到其父代時,這些變量將被覆蓋。

+3

你現在可以檢查你自己的答案:)。這是允許的。實際上,你已經做了一件好事,告訴社區你遇到了什麼困難,然後解決問題。你也可以在一行中提到@sysexpand說你應該試着在一小時前看看這3個屬性,只是爲了良好的團契:) –

+1

你能否爲第一個控件總是會出現的原因以及尾隨N -1控件只會顯示,如果你將他們的3個屬性(寬度,高度和可見)設置爲適當的值?只是想知道和其他讀者也會想知道。如果你很清楚,請寫一些想法。如果它不是很清楚,那麼我相信你很想知道爲什麼你自己... –

+0

我很樂意跟進這些評論。我在工作中,而且在下一個小時左右與社區分享時沒有合適的位置:P – DigitalJedi805

相關問題