![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |