Dominik
Dominik

Reputation: 209

Initialize control in control

This is my custom control class:

// Extended ComboBox where ill change some property.
public class ExtComboBox : ComboBox
{
    ...
}

// ExtButton is a control that i am going to drop on Form from ToolBox.
public partial class ExtButton : Button
{
    public ExtComboBox ComboBoxInsideButton { get; set; }

    public ExtButton()
    {
        InitializeComponent();


        ComboBoxInsideButton = new ExtComboBox();
        ComboBoxInsideButton.Text = "hi!";

        Controls.Add(ComboBoxInsideButton);
    }
}

Basically when i add this control to form there will be ComboBox on top off Button. Don't ask my why i need this :D

Now if i need to change ComboBox text i simply use:

extButton1.ComboBoxInsideButton.Text = "aaa";

All work fine.. but :) when i am trying to change some ComboBox properties in Design mode (Window Properties -> Expand ComboBoxInsideButton -> Change Text to "bbb") after rebuilding or running project ComboBox properties will be reseted (ExtButton.Designer.cs)

Question 1: How to initialize subcontrol with some default properties value, so when ill drop control on Form all setting will be added?

and

Question 2: How to change properties of subcontrol on design time.

EDIT:
Answer here: Designer does not generate code for a property of a subcontrol. Why?
Adding [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] solves the problem.

Upvotes: 1

Views: 1986

Answers (1)

Kcvin
Kcvin

Reputation: 5163

I wrote a mini-tut on how to create custom UserControls and accessing their members here. Pretty much, it looks like you are going to want add properties to your ExtComboBox that expose the ComboBox properties you'd like to change. Then, in ExtButton, you will be able to use the . to change these values at runtime.

Also, instead of doing:

public ExtComboBox ComboBoxInsideButton { get; set; }
...
ComboBoxInsideButton = new ExtComboBox();

do

public ExtComboBox comboBoxInsideButton = null;
...
comboBoxInsideButton = new ExtComboBox();

Be sure to understand the difference between private and public also. I'm not sure if you want your ExtComboBox to be public if you're placing it on another control.

Hope this helps.

Upvotes: 1

Related Questions