老师,你好!
   我在Form1使用了一个butonn和一个label1控件,但我想在另一个类中改写label1控件的标题,怎样才能实现?
namespace WindowsApplication18
{
    public partial class Form1 : Form
     {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            text a= new text();
            a.money();
        }       
    }
    public class text 
    {
        public void money()
        {
           label1.Text = "123";
           
        }
    }
}

解决方案 »

  1.   

    设置一个属性用来修改label的textpublic string LabelText
    {
        set{ label1.Text = value;}
    }
      

  2.   

    第一种
    最简单的办法,找到Form1.designer.cs修改Label1的修饰符由private改为protected
    第二种方法
    给Form1一个公开的属性返回Label1.Text,从而可以在Form2中访问此属性
      

  3.   

    你可以给你的类text添加一个事件,在适当的时候引发事件并把所需的数据以事件参数的形式传到Form中。类似如下:public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    } private void button1_Click(object sender, EventArgs e)
    {
    text a = new text();
    a.Changed += new EventHandler<TextEventArgs>(a_Changed);
    a.money();
    } void a_Changed(object sender, TextEventArgs e)
    {
    label1.Text = e.TextValue; 
    }
    }
    public class TextEventArgs : EventArgs
    {
    private string m_TextValue;
    public TextEventArgs(string textValue)
    {
    this.m_TextValue = textValue;
    }
    public string TextValue
    {
    get { return m_TextValue; }
    set { m_TextValue = value; }
    }
    }
    public class text
    {
    public event EventHandler<TextEventArgs> Changed;
    public void money()
    {
    if (Changed != null)
    {
    TextEventArgs args = new TextEventArgs("123");
    Changed(this, args);
    }
    }
    }
      

  4.   

    较为简单的方式,就是直接把Label传过去,参考代码:public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                text a = new text(label1);
                a.money();
            }
        }
        public class text
        {
            Label pLab;
            public text(Label lab)
            {
                pLab = lab;
            }
            public void money()
            {
                pLab.Text = "123";
            }
        } 
      

  5.   

    你在打开另一个窗体的时候 form.Show(this); 把你要操作的窗体1的引用传入,然后在使用我这个写法
    设置一个属性用来修改label的textpublic string LabelText
    {
        set{ label1.Text = value;}
    }
    操作不就行了
      

  6.   

    namespace WindowsApplication18 
    {
        
        public partial class Form1 : Form 
        {
            public Form1() 
            { 
                InitializeComponent();
            }         private void button1_Click(object sender, EventArgs e) 
            { 
                text a= new text(); 
                a.money(label1); 
            } 
        } 
        public class text 
        { 
            public void money(Label displayLabel) 
            { 
              displayLabel.Text = "123";          
            } 
        } 
    }
      

  7.   

    7楼的代码应该改为这样
        public class text 
        { 
            public void money(ref Label displayLabel) 
            { 
                 displayLabel.Text = "123";          
            } 
        }