我创建了一个自定义颜色控件public partial class CustomControl1 : Contr,但想通过原来Form窗体中的一个Button 来调用这个自定义控件,如何调用?高手指教!

解决方案 »

  1.   

    你的意思是指在button的按钮事件中:
      CustomControl1 my=new CustomControl1() ;????
      

  2.   

    LZ的意思就是调用自己写的控件还不使用系统原来默认的.
       相当年我不会的时候发贴问这个问题.一个高手告诉我:编译成DLL文件用.或是直接调用就可以.可惜的是我到现在还是没有明白到底应该怎么弄.
      

  3.   

    昨天有一个高手给了这段程序!  我怎么能在现有的form中显示这个控件呢???急急急using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class CustomControl1 : Control
        {
            public CustomControl1()
            {
                InitializeComponent();
            }        protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);            Rectangle colorRect = new Rectangle(0, 0, 10, 10);            foreach (KnownColor kc in Enum.GetValues(typeof(KnownColor)))
                {
                    Color color = Color.FromKnownColor(kc);//获取颜色
                    using (SolidBrush brush = new SolidBrush(color))
                    {
                        e.Graphics.FillRectangle(brush, colorRect);
                    }                //画完一个小巨型,然后填充,每一行的数目满了以后,就到下一行去继续画;
                    if (colorRect.Right + colorRect.Width + 2 < this.ClientRectangle.Right)
                    {
                        colorRect.X += colorRect.Width + 2;
                    }
                    else
                    {
                        colorRect.X = 0;
                        colorRect.Y += colorRect.Height + 2;
                    }
                }
            }
            public Color GetColor(int x, int y)
            {
                Rectangle colorRect = new Rectangle(0, 0, 10, 10);
                foreach (KnownColor kc in Enum.GetValues(typeof(KnownColor)))
                {
                    if (colorRect.Contains(x, y))
                    {
                        return Color.FromKnownColor(kc);
                    }
                    if (colorRect.Right + colorRect.Width + 2 < this.ClientRectangle.Right)
                    {
                        colorRect.X += colorRect.Width + 2;
                    }
                    else
                    {
                        colorRect.X = 0;
                        colorRect.Y += colorRect.Height + 2;
                    }
                }
                return Color.Empty;
            }
            protected override void OnMouseClick(MouseEventArgs e)
            {
                base.OnMouseClick(e);
                Console.WriteLine(this.GetColor(e.X, e.Y));
            }    }
    }
      

  4.   

    应该不是很难吧
    首先得到前台代码去注册,在page命令下注册
    然后就可以使用了
      

  5.   

    对了,我是一个WINDOWS应用程序,能用page吗?
      

  6.   

    你是win的啊,没看代码
    那我还真没试过,不好意思啊
      

  7.   

    public partial class CustomControl1 : Contr {
    // 你的代码
    }
    改为
    public partial class CustomControl1 : Contr, System.ComponentModel.Component {
    // 你的代码
    }
    如果是VS2005,在你的工具箱就能看见 CustomControl1 的组件了。VS2003的不清楚。
      

  8.   

    如果成功,它就会出现在工具栏里,当然你也可以找到编译出来的DLL添加到工具栏,用法和其他控件是一样的
      

  9.   

    我也有类似的问题,我托到form中运行时,说找不到WindowsControlLibrary1/Debug/WindowsControlLibrary1.dll无法运行,请高手指点