HighTechTalks DotNet Forums  

Designer Flaw?

Dotnet Framework (WinForms DesignTime) microsoft.public.dotnet.framework.windowsforms.designtime


Discuss Designer Flaw? in the Dotnet Framework (WinForms DesignTime) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
MLongmire
 
Posts: n/a

Default Designer Flaw? - 05-20-2005 , 10:34 PM






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



Reply With Quote
  #2  
Old   
Francisco Padron
 
Posts: n/a

Default Re: Designer Flaw? - 05-21-2005 , 06:53 PM






You may want to try adding:

[ DesignerSerializationVisibility(DesignerSerializat ionVisibility.Hidden) ]

To your Items property.

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

--
Francisco Padron
www.chartfx.com


"MLongmire" <mark (AT) mlongmire (DOT) net> wrote

Quote:
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





Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.