2012-06-07 19 views
1

我在Windows窗體中創建了一個由按鈕和文本框組成的簡單用戶控件。按鈕的點擊事件調整文本框的大小並添加一些文本。我不知道這部分代碼是否相關,但我仍然會將其包含在內。如何在另一個項目中使用Windows窗體用戶控件?類型錯誤拋出?

namespace testUserControl 
{ 
    public partial class UserControl1 : UserControl 
    { 
     public UserControl1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      textBox1.Width = 200; 
      textBox1.Height = 200; 
      textBox1.Text = "this text was added by the button"; 
     } 
    } 
} 

在我想包括在幾個地方該用戶控件的項目,我有,增加了一個標籤頁面的點擊事件的按鈕。我希望標籤頁包含這個自定義用戶控件。然而,當我使用此代碼,我得到一個錯誤,指出:'testUserControl is a namespace but used like a type'

namespace main_project_winform 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      TabPage item = new TabPage("header text"); 
      tabControlCitations.Controls.Add(item); 
      testUserControl u = new testUserControl(); //<!-- error occurs here 
      item.Controls.Add(u); 
     } 
    } 
} 

如何包括,用在我的項目這個自定義用戶控件?

+0

你應該命名你的控件。 – SLaks

+0

.Net公共成員名稱應該是UpperCamelCase。 – SLaks

+0

謝謝你讓我知道。我會確保我遵循相應的標準。 –

回答

3

由於錯誤明確指出,testUserControl是一個命名空間。

該類型爲UserControl1
您還需要一個using語句來導入名稱空間。

+0

謝謝。這當然是一個簡單的錯誤,但我無法弄清楚。也許在一天中太遲。我添加了'使用testUserControl',將我的用戶控件重命名爲'myUserControl',並且在我使用'myUserControl u = new myUserControl()時它正在工作;'再次感謝您。一旦時限到期,我會接受答案。 –

相關問題