txtLabelText 要么是属性,要么是字段.
不太明白你的意思.

解决方案 »

  1.   

    child controls are not exposed with their id/name, you need to useTextBox tb = (TextBox)myControl.Controls[0];orTextBox tb = (TextBox)myControl.FindControl("txtLabelText"); //webform
      

  2.   

    谢思归,但是我按你的第一个写法写,tb没有length属性,按第二个写法写,编译时提示myControl没有findcontrol方法 :(,还望不吝指教
      

  3.   

    FindControl is in webform, are you using winform? TextBox doesn't have a Length property, do you mean TextBox.Text.Length?
      

  4.   

    我怎么写都不能引发自定义控件的这个事件:MaxLengthReached
    下面是全部源代码:
    1。自定义控件
    namespace LabelTextbox
    {
    public class ctlLabelTextbox : System.Windows.Forms.UserControl
      {
        public enum PositionEnum
        {
          Right,
          Below
        }    private System.Windows.Forms.Label lblTextBox;
        private System.Windows.Forms.TextBox txtLabelText;
        private System.ComponentModel.Container components = null;    private PositionEnum mPosition = PositionEnum.Right;
        private int mTextboxMargin = 0;    public event System.EventHandler PositionChanged;    public event System.EventHandler MaxLengthChanged;    public event System.EventHandler MaxLengthReached;    public ctlLabelTextbox()
        {
          InitializeComponent();
          this.SizeChanged += new System.EventHandler(this.OnSizeChanged);
          this.Load += new EventHandler(this.OnLoad);      this.txtLabelText.KeyDown += new KeyEventHandler(this.txtLabelText_KeyDown);
          this.txtLabelText.KeyUp += new KeyEventHandler(this.txtLabelText_KeyUp);
          this.txtLabelText.KeyPress += new KeyPressEventHandler(this.txtLabelText_KeyPress);
        protected override void Dispose( bool disposing )
        {
          if( disposing )
          {
            if( components != null )
              components.Dispose();
          }
          base.Dispose( disposing );
        }    public PositionEnum Position
        {
          get
          {
            return mPosition;
          }
          set
          {
            mPosition = value;
            MoveControls();
            if (PositionChanged != null)
        {
              System.Delegate[] subscribers = PositionChanged.GetInvocationList();
              foreach (System.EventHandler target in subscribers)
              {
                target(this, new EventArgs()); // Call the method
              }
            }
          }
        }
        public int TextboxMargin
        {
          get
          {
            return mTextboxMargin;
          }
          set
          {
            mTextboxMargin = value;
            MoveControls();
          }
        }    public string LabelText
        {
          get
          {
            return lblTextBox.Text;
          }
          set
          {
            lblTextBox.Text = value;
            MoveControls(); // Call MoveControls to make the size the textbox if needed
          }
        }    public string TextboxText
        {
          get
          {
            return txtLabelText.Text;
          }
          set
          {
            txtLabelText.Text = value;
          }
        }
      public int MaxLength
      {
      get
      {
      return txtLabelText.MaxLength;
      }   set
      {
      txtLabelText.MaxLength=value;   if(MaxLengthChanged!=null)
      {
      System.Delegate[] subscribers=MaxLengthChanged.GetInvocationList();   foreach (System.EventHandler target in subscribers)
      {
      target(this,new EventArgs());
      }
      }
      

  5.   

      }
      }
        private void MoveControls()
        {
          switch (mPosition)
          {
            case PositionEnum.Below:
            {
              // Place the top of the Textbox just below the label
              this.txtLabelText.Top = this.lblTextBox.Bottom;
              this.txtLabelText.Left = this.lblTextBox.Left;          // Change the width of the Textbox to equal the width of the control
              this.txtLabelText.Width = this.Width;
    this.Height = txtLabelText.Height + lblTextBox.Height;
              break;
            }
            case PositionEnum.Right:
            {
              // Set the top of the textbox to equal that of the label
              txtLabelText.Top = lblTextBox.Top;          // If the margin is zero, we'll place the textbox next to the label
              if (mTextboxMargin == 0)
              {
                int width = this.Width-lblTextBox.Width-3;
                txtLabelText.Left = lblTextBox.Right + 3;
                txtLabelText.Width = width;
              }
              else
              {
                // If the margin isn't zero, we place the textbox where the user have specified
                txtLabelText.Left = mTextboxMargin;
                txtLabelText.Width = this.Right-mTextboxMargin;
              }
              break;
            }
          }
        }    private void OnSizeChanged(object sender, System.EventArgs e)
        {
          MoveControls();
        }    private void txtLabelText_KeyDown(object sender, KeyEventArgs e)
        {
          OnKeyDown(e);
        }    private void txtLabelText_KeyUp(object sender, KeyEventArgs e)
        {
          OnKeyUp(e);
        }    private void txtLabelText_KeyPress(object sender, KeyPressEventArgs e)
        {
          OnKeyPress(e);

        }    private void OnControlAdded(object sender, ControlEventArgs e)
        {
          MoveControls();
        }    private void OnLoad(object sender, EventArgs e)
        {
          lblTextBox.Text = this.Name;
          this.Height = txtLabelText.Height + lblTextBox.Height;
          MoveControls();
        } #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
    this.lblTextBox = new System.Windows.Forms.Label();
    this.txtLabelText = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // lblTextBox
    // 
    this.lblTextBox.AutoSize = true;
    this.lblTextBox.Location = new System.Drawing.Point(0, 0);
    this.lblTextBox.Name = "lblTextBox";
    this.lblTextBox.Size = new System.Drawing.Size(35, 17);
    this.lblTextBox.TabIndex = 0;
    this.lblTextBox.Text = "Label";
    // 
    // txtLabelText
    // 
    this.txtLabelText.Location = new System.Drawing.Point(8, 40);
    this.txtLabelText.Name = "txtLabelText";
    this.txtLabelText.TabIndex = 1;
    this.txtLabelText.Text = "";
    this.txtLabelText.TextChanged += new System.EventHandler(this.txtLabelText_TextChanged);
    // 
    // ctlLabelTextbox
    // 
    this.Controls.Add(this.txtLabelText);
    this.Controls.Add(this.lblTextBox);
    this.Name = "ctlLabelTextbox";
    this.Load += new System.EventHandler(this.ctlLabelTextbox_Load);
    this.ResumeLayout(false); }
    #endregion   private void ctlLabelTextbox_Load(object sender, System.EventArgs e)
      {
      
      }   private void txtLabelText_TextChanged(object sender, System.EventArgs e)
      {
          if(MaxLengthReached!=null)
    {
    System.Delegate[] subscribers=MaxLengthReached.GetInvocationList();
    foreach (System.EventHandler target in subscribers)
    {
    target(this,new EventArgs()); } }
      }     }
    }
      

  6.   

    以上是自定义控件的代码,下面是测试这个控件的代码:
    namespace LabelTextboxTest
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    private LabelTextbox.ctlLabelTextbox myControl=new LabelTextbox.ctlLabelTextbox();
    private LabelTextbox.ctlLabelTextbox ctlLabelTextbox1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;

    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

                myControl.PositionChanged += new EventHandler(this.myControl_PositionChanged); myControl.MaxLengthChanged+=new EventHandler(this.myControl_MaxLengthChanged); myControl.MaxLengthReached+=new EventHandler(this.myControl_MaxLengthReached);
    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.ctlLabelTextbox1 = new LabelTextbox.ctlLabelTextbox();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(125, 258);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(90, 25);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // ctlLabelTextbox1
    // 
    this.ctlLabelTextbox1.LabelText = "ctlLabelTextbox";
    this.ctlLabelTextbox1.Location = new System.Drawing.Point(0, 0);
    this.ctlLabelTextbox1.MaxLength = 32767;
    this.ctlLabelTextbox1.Name = "ctlLabelTextbox1";
    this.ctlLabelTextbox1.Position = LabelTextbox.ctlLabelTextbox.PositionEnum.Right;
    this.ctlLabelTextbox1.Size = new System.Drawing.Size(150, 38);
    this.ctlLabelTextbox1.TabIndex = 1;
    this.ctlLabelTextbox1.TextboxMargin = 0;
    this.ctlLabelTextbox1.TextboxText = "";
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(8, 96);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 2;
    this.textBox1.Text = "";
    this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(350, 294);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.ctlLabelTextbox1);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }        private void button1_Click(object sender, System.EventArgs e)
            {
                if (myControl.Position == LabelTextbox.ctlLabelTextbox.PositionEnum.Below)
    {
                   myControl.Position = LabelTextbox.ctlLabelTextbox.PositionEnum.Right;
        } else {
                   myControl.Position = LabelTextbox.ctlLabelTextbox.PositionEnum.Below;
    }
            }        private void myControl_PositionChanged(object sender, EventArgs e)
            {
               MessageBox.Show("Changed");
            } private void myControl_MaxLengthChanged(object sender,EventArgs e)
    {
    //int intMaxLengthChanged;
    //intMaxLengthChanged=myControl.MaxLength;
    MessageBox.Show(Convert.ToString(myControl.MaxLength)); } private void textBox1_TextChanged(object sender, System.EventArgs e)
    {
    myControl.MaxLength=Convert.ToInt16(textBox1.Text); } private void myControl_MaxLengthReached(object sender,EventArgs e)
    {
        TextBox tb = (TextBox)myControl.Controls[1]; if(tb.Text.Length==myControl.MaxLength)
    {
    MessageBox.Show("MaxLength reached!"); }
    } }
    }
      

  7.   

    you have two ctlLabelTextbox controls, one of them, "myControl", has the event handler hooked up, but you didn't even add it to the Form.Controls!!!! the other, "ctlLabelTextbox1", didn't have the MaxLengthReached event hooked up!!!