Designer Flaw?

M

MLongmire

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
 
B

Bruce Wood

I'm not 100% sure that this will work, but in your PrintComboBox class,
have you tried adding this:

private bool ShouldSerializeItems()
{
if (base.Items.Count != 3)
{
return true;
}
if (!base.Items[0].GetType().Equals(typeof(string)) ||
(string)base.Items[0] != DEST_SCREEN)
{
return true;
}
if (!base.Items[1].GetType().Equals(typeof(string)) ||
(string)base.Items[1] != DEST_FILE)
{
return true;
}
if (!base.Items[2].GetType().Equals(typeof(string)) ||
(string)base.Items[2] != DEST_EMAIL)
{
return true;
}
return false;
}

You have to tell the Designer that your combo box contains the default
items and that so long as it has those items the Designer shouldn't
serialize the Items list. Now, I have some doubts about whether this
will work, because you can't override Items in your derived class, so
the Items property is in the base ComboBox class while the derived
ShouldSerializeItems property is in your derived class. Nonetheless,
it's worth a shot.

You might also want to post this question in the newsgroup
microsoft.public.dotnet.windowsforms.designtime group, as this is a
design time problem with your class. Someone there will surely know how
to fix you up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top