Daniel Lip
Daniel Lip

Reputation: 11335

Custom control not available in toolbox

I have a new project i created type: Class Library

When creating the new project i selected: Visual C# > Windows > Windows Forms Control Library

Then in the new project name properties i see under Output type: Class Library

I deleted the file UserControl1.cs and then did: Add > Class

In the new class i added this code for the testing:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace CustomControl
{
    class ExtdTextBox : TextBox
    {
        #region Member Variables
        Color waterMarkColor = Color.Gray;
        Color forecolor;
        Font font;
        Font waterMarkFont;
        string waterMarkText = "Your Text Here";
        #endregion 
        #region Constructor
        public ExtdTextBox()
        {
            base.Text = this.waterMarkText;
            this.forecolor = this.ForeColor;
            this.ForeColor = this.waterMarkColor;
           this.font = this.Font;
            //event handlers
            this.TextChanged += new EventHandler(ExtdTextBox_TextChanged);
            this.KeyPress += new KeyPressEventHandler(ExtdTextBox_KeyPress);
            this.LostFocus += new EventHandler(ExtdTextBox_TextChanged);
        }
        #endregion
        #region Event Handler Methods
        void ExtdTextBox_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.Text))
            {
                this.ForeColor = this.forecolor;
                this.Font = this.font;
            }
            else
            {
                this.TextChanged -= new EventHandler(ExtdTextBox_TextChanged);
                base.Text = this.waterMarkText;
                this.TextChanged += new EventHandler(ExtdTextBox_TextChanged);
                this.ForeColor = this.waterMarkColor;
                this.Font = this.waterMarkFont;
            }
        }
       void ExtdTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            string str = base.Text.Replace(this.waterMarkText, "");
            this.TextChanged -= new EventHandler(ExtdTextBox_TextChanged);
            this.Text = str;
            this.TextChanged += new EventHandler(ExtdTextBox_TextChanged);
        }
        #endregion
        #region User Defined Properties
        /// <summary>
        /// Property to set/get Watermark color at design/runtime
        /// </summary>
        [Browsable(true)]
        [Category("Extended Properties")]
       [Description("sets Watermark color")]
        [DisplayName("WaterMark Color")]
        public Color WaterMarkColor
        {
            get
            {
                return this.waterMarkColor;
            }
            set
            {
                this.waterMarkColor = value;
                base.OnTextChanged(new EventArgs());
            }
        }
        [Browsable(true)]
        [Category("Extended Properties")]
        [Description("sets TextBox text")]
        [DisplayName("Text")]
        /// <summary>
        /// Property to get Text at runtime(hides base Text property)
        /// </summary>
        public new string Text
        {
            get
            {
                //required for validation for Text property
                return base.Text.Replace(this.waterMarkText, string.Empty);
            }
            set
            {
                base.Text = value;
            }
        }
        [Browsable(true)]
        [Category("Extended Properties")]
        [Description("sets WaterMark font")]
        [DisplayName("WaterMark Font")]
        /// <summary>
        /// Property to get Text at runtime(hides base Text property)  
      /// </summary>
        public Font WaterMarkFont
        {
            get
            {
                //required for validation for Text property
                return this.waterMarkFont;
            }
            set
            {
                this.waterMarkFont = value;
                this.OnTextChanged(new EventArgs());
            }
        }
        /// <summary>
        ///  Property to set/get Watermark text at design/runtime
        /// </summary>
        [Browsable(true)]
        [Category("Extended Properties")]
        [Description("sets Watermark Text")]
        [DisplayName("WaterMark Text")]
        public string WaterMarkText
        {
            get
            {
                return this.waterMarkText;
            }
            set
            {
                this.waterMarkText = value;
                base.OnTextChanged(new EventArgs());
            }
        }
        #endregion
    }
}

Then i did BUILD > Rebuild Solution

Then in my other windows forms project i went to the tool box on the left i did choose items and browsed to the .dll file of the class library project selected it when i selected it i'm getting this error:

There are no components in d:\......CustomControl.dll that can be placed on the toolbox

What could be the problem ?

I took the example from this site:

Custom Control

Upvotes: 0

Views: 123

Answers (2)

Kokom
Kokom

Reputation: 1

The ExtdTextBox class needs to be marked as public as no access modifier will default to internal.

Upvotes: 0

Shaharyar
Shaharyar

Reputation: 12449

You forgot the public keyword on your class:

namespace CustomControl
{
    public class ExtdTextBox : TextBox
    {

Upvotes: 1

Related Questions