大家好!请教一个问题:(下面仅仅是一个示例问题,具体使用当中不是这么简单的)在VS2005中新建一个C#项目
1、新建主窗体 form1
2、在窗体中拖放一个文本框 textbox1 和按钮 Button13、然后创建一个自定义控件 usercontrol1
4、在用户自定义控件中托放入一个文本框 textbox15、输入“代码”编译执行
6、然后将自定义控件 usercontrol1 拖放入主窗体编译执行要实现的功能:在第 2 步的文本框 textbox1 中输入内容 123 ,点击提交按钮后,在主窗体自定义控件
文本框中也相应显示 123 !效果图:问题:请问第 5 步中的代码应该怎么写???请给出源代码!拜托大家了...

解决方案 »

  1.   

    第五步:
    在自定义控件里写个方法 public void Init(string txt)
            {
                this.txtUserControl.Text = txt;
            }然后在你的Form里调这个方法把值传过来。
      

  2.   


    是下面的写法么??好像不能实现啊!// UserControl1.cs
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication3
    {
        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
            }        public void Init(string txt)
            {
                this.textBox1.Text = txt;
            }    }
    }
    // Form1.cs
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace WindowsApplication3
    {
        public partial class Form1 : Form
        {
            
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                UserControl1 uc = new UserControl1();
                uc.Init(this.textBox1.Text);
            }
        }
    }
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form2 f2 = new Form2();        public Form1()
            {
                InitializeComponent();            UserControl1 uc = new UserControl1();
                panel1.Controls.Add(uc);
            }        private void button1_Click(object sender, EventArgs e)
            {
                string str = textBox1.Text;
                Control[] c = panel1.Controls.Find("UserControl1", false);
                foreach (Control control in c)
                {
                    Control[] con=control.Controls.Find("TextBox1", false);
                    con[0].Text = str;
                }
            }
        }
    }
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            UserControl1 uc = new UserControl1();
                panel1.Controls.Add(uc);
            }        private void button1_Click(object sender, EventArgs e)
            {
                string str = textBox1.Text;
                Control[] c = panel1.Controls.Find("UserControl1", false);
                foreach (Control control in c)
                {
                    Control[] con=control.Controls.Find("TextBox1", false);
                    con[0].Text = str;
                }
            }
        }
    }