Help with ComboBox control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fragget

    #1

    Help with ComboBox control

    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
  • Dmitriy Lapshin [C# / .NET MVP]

    #2
    Re: Help with ComboBox control

    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

    Bring the power of unit testing to VS .NET IDE

    "fragget" <fragget@home.c om> wrote in message
    news:164101c38c 1a$4a8a45c0$a30 1280a@phx.gbl.. .[color=blue]
    > 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[/color]

    Comment

    • fragget

      #3
      Re: Help with ComboBox control

      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.

      [color=blue]
      >-----Original Message-----
      >Hi,
      >
      >The simplest way is to move off the item addition code[/color]
      out of the[color=blue]
      >constructor. I beleive more elegant solution exists and[/color]
      has something to do[color=blue]
      >with design-time support for custom controls. MSDN[/color]
      Magazine published a very[color=blue]
      >good two-part article on this in its May and June issues[/color]
      this year (I can be[color=blue]
      >wrong about the months though), you might want to check[/color]
      it out.[color=blue]
      >
      >--
      >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" <fragget@home.c om> wrote in message
      >news:164101c38 c1a$4a8a45c0$a3 01280a@phx.gbl. ..[color=green]
      >> 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[/color][/color]
      controls[color=blue][color=green]
      >> 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[/color][/color]
      to[color=blue][color=green]
      >> the control. This problem seems to be corrolated to[/color][/color]
      that[color=blue][color=green]
      >> 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[/color][/color]
      code[color=blue][color=green]
      >> and how can I fix this issue.
      >>
      >>
      >> Thanks - fg[/color]
      >
      >.
      >[/color]

      Comment

      • Dmitriy Lapshin [C# / .NET MVP]

        #4
        Re: Help with ComboBox control

        No, but the Windows Forms designer will re-generate the
        InitializeCompo nent() 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

        Bring the power of unit testing to VS .NET IDE

        "fragget" <fragget@home.c om> wrote in message
        news:044701c38c 2c$6d39a590$a10 1280a@phx.gbl.. .[color=blue]
        > 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.
        >
        >[color=green]
        > >-----Original Message-----
        > >Hi,
        > >
        > >The simplest way is to move off the item addition code[/color]
        > out of the[color=green]
        > >constructor. I beleive more elegant solution exists and[/color]
        > has something to do[color=green]
        > >with design-time support for custom controls. MSDN[/color]
        > Magazine published a very[color=green]
        > >good two-part article on this in its May and June issues[/color]
        > this year (I can be[color=green]
        > >wrong about the months though), you might want to check[/color]
        > it out.[color=green]
        > >
        > >--
        > >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" <fragget@home.c om> wrote in message
        > >news:164101c38 c1a$4a8a45c0$a3 01280a@phx.gbl. ..[color=darkred]
        > >> 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[/color][/color]
        > controls[color=green][color=darkred]
        > >> 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[/color][/color]
        > to[color=green][color=darkred]
        > >> the control. This problem seems to be corrolated to[/color][/color]
        > that[color=green][color=darkred]
        > >> 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[/color][/color]
        > code[color=green][color=darkred]
        > >> and how can I fix this issue.
        > >>
        > >>
        > >> Thanks - fg[/color]
        > >
        > >.
        > >[/color][/color]

        Comment

        Working...