我设计了两个窗体,在form2中有一个一个命令按钮,执行后,可改变form1下的文本框的背景色.但是,做完后,我单步执行,代码执行起来没有问题,可form1文本框的颜色却没变.什么原因呀?高手给指点下.没积分了,版主莫删我的帖子哦.
以下是form2按钮下的部分代码:private void button1_Click(object sender, EventArgs e)
        {
            Form1 frm1 = new Form1();
            const string BackColW ="白色";
            const string BackColB = "黑色";
            const string BackColBL="蓝色";
            const string BackColR="红色";
            const string BackColG="绿色";
            string BCC;
            
            BCC = comboBox1.Text;
            
            if (BCC == BackColW)
                frm1.txtContent.BackColor = Color.White;
            else if (BCC == BackColB)
                frm1.txtContent.BackColor = Color.Black;
            else if (BCC == BackColBL)
                frm1.txtContent.BackColor=Color.Blue;
            else if (BCC == BackColR)
                frm1.txtContent.BackColor=Color.Red;
            this.Dispose();
来源: http://www.programbbs.com/bbs/view14-19010-1.htm

解决方案 »

  1.   

    Form1 frm1 = new Form1(); 
    你这里的frm1是新的,跟原来的没关系。当然不能改变上面的控件背景色了
      

  2.   

    一个改变窗体背景色的方法,参考一下。来自http://bingning.net/VB/SOURCE/form/mdibackcolor.html[C#]
     
     /// <summary>
     /// 查找窗体的MdiClient控件并返回
     /// </summary>
     /// <param name="f">查找MdiClient控件的窗体</param>
     /// <returns>查找到的MdiClient控件</returns>
     public static System.Windows.Forms.MdiClient 
         GetMdiClient(System.Windows.Forms.Form f)
     {
         foreach (System.Windows.Forms.Control c in f.Controls)
             if (c is System.Windows.Forms.MdiClient)
                 return (System.Windows.Forms.MdiClient) c;
         return null;
     }//查找MdiClient
     System.Windows.Forms.MdiClient mc = GetMdiClient(this);
     if (mc != null)
     {
         //更改背景色,重新描绘
         mc.BackColor = this.BackColor;
         mc.Invalidate();
     }