Click here to Skip to main content
15,905,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
    }
Posted
Updated 9-Apr-19 23:42pm
v3
Comments
[no name] 10-Apr-19 11:48am    
1

1 solution

No, that's not going to work - the new 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).
 
Share this answer
 
Comments
EM_Y 10-Apr-19 5:44am    
I ve update my code ; please check it !
i tried the property methode , but my value is not changing ! Boolquery is always false !
OriginalGriff 10-Apr-19 5:48am    
You aren't listening to what I said ... You need to go via the parent, not create a new instance.

Follow the link, read what it says, and try the download. What you are doing will not work: you create a new instance each time and that isn't the same as what the user is looking at.

What you are doing is putting your mobile phone in the glove box of my car, buying yourself a new car and expecting to find your phone in it's glove box. You know it won't be there, because it's still in my car!
So why are you doing the same thing with your classes?
EM_Y 10-Apr-19 11:54am    
I tried to follow the link solution but i got any result ! my mistake !

i worked with condtructor methode , it works but I would be happy if you helped me with my first try (property methode ):

public frmChild (string data)
{
InitializeComponent();
tbData.Text = data;
}
Parent Form
Hide Copy Code
frmChild child = new frmChild("Hello child!");
child.Show();


Thank you for your help and attention :)
OriginalGriff 10-Apr-19 11:57am    
Follow the link, it gives you the property code!
EM_Y 11-Apr-19 4:27am    
very thankful :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900