Help creating custom ComboBox control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mongphong28@yahoo.com.au

    Help creating custom ComboBox control

    Hi,

    I'm populating a ComboBox with objects which contain two properties -
    IntegerValue and StringValue. I display StringValue in the ComboBox
    form and can retrieve IntegerValue from the SelectItem.Inte gerValue
    property. This is all good but now I want to get a little bit more
    complicated.

    I want to be able to set the ComboBox's SelectedItem property by
    setting IntegerValue, so for instance if I have the following items in
    my ComboBox:

    IntegerValue StringValue
    3 Apple
    4 Banana
    1 Carrot
    5 Eggplant
    2 Fig

    By doing something like ComboBox.Select edItem.IntegerV alue = 1 the
    ComboBox would then display Carrot.

    I imagine I would need to create a custom ComboBox control and add a
    function which is kicked off when the IntegerValue is Set, or perhaps
    just override an existing function.

    Has anyone done this or something similar before or have any
    suggestions?

    Regards,
    Mong

  • Cor Ligthert [MVP]

    #2
    Re: Help creating custom ComboBox control

    Mong,

    If you use a datatable as datasource this is in my idea dead simple.

    If you try it using this sample on our website



    And than add in this sample,

    'ComboBox1.Data Source = dt
    'ComboBox1.Disp layMember = "FullName"
    ComboBox1.Displ ayValue = "LName"
    Combobox1.Selec tedText = "Cor Ligthert"
    (In your case a number of course)

    I hope this helps.

    Cor


    <mongphong28@ya hoo.com.au> schreef in bericht
    news:1140396772 .295608.56100@z 14g2000cwz.goog legroups.com...[color=blue]
    > Hi,
    >
    > I'm populating a ComboBox with objects which contain two properties -
    > IntegerValue and StringValue. I display StringValue in the ComboBox
    > form and can retrieve IntegerValue from the SelectItem.Inte gerValue
    > property. This is all good but now I want to get a little bit more
    > complicated.
    >
    > I want to be able to set the ComboBox's SelectedItem property by
    > setting IntegerValue, so for instance if I have the following items in
    > my ComboBox:
    >
    > IntegerValue StringValue
    > 3 Apple
    > 4 Banana
    > 1 Carrot
    > 5 Eggplant
    > 2 Fig
    >
    > By doing something like ComboBox.Select edItem.IntegerV alue = 1 the
    > ComboBox would then display Carrot.
    >
    > I imagine I would need to create a custom ComboBox control and add a
    > function which is kicked off when the IntegerValue is Set, or perhaps
    > just override an existing function.
    >
    > Has anyone done this or something similar before or have any
    > suggestions?
    >
    > Regards,
    > Mong
    >[/color]


    Comment

    • mongphong28@yahoo.com.au

      #3
      Re: Help creating custom ComboBox control

      Thanks Cor,

      I was able to get this working using your suggestion and building an
      arraylist which I then bind the combo to. Only problem is because the
      control is bound (I assume) it defaults to selecting index 0 rather
      than -1, even if the last thing I do is set SelectedItem = -1 it will
      still display the first item in the list. Is there any way around this?

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Help creating custom ComboBox control

        Mongphong,

        Bephore we try other things there is a bug in version 1.x that you have to
        set the selecteditem twice to -1. I have the idea it is in the way you
        describe it.

        Cor


        Comment

        • mongphong28@yahoo.com.au

          #5
          Re: Help creating custom ComboBox control

          Cor,

          If I use the follwoing:

          cbo.SelectedInd ex = -1
          cbo.SelectedInd ex = -1

          the result is still the same, the first item is displayed. However if I
          place a button the form with the following in it's click event (and
          click the button):

          cbo.SelectedInd ex = -1

          the combobox does correctly clear with no item displayed.

          Why do you think it matters where the code is placed?

          The only difference between the two is that the form has not been shown
          when the first attempt to set SelectedIndex to -1 is run. Is this the
          problem? Is it a bug? Is there a fix?

          Thanks again,
          Mong

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Help creating custom ComboBox control

            Mong,

            Did you place it as the last instructions in the Load Event?

            Cor


            Comment

            • mongphong28@yahoo.com.au

              #7
              Re: Help creating custom ComboBox control

              No it's in the initialisation of the form, that's what I meant in my
              last paragraph when I said the form hadn't been shown yet hence the
              Load event had not been run. So it's a bug then, is there a fix?

              Obviously I can add cbo.SelectedInd ex = -1 to the Load event of the
              form, but it shouldn't matter if the control is visible or not.

              Comment

              • Cor Ligthert [MVP]

                #8
                Re: Help creating custom ComboBox control

                Monghponhg,

                No it is not a bug, by settting the datasource will the index as well be
                set.

                Therefore if you with this, than have you to do this after that setting of
                the datasource, by instance in the load event of the form.

                I hope this helps,

                Cor


                Comment

                • mongphong28@yahoo.com.au

                  #9
                  Re: Help creating custom ComboBox control

                  Cor,

                  As previously stated the datasource has been set, the combo list has
                  been populated, and the index has been set to -1. However as I also
                  previously stated if this is done before the form has been visually
                  shown ie form.show() has been called and the form load event fired,
                  setting the index to -1 does not work. It will only work if the index
                  is set to -1 after form load has run.

                  As the object has been created during form initialisation (meaning an
                  object of the class has been instantiated), whether it is visible or
                  not should be irrelavent as the object exists. However this situation
                  shows that although other functionality of the ComboBox class works as
                  expected ie. setting the datasource, building the list of items etc,
                  setting the index does not. This my friend has the hallmarks of a bug.

                  I'm more than happy to hear from you any evidence to disprove that this
                  is a bug. I only ask could you please get an english speaking colleague
                  to proof read your response as I have difficulty deciphering your posts
                  and am beginning to think you're from Dagobah.

                  Regards,
                  Mong



                  Cor Ligthert [MVP] wrote:
                  [color=blue]
                  > Monghponhg,
                  >
                  > No it is not a bug, by settting the datasource will the index as well be
                  > set.
                  >
                  > Therefore if you with this, than have you to do this after that setting of
                  > the datasource, by instance in the load event of the form.
                  >
                  > I hope this helps,
                  >
                  > Cor[/color]

                  Comment

                  Working...