C# cross-window call control/cross-window change control

Idea:

1. First write the method to change the control in the parent window, and set the method to Public to facilitate the call in the child window.

2. Pass in the parent window when creating the child window, and store it in a variable in the child window. Then just call the method of the parent window below.

Parent window code reference:

MiniForm mini = new MiniForm(this);//传入父窗口,this代表本窗口
mini.Show();//打开子窗口

The code of the control control that needs to be called by the child window:

public void BtnStopChangeColor()//这里是改变按钮和panel的颜色
{
  BtnStar.BackColor = Color.FromArgb(51, 51, 76);//UI颜色改变
  panel3.BackColor = Color.FromArgb(0, 150, 136);
}

Subwindow code reference:

static Form ClientFormSet;//定义一个静态窗口变量去存储主窗口,方便调用
        
public MiniForm(CloudVspd mainform)//接收主窗口类型的参数
{
   InitializeComponent();
   ClientFormSet = mainform;//接收主窗口
}

Next, when you want to call the main window at a certain step, you can use the variable that just stored the main window:

(ClientFormSet as CloudVspd).BtnStopChangeColor();//调用,此时主窗口方法就会被执行,主窗口控件发生变化
(ClientFormSet as CloudVspd).Visible=true;//也可以控制主窗口的显示或隐藏,只要设置为false就是隐藏

Guess you like

Origin blog.csdn.net/aa989111337/article/details/127615888