Solved

Can I prevent VS Designer from initializing my combo box?

Posted on 2008-10-14
14
268 Views
Last Modified: 2013-12-17
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
Comment
Question by:wsprhino
  • 7
  • 6
14 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22716058
try to fill your combo at the Load event
0
 

Author Comment

by:wsprhino
ID: 22716256

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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22716415
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
 

Author Comment

by:wsprhino
ID: 22716488

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
 

Author Comment

by:wsprhino
ID: 22716634

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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22717855
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
 

Author Comment

by:wsprhino
ID: 22718109

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22732695
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
 

Author Comment

by:wsprhino
ID: 22733025

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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22734576
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
 

Author Comment

by:wsprhino
ID: 22735431

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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 22735644
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
 

Author Comment

by:wsprhino
ID: 22746510

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
 
LVL 62

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now