0

I have these properties on a form.

public enum LanguageID
{
    en, fr, de, it, ro
}

[Browsable(false)]
public Language[] Languages = new Language[]()
{
    new Language { LangID = LanguageID.en, TranslatedText = "One" },
    new Language { LangID = LanguageID.fr, TranslatedText = "Un"},
    new Language { LangID = LanguageID.de, TranslatedText = "Einer"}
    // and so on, for all other languages
}

public class Language
{
    public LanguageID LangID { get; set; } = LanguageID.en;
    public string TranslatedText { get; set; } = "One";
}

In design-time, user can change LanguageID property. I added some custom Control objects on my form. In those objects constructors I want to access form's public property Languages, to be able to set a property of my custom controls according to selected LanguageID from form's property.

I've tried the solution from this post, but Site property will be set only in design-time, not in runtime. In runtime Site property is null.

public ContainerControl ContainerControl
{
  get { return _containerControl; }
  set { _containerControl = value; }
}
private ContainerControl _containerControl = null;

// Custom object constructor
public MyControl()
{
    var langID = ((Form)ContainerControl).LanguageID; // the problem is here, ContainerControl is populated in DesignMode, but not in runtime. In runtime it's value is null
    var selectedLang = Array.Find(Form.Languages, l => l.LangID = langID);
    // code to set text here
}

public override ISite Site
{
  get { return base.Site; }
  set
  {
    base.Site = value;
    if (value == null)
    {
      return;
    }

    IDesignerHost host = value.GetService(
        typeof(IDesignerHost)) as IDesignerHost;
    if (host != null)
    {
        IComponent componentHost = host.RootComponent;
        if (componentHost is ContainerControl)
        {
            ContainerControl = componentHost as ContainerControl;
        }
    }
  }
}

If this is not the good approach, please, I need some advice.

Vali Maties
  • 94
  • 1
  • 8
  • Reading this I'm not completely clear on exactly what the problem is, maybe you can clarify. What I understand is that you're developing a user defined form and need a reference to the form's `Language` property, which is selected by the user of the form when they include it in their own project. You haven't been able to figure out how to determine at run-time which Language was selected and need a suggestion about how to get that information. – Christopher Hamkins Oct 22 '21 at 12:13
  • Exactly @ChristopherHamkins. I don't know if it is a good approach, but is what I think about . Maybe there is another way of doing that. But, as a conclusion, all my custom controls, which have the property `x` must access Language property of form to be able to set its own language text. – Vali Maties Oct 22 '21 at 12:43
  • @ValiMaties said, "In those objects constructors I want to access form's public property `Languages`". I think what you really want is to `pass in` the Form's `Language` value to the constructor of the UserControl. – HardCode Oct 22 '21 at 16:37

1 Answers1

0

Each controller has a parent object and you can use the following code to get the form:

Type type = this.Parent.GetType();
Control x = this.Parent;
while (type.BaseType.Name != "Form")
{
    x = x.Parent;
    type = x.GetType();
}
// HERE x is your form

Or as a method:

public Form GetParentForm()
{
    Type type = this.Parent.GetType();
    Control x = this.Parent;
    while (type.BaseType.Name != "Form")
    {
        x = x.Parent;
        type = x.GetType();
    }
    return (Form)x;
}

The loop is needed because your control might be inside another container inside of the form.