Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / May 2005

Tip: Looking for answers? Try searching our database.

Designer Flaw?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MLongmire - 21 May 2005 04:33 GMT
I'm trying to create a ComboBox class that will present the same printout
options to users no matter where I need to use the combo box.  I extend the
ComboBox class and in the constructor I add predefined print destinations,
along with setting other properties.

The problem comes when I drop the class onto the form in the designer. The
code thats gets generated automatically "scrapes" the constructor and
creates an AddRange() call for the values I use during instantiation.

This behavior is annoying.  When I run the program two sets of options
appear in the drop down.  On top of the AddRange() call being injected, the
values for the AddRange() call are converted from variable references to
literal values.  If I use the combo box throughout my application and then
decide to change the values of the variables, I'll have to search and
replace the literal values, instead of changing the values in the class.

Here is a sample.  I used sharpDevelop, but the problem(?) occurs in
devStudio, also.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WinFormsTest {
   public class MainForm : System.Windows.Forms.Form {
       public MainForm() {
           InitializeComponent();
       }
       [STAThread]
       public static void Main(string[] args) {
           Application.Run(new MainForm());
       }
       private void InitializeComponent() {
           this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
           this.ClientSize = new System.Drawing.Size(292, 271);
           this.Name = "MainForm";
       }
   }

   public class PrintComboBox : System.Windows.Forms.ComboBox {
       public PrintComboBox() {
               Items.Add( DEST_SCREEN );
               Items.Add( DEST_FILE );
               Items.Add( DEST_EMAIL );
               DropDownStyle = ComboBoxStyle.DropDownList;
               SelectedIndex = 0;
       }
       private const String DEST_SCREEN  = "View";
       private const String DEST_FILE    = "File";
       private const String DEST_EMAIL   = "eMail";
   }

}

When you switch to design mode, the PrintComboBox will appear in the Custom
Components section of the toolbox.  Add the combo box to the form, then
check the source code and you'll see this statement will have been added to
MainForm.InitializeComponent().

           this.printComboBox1.Items.AddRange(new object[] {
                       "View",
                       "File",
                       "eMail"});

There must be some explanation, since both devStudio and sharpDevelop
produce the same results.
Is this typical behaviour?

Am I implementing this correctly?

Thanks,
Mark
Francisco Padron - 22 May 2005 00:53 GMT
You may want to try adding:

[ DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden) ]

To your Items property.

You can also add a ShouldSerializeItems method to your class that always
returns false.

Signature

Francisco Padron
www.chartfx.com

> I'm trying to create a ComboBox class that will present the same printout
> options to users no matter where I need to use the combo box.  I extend
[quoted text clipped - 69 lines]
> Thanks,
> Mark

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.