Help with ComboBox control

  • Thread starter Thread starter fragget
  • Start date Start date
F

fragget

Hiya-

I wrote a color combo box control that shows colors
in the drop down part of a combo box.

The control
works great, however, whenever I drag any of the controls
around on a form this control is on (the color control)
the following code gets added to the form the control
sits on.

this.comboBox1.Items.AddRange(new object[] {
"1",
"2",
"3",
"4",
"5"});

In my constructor of my color combo box I add 5 items to
the control. This problem seems to be corrolated to that
in some way.

When this code gets added and I run my app the control
fails do to an index out of bounds exception.

My question is. Why the the designer keep adding this code
and how can I fix this issue.


Thanks - fg
 
Hi,

The simplest way is to move off the item addition code out of the
constructor. I beleive more elegant solution exists and has something to do
with design-time support for custom controls. MSDN Magazine published a very
good two-part article on this in its May and June issues this year (I can be
wrong about the months though), you might want to check it out.
 
is the code fromt he constructor adding these items as it
is moved around the form? i tried setting a debug
statement to see if the constructor was being called when
it is dropped on the form.

-----Original Message-----
Hi,

The simplest way is to move off the item addition code out of the
constructor. I beleive more elegant solution exists and has something to do
with design-time support for custom controls. MSDN Magazine published a very
good two-part article on this in its May and June issues this year (I can be
wrong about the months though), you might want to check it out.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

fragget said:
Hiya-

I wrote a color combo box control that shows colors
in the drop down part of a combo box.

The control
works great, however, whenever I drag any of the controls
around on a form this control is on (the color control)
the following code gets added to the form the control
sits on.

this.comboBox1.Items.AddRange(new object[] {
"1",
"2",
"3",
"4",
"5"});

In my constructor of my color combo box I add 5 items to
the control. This problem seems to be corrolated to that
in some way.

When this code gets added and I run my app the control
fails do to an index out of bounds exception.

My question is. Why the the designer keep adding this code
and how can I fix this issue.


Thanks - fg

.
 
No, but the Windows Forms designer will re-generate the
InitializeComponent() code upon moving a control (as it will at least have
to update the control's Position property).

And I'm afraid setting a breakpoint won't work - at least until you find a
way to run the Windows Forms designer itself under the debugger. Still,
constructor IS called when a component is dropped on a form in the design
mode - you may try to throw an exception from a constructor and after
compiling the project you won't be able to drop the control on a form.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

fragget said:
is the code fromt he constructor adding these items as it
is moved around the form? i tried setting a debug
statement to see if the constructor was being called when
it is dropped on the form.

-----Original Message-----
Hi,

The simplest way is to move off the item addition code out of the
constructor. I beleive more elegant solution exists and has something to do
with design-time support for custom controls. MSDN Magazine published a very
good two-part article on this in its May and June issues this year (I can be
wrong about the months though), you might want to check it out.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

fragget said:
Hiya-

I wrote a color combo box control that shows colors
in the drop down part of a combo box.

The control
works great, however, whenever I drag any of the controls
around on a form this control is on (the color control)
the following code gets added to the form the control
sits on.

this.comboBox1.Items.AddRange(new object[] {
"1",
"2",
"3",
"4",
"5"});

In my constructor of my color combo box I add 5 items to
the control. This problem seems to be corrolated to that
in some way.

When this code gets added and I run my app the control
fails do to an index out of bounds exception.

My question is. Why the the designer keep adding this code
and how can I fix this issue.


Thanks - fg

.
 
Back
Top