views:

49

answers:

1

How can I go about accessing the result of an if statement in a user control?

UserControl code:

public bool SendBack(bool huh)
{
     if(huh)
       huh = true;
     else huh = false;

     return huh;
}

And in a separate project i am trying to access it like this:

private void button1_Click(object sender, EventArgs e)
{
     MyControl.TextControl t = (MyControl.TextCOntrol)sender;
     if(t.SendBack(true))
     {
        // Do something.
     }
}
+2  A: 

In this case I thing the sender will be the button1, so it will not be castable to your usercontrol...

You will need a reference form the container (form/panel/...) that contains your usercontrol.

Also, I know this might be for simplicity but you can change

public bool SendBack(bool huh) 
{ 
     if(huh) 
       huh = true; 
     else huh = false; 

     return huh; 
} 

to

public bool SendBack(bool huh) 
{
     return huh; 
} 

You might also want to take a look at Control.ControlCollection.Find Method

Searches for controls by their Name property and builds an array of all the controls that match.

astander
Sorry I went to press the up arrow but my mouse jumped, undid the downvote :) -- I am trying to reference the Form containing my usercontrol now :) thank you
lucifer
Thank you very very much astander! Control.ControlCollection.Find worked great, now I can access the correct control :)
lucifer