Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

Can I prevent VS Designer from initializing my combo box?

I creates the items in my combo box, as seen in the attached code, but sometime when VS designer decides to rebuild the the panel on which the combo boxes appear it tries to create the list. And, sometimes when it has finished and my application run I have two, and sometimes more, copies of the items in my combo box. That is even when I have specifically gone through all the combo boxes on the panel and deleted all the items which it has placed in the list.

How do I stop designer being so smart?

Thanks,
Steve
public class LineWidthComboBox : System.Windows.Forms.ComboBox
    {
        public LineWidthComboBox()
        {
            this.DropDownStyle  = ComboBoxStyle.DropDownList;
 
            this.Size           = new Size(85, 24);
            this.Font           = new Font("Microsoft San Serif",
                                           9.00f,
                                           FontStyle.Regular);
            this.BeginUpdate();
            this.Items.Clear();
            this.Items.Add("Narrow");
            this.Items.Add("Normal");
            this.Items.Add("Wide");
            this.Items.Add("Extra Wide");
            this.EndUpdate();
 
        }
 
   }

Open in new window

0
wsprhino
Asked:
wsprhino
  • 7
  • 6
1 Solution
 
Jaime OlivaresCommented:
try to fill your combo at the Load event
0
 
wsprhinoAuthor Commented:

I am not sure what you mean. I tried in OnCreateControl() but I lost the selection because I was destroying and then recreating the combobox in OnCreateControl() after I had done the initialize.

I do not see an OnLoad() event to override. But, it sounds like I would go back to having the same initialization problem if I create the Item list then.

Steve.

0
 
Fernando SotoCommented:
Hi Steve;

If I remember correctly you already initialize the comboBox through the designer and therefore no need to do it in code a second time.

Is this still the case?

Fernando
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
wsprhinoAuthor Commented:

I want this particular item list for this combobox type. I don't want to have to go through the designer changing every LineWidthComboBox that I have, and, I have a lot, ever time I want to  make a change to the item list. Which is what I would have to do if I left the list there.

The items were loaded in the constructor so there would be a central place to make this change. If VS designer puts it into the property window then I have to go through every LineWidthComboBox widget and reset all the time.

I don't understand why it strips the information out of the code. Why isn't there some way to prevent this?

Steve
0
 
wsprhinoAuthor Commented:

What is more is that every time it makes this change for me I have to go through all the windows where it has made the initialization to delete the duplicates as well as the initial list. It is really time consuming.
0
 
Fernando SotoCommented:
Hi Steve;

Not really following you last post. Can you post what you want to happen from the time you drag the control on to the form. Also can you re-post the current state of the control code.

Fernando
0
 
wsprhinoAuthor Commented:

I have managed to recreate the problem simply.  First I create an empty Windows Forms Project. Add the existing LineWidthComboBox class to the project. Build it and I can see that it only has one set of items in the Property collection.

Now, if I drag the new LineWidthComboBox toolbox item, build and run the application I see that the dropdown list now has two copies of "Narrow, Normal, Wide, Extra Wide".

It does get worse because, in my application at least, I can end up with two or more copies of the line width list. But, from what I can see, I should never get more than 1 copy of the list. If Designer didn't insist on generating the list and allowed the program to do it there would be no problem. Personally I think the designer is messing up. It keeps ading the list into its InitializeComponent because this is class constructor is the only place I generate the list and it is cleared before it is created. So, I see absolutely no way that it is a problem with the application.

Designer appears to be trying to be smart and generate the list up front. But, if I was to read the list from a database at run time in the constructor Designer would totally fail me.

Hope the same happens on your machine and not some problem I have with my installation.

Also, as I said I cannot move the list generation to the OnCreation() handler because I would have difficulty then in setting the SelectedItem.

Thanks very much for looking at this,

