I’ve created a simple user control in Windows Forms that consists of a button and a textbox. The click event of the button resizes the text box and adds some text. I don’t know if this part of the code is relevant, but I’ll include it anyway.
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";
}
}
}
In the project where I’m trying to include this user control in several places, I have a button with a click event that adds a tab page. I want the tab page to include this custom user control. However, when I use this code, I get an error stating: '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);
}
}
}
How do I include and use this custom user control in my project?
As the error clearly states,
testUserControlis a namespace.The type is
UserControl1.You will also need a
usingstatement to import the namespace.