怎么写的? 这个最好不要从UserControl继承下来,直接从TextBox继承好一些。public class NumBox : TextBox
{
   private decimal m_value;
   public void NumBox(){
     m_value = 0;
   }
   public decimal Value{ 
     get{return m_value;} 
     set{m_value = value; base.Text = value.toString();}
   }
   .......
}

解决方案 »

  1.   

    我改为从TextBox继承了,ForeColor属性生效啦,可是此时,发现控件中定义的事件却不起作用,比如我定义了一个事件:
    private void DigitalTextBox_TextChanged(object sender, System.EventArgs e)
    {
                            Console.WriteLine("DigitalTextBox_TextChanged");
    }可运行程序,在文本框中入力时,根本就没有出发TextChanged事件,不知为何?
      

  2.   

    不要写事件了,写在
    protected override void OnTextChanged()  里。
      

  3.   

    但要记得在重写的OnTextChanged() 方法里调用基类的这个方法,否则事件就关联不上了。protected override void OnTextChanged()  
    {
      base.OnTextChanged(...);
      ....
    }
      

  4.   

    试试 this.UserControl.textBox1.ForeColor
      

  5.   

    冷眉说得对。直接覆盖OnTextChanged可行。
    以下是另一个方法。using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.ComponentModel;
    using System.Windows.Forms.Design;namespace CustomControlLibrary
    {
    ////////////////////////////////////////////////////////////////////////
    [Designer(typeof(NumberFormatTextBox.NumberFormatTextBoxDesigner))]
    //[DefaultProperty("MinNum")]
    //[DefaultProperty("MaxNum")]
    // /// <summary>
    /// NumberFormatTextBox 的摘要说明。
    /// </summary>
    public class NumberFormatTextBox : RichTextBox
    {
    [DllImport("user32.dll")]
    private static extern bool MessageBeep(uint uType);
    [DllImport("user32.dll")]
    private static extern bool GetCaretPos(ref System.Drawing.Point point);

    public int minNum;
    public int maxNum;

    private void InitializeComponent()
    {
    // 
    // NumberFormatTextBox
    // 
    }

    public NumberFormatTextBox()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    } [DefaultValue("0")]
    public int MinNum  {
    get
    {
    return minNum;
    }
    set
    {
    minNum = value;
    }
    } [DefaultValue("0")]
    public int MaxNum 

    {
    get
    {
    return maxNum;
    }
    set
    {
    maxNum = value;
    }
    } protected override bool ProcessKeyEventArgs(ref Message m)
    {
    int keyValue = m.WParam.ToInt32();

    if (  ( keyValue>47 && keyValue<58 ) ||keyValue==8)
    {
    if ( keyValue>47 && keyValue<58 ) //0-9
    {
    System.Drawing.Point point = new    System.Drawing.Point();
    GetCaretPos(ref point);
    int pos = this.GetCharIndexFromPosition(point); if(this.Text!="")
    {
    string newtext;
    if(pos == -1)
    {
    newtext = (keyValue-48) + this.Text;
    }
    else if(pos == this.Text.Length-1)
    {
    newtext = this.Text + (keyValue-48);
    }
    else
    {
    string leftStr = this.Text.Substring(0,pos);
    string rightStr = this.Text.Substring(pos,this.Text.Length-pos);
    newtext = leftStr + (keyValue-48) + rightStr;
    }
    int newValue = Int32.Parse(newtext);

    if(newValue<this.minNum||newValue>this.maxNum)
    {
    MessageBeep(0);
    return true;
    }
    }
    }
    return base.ProcessKeyPreview(ref m);
    }
    else
    {
    MessageBeep(0);
    return true;
    }
    }

    //////////////////////////////////////////////////////////////////////////
    internal class NumberFormatTextBoxDesigner : ControlDesigner
    {
    /*
    protected override void PostFilterProperties(System.Collections.IDictionary properties)
    {
    properties.Remove("Text");
    }
    */ /*
    protected override void PreFilterProperties(System.Collections.IDictionary properties)
    {
    properties.Add("LowerNum", TypeDescriptor.CreateProperty(typeof(DateFormatTextBoxDesigner), "LowerNum", typeof(Int32), null));
    }
    */
    }
    }
    }