我在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);
}
}
}
如何包括,用在我的項目這個自定義用戶控件?
你應該命名你的控件。 – SLaks
.Net公共成員名稱應該是UpperCamelCase。 – SLaks
謝謝你讓我知道。我會確保我遵循相應的標準。 –