在Form1中有用户控件usercontrol1,Textbox2;在用户控件usercontrol1中有Textbox1和button11.如何从Form1中将字符串“123456”传递给用户控件usercontrol1中的Textbox1,并使Textbox1的背景颜色变为红色?
2.主窗体Form1,如何读取用户控件usercontrol1中Textbox1的值,并赋值给Textbox2?
3.点击用户控件usercontrol1中的button1,如何才能给Textbox2赋值?
4.点击用户控件usercontrol1中的button1,如何才能读取Textbox2中的值,并赋值给Textbox1?共4个小问题,每题20分,请大家帮帮忙!

解决方案 »

  1.   

    自定义控件定义Text属性给TextBox啊,
      

  2.   


    UserControl1 代码:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
            }        public void SetTextBox(string text)
            {
                this.textBox1.Text = text;
            }        public string GetTextBox()
            {
                return textBox1.Text;
            }
            public void SetTextColor(Color color)
            {
                this.textBox1.BackColor = color;
            }        public delegate void SetTextValue(string value);
            public event SetTextValue OnBtnClick;
            private void button1_Click(object sender, EventArgs e)
            {
                if (OnBtnClick != null)
                {
                    OnBtnClick(this.textBox1.Text);//把TEXTBOX1的TEXT传递过去
                }
            }
        }
    }
    FORM1的代码:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.Text.RegularExpressions;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                this.userControl11.SetTextBox("123456");//传递值
                this.userControl11.SetTextColor(Color.Red);//设置颜色
                this.textBox2.Text = this.userControl11.GetTextBox();
    //委托事件取TEXTBOX1的值,并给TEXTBOX2
                this.userControl11.OnBtnClick += new UserControl1.SetTextValue(userControl11_OnBtnClick);//也可以放在构造函数之中
            }        void userControl11_OnBtnClick(string value)
            {
                this.textBox2.Text = value;//赋给TEXTBOX2
            }    }
    }
      

  3.   

    1.Textbox1。text = “123456”
    2.Textbox2.text = textbox2.text;
    3.sendmessage(textbox1,。。); 
    4.sendmessage(textbox1,。。); Textbox1。text = textbox2.text
      

  4.   

    3.点击用户控件usercontrol1中的button1,如何才能给Textbox2赋值? 
    4.点击用户控件usercontrol1中的button1,如何才能读取Textbox2中的值,并赋值给Textbox1? 求解决办法!
    除了3楼的方法,还有没有别的办法啊?
      

  5.   

    1.在usercontrol1定义一个属性
     如:
      
      public TextBoxValue
      { 
        get { return Textbox1.text; }
        set {
             if(value==“123456”)
             {
              Textbox1.text=value;
              Textbox1.背影=颜色; }
      }
      2.如果是在Form1中触发,1中已经写了TextBoxValue,可以得到值
     如果是在usercontrol1触发,回答3中解
    3. 1.写一个usercontrol1事件,当button1时向外触发,然后通过textbox2.text=TextBoxValue;
       public event EventHandler OutputText;
      2.构造时将textbox2对象传进来。直接在button1事件里就可以赋值了。
    4. 构造时将textbox2对象传进来。
      

  6.   

    1.  string text="123456";
        TextBox1.Text=text;
    2.  TextBox1.Text=TextBox2.text;
    3/4.  把上面的两个事件分别写到Buuton按钮生成的方法里就可以了...