Steve
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace Graphics2D
{
    public class LineWidthComboBox : System.Windows.Forms.ComboBox
    {
 
        public LineWidthComboBox()
        {
            this.DropDownStyle  = ComboBoxStyle.DropDownList;
 
            this.Size           = new Size(85, 24);
            this.Font           = new Font("Microsoft San Serif",
                                           9.00f,
                                           FontStyle.Regular);
            this.BeginUpdate();
            this.Items.Clear();
            this.Items.Add("Narrow");
            this.Items.Add("Normal");
            this.Items.Add("Wide");
            this.Items.Add("Extra Wide");
            this.EndUpdate();
 
 
        }
 
   }
    
 
    
    public class LineWidth
    {
        public enum LineWidthValue
        {
            Unset       = -1,
            Narrow      = 0,
            Normal      = 1,
            Wide        = 2,
            [Description("Extra Wide")]
            ExtraWide   = 3
        }
 
        public LineWidth() { }
 
 
        public static LineWidthValue ParseEdgeWidth(LineWidthComboBox cbxWidth)
        {
            LineWidthValue lineWidth;
 
            int selectedIndex = cbxWidth.SelectedIndex;
            try
            {
                lineWidth = (LineWidthValue)LineWidthValue.Parse(typeof(LineWidthValue),
                                                                 cbxWidth.Items[selectedIndex].ToString().Replace(" ", ""));
            }
            catch (ArgumentException)
            {
                lineWidth = LineWidthValue.Normal;
            }
 
            return (lineWidth);
 
        }
 
        
        public static float GetEdgeWidth(LineWidthValue lineWidth)
        {
            switch (lineWidth)
            {
                case LineWidthValue.Narrow :       return(0.5f);
 
                case LineWidthValue.Normal :       return(1.0f);
 
                case LineWidthValue.Wide :         return(2.0f);
 
                case LineWidthValue.ExtraWide :    return(3.0f);
 
                default :                          return(1.0f);
            }
 
        }
    }
}

Open in new window

0
 
Fernando SotoCommented:
Hi Steve;

I do not know if there is any good answer to your question. It seems that when you place items in the constructor of the custom combo box then host that control on a from the designer automatically adds that to the Initialize components section of the from. Now when you go an run the program and the control is instantiate the constructor adds the items to the list as well as the form adding to the list giving two copies of the same data. I have seen post on the net having the same issues but no one has a solution.

Fernando
0
 
wsprhinoAuthor Commented:

I have stripped the code out of the initialize and made the MaxListSize exactly the size that it should be. And it will work fine untill something forces it to regenerate the code for InitializeComponent.

Seems to me that this is a very basic issue for a fundamental component of forms. It is bad that MS can't be bothered to fix it. Very frustrating. I don't understand what got into the heads of the developers at MS. By stripping information out of the code and squirreling it away in resource files it is obviously going to make programmers lives intolerable at times. Probably one of the worst ideas they ever had.

Thanks
Steve
0
 
Fernando SotoCommented:
Hi Steve;

I have come up with a solution although not very elegant. The issues with this solution is that the default items that are placed into the combo box is done at run time and those values will not show up in the designer at design time. You can still add more values at design time and will be added to the default value at run time. Below is the only code that needs to be changed. See what you think.

Fernando

public LineWidthComboBox() : base()
{
    this.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F);
    this.FormattingEnabled = true;
    this.Location = new System.Drawing.Point(65, 32);
    this.Name = "lineWidthComboBox1";
    this.Size = new System.Drawing.Size(121, 23);
    this.TabIndex = 0;
}
 
private bool comboBoxDisplayed = false;
 
protected override void OnVisibleChanged(System.EventArgs e)
{
    if (!comboBoxDisplayed)
    {
        comboBoxDisplayed = true;
        this.Items.AddRange(new object[] {
            "Narrow",
            "Normal",
            "Wide",
            "Extra Wide"});
    }
    base.OnVisibleChanged(e);
}            

Open in new window

0
 
wsprhinoAuthor Commented:

I can't get it to work but I am not sure whether that is because of some of the changes I have made. For example, I deleted all InitializeComponent code that reads the Item list from the resource file. So, far it has not tried to regenerate that code. My worry is that it will. The properties panel still shows the item list with the 4 items. But, it has been working for a while now.

As for your code sample : Once I take the initialization out of the constructor I have problems initializing the selector. If I keep the initialization in the constructor and in the OnVisibleChanged then it runs but none of my selected indexes are set.

Steve
0
 
Fernando SotoCommented:
You may need to delete the control or force the designer code to recreate the designer.cs file for the form so that it re-samples the new control code.
0
 
wsprhinoAuthor Commented:

I will probably try and stick with the changes that I have for the time being. Until, at least designer regenerates the source code for InitializeComponent. But, when things go wrong again I will give it another try. Fixing all the comboboxes up in 100 different places is no fun. I just wish MS would stop generating files from my code that I am not supposed touch - especially when they take something that works and turn it into something that doesn't work.

Thanks very much for all the work you have put in, it is appreciated.

Steve
0
 
Fernando SotoCommented:
Well we can only hope that they fix issues like this. Glad to have been of some help.  ;=)
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now