以不同的形式工作 [英] working on different forms

查看:90
本文介绍了以不同的形式工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用两个具有一个按钮和一个文本框的表单.我希望单击表单1中的按钮1,两个文本框的颜色都应更改.

i am working with two forms with one button and one text box on each.i want on click of button 1 in form 1 color of both text box should change.

推荐答案

您需要观察者模式".您可以使用事件来实现它:Form1单击Button1时触发一个事件,Form2订阅该事件,并在接收到该事件时更改其Textbox的颜色.
也就是说,您需要研究的是C#中的事件和事件处理.
You need the "Observer Pattern". You can implement it with events: Form1 fires an event when its Button1 was clicked, Form2 subscribes to that event, and changes the color of its Textbox when it receives the event.
That is, the thing you have to study here are Events and Event Handling in C#.


首先创建一个像这样的类
first create one class like this
public class Class1
   {
       public string color { get; set; }
   }



创建两个表单(form1和form2)

在form1按钮单击事件中编写此代码



Create two forms (form1 and form2)

write this code in form1 button click event

private void button1_Click(object sender, EventArgs e)
     {
         Class1 c = new Class1();
         c.color = "red";
         textBox1.BackColor = System.Drawing.Color.Red;
         Form2 f2 = new Form2(c);
         f2.Show();
     }



修改form2的构造函数



Modify the constructor of form2

public Form2(Class1 c) // constructor
        {
            InitializeComponent();

            if (c.color == "red")
                textBox1.BackColor = System.Drawing.Color.Red;
        }


这篇关于以不同的形式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