views:

32

answers:

3

I have created a WindowsFormControlLibrary porject. It works fine, I can drop it on the forms,call its methods,etc ... but now as a property of it, I am passing the name of a Label to it. and I want this custom control to be able to use that label name and for example change its font to bold . so the question is that if I have a WinForm and I have a Label on that form and my custom control on that form, then how can I tell my custom control to do something with that label which I am passing its name to it?

+2  A: 

Instead of sending in the name of the label, send in a reference to the actual label and then the custom control can both read the name if it needs to and change the label's font and other properties.

Be careful though, it can quickly get messy to keep track of what's happening if various forms and controls change controls on other forms etc.

Edit: Added code to do what you ask for in the comments

Code isn't tested so it might not be completely correct, but something similar to this should work.

foreach (Control c in Parent.Controls) 
{
    if (c is Label) 
    {
        Label l = (Label)c;
        // do stuff to label l
    }        
}
ho1
is there a way that I can call FindContorl or something like that?
BDotA
or even better: adding a property to this control that can enumerate all the labels on its containing form? then we can pick one of them at design time ...
BDotA
Sure, I'll update my answer with a code sample.
ho1
A: 

cool :) I just added a get set public property of type Label... it automatically lists all the label on the form.

BDotA
+1  A: 

First, if you wish to access a Control from your UserControl, you will need to use the FindForm() method.

Second, you will be required to expose your TextBox control, for example, through a property of your form.

Then, you would need to know the type of this Form returned by this FindForm() method.

Once you know it, you need to type-cast this result to the correct type.

So, here a sample untested pseudo-code to give you the idea:

public partial class MyMainForm {
    private TextBox textBox1;

    public MyMainForm() {
        textBox1 = new Textbox();
        textBox1.Name = @"textBox1";
        textBox1.Location = new Point(10, 10);
        textBox1.Size = new Size(150, 23);
        this.Controls.Add(textBox1);
    }

    public Font MyTextBoxFont {
        get {
            return textBox1.Font;
        } set {
            if (value == null) return;
            textbox1.Font = value;
        }
    }
}

Then, assuming you have dropped your control on your form, your UserControl could have a property like so:

public partial class MyUserControl {
    private Form GetContainerForm {
        get {
            return this.FindForm();
        }
    }

    // And later on, where you need to set your TextBox's font:
    private void SetContainerInputFieldFont(Font f) {
        if (GetContainerForm == null) return; // Or throw, depending on what you need to do.

        ((MyMainForm)GetContainerForm).MyTextBoxFont = f
    }
}
Will Marcouiller