簡體   English   中英

如何實時添加自定義組件?

[英]How to add a custom component in real-time?

好的,我在命名空間My_Namespace下創建了一個自定義組件。 然后代碼中的某處我試圖實時添加我的自定義組件:

public void Some_Method()
{
  My_Namespace.My_Custom_Component my_component;

  my_component.Location = new System.Drawing.Point(100, 100);
  my_component.Name = "my_component";
  my_component.Size = new System.Drawing.Size(380, 380);

  this.Controls.Add(my_component);
}

但是我得到了這個錯誤

錯誤CS0165:使用未分配的局部變量'my_component'

我不明白,因為my_component是在我的方法的開頭聲明的。

誰知道什么是錯的? 否則,如何實時調用自定義組件的好方法?

我的猜測應該是:

My_Namespace.My_Custom_Component my_component = new My_Namespace.My_Custom_Component();

您不能使用未初始化的變量。

my_component為null,您應該在使用它之前對其進行初始化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM