[英]How do I access another form's properties with a button live?
I'm trying to code a theme changer to my gui but it doesnt work that well, I've tried everything I know. 我正在尝试将主题更改程序编码为我的gui,但效果不佳,我已经尝试了一切。 I have 2 forms MainUI and Themes and I'm trying to press a button under themes form and then it will fire the code under MainUi live, what I mean with live is that it will happend directly so I don't need to close Themes for it to take effect for an example.
我有2种表单MainUI和主题,我试图按主题表单下的按钮,然后它将在MainUi live下触发代码,我的意思是live将直接发生,所以我不需要关闭主题以使其生效为例。 .
。
My Main Ui's code for Themes is this: 我的Main Ui的主题代码是这样的:
private void button7_Click(object sender, EventArgs e)
{
bool Isopen = false;
foreach(Form f in Application.OpenForms)
{
if (f.Text == "Themes")
{
Isopen = true;
f.BringToFront();
break;
}
}
if (Isopen == false)
{
Themes theme = new Themes();
theme.Show();
}
}
public void FireEvent()
{ //Example
BackColor = Color.FromArgb(255, 255, 255);
}
Themes: 主题:
private void button4_Click(object sender, EventArgs e)
{
MainUI main = new MainUI();
main.FireEvent();
}
You're creating a new Instance of MainUI
each time a theme is selected, so you call FireEvent
on the wrong instance of the form. 每次选择主题时,您都在创建一个
MainUI
的新实例,因此您在错误的表单实例上调用FireEvent
。 You need to pass a reference to the Themes
form. 您需要传递对“
Themes
表单的引用。 For example create a constructor that recieves a MainUI
instance. 例如,创建一个接收
MainUI
实例的构造函数。
class Themes : Form
{
private readonly MainUI _main;
public Themes(MainUI main) : this()
{
_main = main;
}
private void button4_Click(object sender, EventArgs e)
{
_main.FireEvent();
}
}
In the main UI use the following code: 在主界面中,使用以下代码:
private Themes _theme;
private void button7_Click(object sender, EventArgs e)
{
if(_theme == null)
_theme = new Themes(this);
_theme.Show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.