tags:

views:

37

answers:

2

Hello, for a project of mine I inherited a ComboBox to change its size behaviour. In addition to this i wanted, to speed up my forms' creation, to set the default DropDownStyle to ComboBoxStyle.DropDownList

To do this i used the [Default()] command overwriting the DropDownStyle property

[DefaultValue(ComboBoxStyle.DropDownList)]
public new ComboBoxStyle DropDownStyle
{
    get
    {
        return base.DropDownStyle;
    }
    set
    {
        base.DropDownStyle = value;
    }
}

Then i modified the default value in the Designer setting the DropDownStyle to ComboBoxStyle.DropDownList.

And here comes the problem... There is a small number of InheritedComboBox which i want to have ComboBoxStyle.DropDown because they need to work with

AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Append;
AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;

If i set it from the Designer it works fine, however, sometimes, after i rebuild the form, it throws an exception (also at design time) regarding the ComboBoxStyle. When i look to the FormName.Designer.cs file, i can find that for the specific InheritedComboBox there is no

DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown

and I have to add it manually. This is a little boring because sometimes i just notice it at runtime, when the program throws an exception without showing the form and i cannot test every form every time i rebuild...

Do you have any idea why i get this strange behaviour?

Is there any way to fix it?

Thanks a lot for any answer!

A: 

Try setting this value to the property in the constructor of the inherited combobox too to the value you set with DefaultValue. This should probably fix your issue.

Pieter
No, unfortunately this didn't help
il_guru
The problem you describe does sound a lot like this would be the issue. .NET gets confused when the DefaultValue and the value set by the constructor are out of sync. Try deleting the assignment from Designer.cs, set it again in the property list and see if that helps.
Pieter
The problem is that VS delete it on his own, and i always have to write it manually again. I also tried to delete it and set it again in designer or manually, but this did not work.
il_guru
Whey you add the inherited control to the form, what does the value in the property list shows? Also, is it bold?
Pieter
+1  A: 

When you set the AutoCompleteMode or AutoCompleteSource property, I believe the designer is looking to the base ComboBox and not generating the line to set the DropDownStyle, since DropDown is the default value for ComboBox.

I was able to correct this by adding an AutoCompleteMode and AutoCompleteSource property to the inherited ComboBox, but also had to add in a line to set the base DropDownStyle because of the order in which the designer sets the properties.

Try something like this and see if it works for you:

public class MyComboBox : ComboBox
{
    public MyComboBox()
    {
        DropDownStyle = ComboBoxStyle.DropDownList;
        AutoCompleteMode = AutoCompleteMode.None;
        AutoCompleteSource = AutoCompleteSource.None;
    }

    [DefaultValue(ComboBoxStyle.DropDownList)]
    public new ComboBoxStyle DropDownStyle
    {
        get { return base.DropDownStyle; }
        set { base.DropDownStyle = value; }
    }

    [DefaultValue(AutoCompleteMode.None)]
    public new AutoCompleteMode AutoCompleteMode
    {
        get { return base.AutoCompleteMode; }
        set 
        {
            if (value != AutoCompleteMode.None)
                base.DropDownStyle = ComboBoxStyle.DropDown;

            base.AutoCompleteMode = value; 
        }
    }
    [DefaultValue(AutoCompleteSource.None)]
    public new AutoCompleteSource AutoCompleteSource
    {
        get { return base.AutoCompleteSource; }
        set 
        {
            if (value != AutoCompleteSource.None)
                base.DropDownStyle = ComboBoxStyle.DropDown;

            base.AutoCompleteSource = value; 
        }
    }
}
adrift
Thanks a lot! This works! I noticed that in fact, the DropDownStyle properties is set by the designer, but after the AutoComplete (it seems to be alphabetically). Probably it works fine for the original ComboBox because the DropDown is the default. I wonder if there is any way to affect the order in which the properties are assigned by the designer.. i tried to search but i didn't find anything.
il_guru
Great, glad it worked for you. I also looked for some way to change the order (looked for an attribute), but couldn't find anything.
adrift