使用C#从用户控件1获取布尔最后一个值到另一个UC [英] Get boolean last value from user control 1 to another UC using C#
本文介绍了使用C#从用户控件1获取布尔最后一个值到另一个UC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试在第二个用户控件中获取我的布尔值时,它会给我带来这个布尔值的初始化值而不是它的最后一个值!
= >结果一直显示为假!
我需要你的帮助,谢谢你的关注!
我尝试过:
用户控制1:
公共部分类UC1:UserControl
{
public bool boolquery = false;
private void BUTTON_Click(object sender,EventArgs e)
{
if(MYcondition为true)
{
boolquery = true;
}
其他
{
boolquery = false;
}
用户控制2:
public partial class UC2:MetroFramework.Controls.MetroUserControl
{
private void buttonShow_Click(object sender,EventArgs e)
{
userControl1 uc1 = new userControl1 ();
MessageBox.Show(uc1.boolquery.ToString());
}
}
我试过了房产方法(父母对孩子) ):相同的broblem(Boolquery值总是假的!
子表单(用户控件2):
public bool boolquery;
public bool Boolquery
{
get {return boolquery;}
set {boolquery = value;}
}
private void getdaTEbOOLvalue_Click(object sender,EventArgs e)
{
MessageBox.Show(Boolquery.ToString() );
}
父表单(用户控件1):
private usercontrol2 uc2;
public void setValueBool_Click(object sender,EventArgs e)
{
uc2 = new UCconfig();
uc2.Boolquery = true;
}
解决方案
不,那不行 -new
关键字意味着您获得了一个全新的实例,而不是与使用内容相关的任何内容我可以看到。
您需要通过父容器:在两种形式之间传递信息,第3部分:儿童与儿童 [ ^ ] - 它指的是Forms,但它与UserControls完全相同(表格毕竟来自Controls) )。
WHen I try to get my boolean value in the second user control , it brings me the initialized value of this boolean not the last value of it !
=> result shows false all the time !
I need your help , thank you for any attention!
What I have tried:
user control 1 :
public partial class UC1 : UserControl { public bool boolquery= false; private void BUTTON_Click(object sender, EventArgs e) { if(MYcondition is true ) { boolquery=true ; } else { boolquery= false ; }
user control 2 :
public partial class UC2: MetroFramework.Controls.MetroUserControl { private void buttonShow_Click(object sender, EventArgs e) { userControl1 uc1= new userControl1 (); MessageBox.Show(uc1.boolquery.ToString()); } }
I have tried the Property Methode ( Parent to child ): same broblem (Boolquery value is always false !
child form (user control 2):
public bool boolquery; public bool Boolquery { get { return boolquery; } set { boolquery= value; } } private void getdaTEbOOLvalue_Click(object sender, EventArgs e) { MessageBox.Show(Boolquery.ToString()); }
parent form (user control 1) :
private usercontrol2 uc2; public void setValueBool_Click(object sender, EventArgs e) { uc2= new UCconfig(); uc2.Boolquery = true; }
解决方案
No, that's not going to work - thenew
keyword means that you get a brand new instance, not anything related to what the user can see.
You need to go via the parent container: Transferring information between two forms, Part 3: Child to Child[^] - it refers to Forms, but it's exactly the same process for UserControls (Forms are derived from Controls, after all).
这篇关于使用C#从用户控件1获取布尔最后一个值到另一个UC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文