如题,传一个变量的值要怎么做呢?
我新手
希望高手多帮帮忙
先谢谢了

解决方案 »

  1.   

    UserControl1中public string str;form1中
    UserControl1 uc = new UserControl1();
    uc.str = "ssss";
      

  2.   

    在UserControl1中定义属性。FORM1设置UserControl1的属性 值就是了。如:VS2008:public UserControl1:UserControl
    {
         public string Text{get;set;}
     
    }在FORM1中:
    UserControl1 uc1=new UserControl();
    uc1.Text="123";
      

  3.   

    class   UserControl1
      {   
            private   string   a;   
            public  UserControl1()   
            {   
                  a="";   
            }   
            public   string   A   
            {   
                  set{a=value;}   
                  get{return   a;}   
            }   
      }   
    UserControl1 u=new   UserControl1()
    u.A="";
      

  4.   

    在usercontrol的类中定义要接受值的字段,在from中实例usercontrol时给其赋值
      

  5.   


    如果uc1调用父类窗体,可以在uc1的构造窗体里边加上父类。
    UserControl1 uc1=new UserControl(parentForm)
    这样控件也可以使用父类的值了。需要什么值,加上个属性就可以了!!
      

  6.   

    还可以写个公共方法在用户控件里,窗体去调用
    UserControl1string m_key;
    Public void a(string key)
    {
       m_key=key;
    }FormUserControl1 uc1=new UserControl(); 
    uc1.a("arg");
      

  7.   

    你要在UserControl中事先定义了属性才好说话呀,否则窗体中的数据怎么传到UserControl中呢。。
      

  8.   

    先谢谢大家
    不过还是不行呀UserControl1代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication2
    {
        public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
            }        public string a { get; set; }        private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Text = a;
            }
        }
    }
    form1代码
    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 WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        UserControl1 my = new UserControl1();        private void button1_Click(object sender, EventArgs e)
            {
                my.a = "aaa";
            }
        }
    }
    大家帮忙看看哪不对啊?
      

  9.   

    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        public string str;
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = str;
        }
    }public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
        }
        UserControl1 uc = new UserControl1();
        void Form1_Load(object sender, EventArgs e)
        {
            uc.Left = 100;
            uc.Top = 100;
            this.Controls.Add(uc);
        }    private void button1_Click(object sender, EventArgs e)
        {
            uc.str = "test";
            //uc.Invalidate();
        }
    }
      

  10.   

    我知道哪错了
    我直接在设计面板添加的控件,名字是UserControl11而不是我自己命名的
    谢谢大家了