How do you cast an object to a ComboBox Item to Select it....

Hi all,

I have a very simple class that has two properties (ID, and Desc). Now I use these to populate the ComboBox (setting the ID as DataValue, and Desc as TextValue) - the population of the Combo Control is fine. (stripped down example is below)

public class RecGeoTree : MCS.PocketPC.Record
{
public int m_nElementUID;
public string m_strElementDescription;
}

(Now I have a ComboBox populated with these RecGeoTree objects - thought they aren't RecGeoTree objects types, as ComboBoxes can't contain these objects).

But, what I want to do is to be able to get an instance of a RecGeoTree object and to set the SelectedItem in the ComboBox to the object with the same data - I know this is possible, and if I am correct I may need to do something with the ToString() in the Class. I have the following...(where m_Tiers is an ArrayList)

m_Tiers = Global.GetLocationInfo().GetTierList();
comboTier1.SelectedItem = ((TblGeoTree.RecGeoTree)m_Tiers[0]);

But this isn't working. Does anyone know what I need to do (The ComboBox control does contain an Item with the same Data and Text values as an object instances ID and Desc fields).

Thanks

Tryst



Answer this question

How do you cast an object to a ComboBox Item to Select it....

  • Matt Sipes

    Hi Tryst,

    Since you produce class RecGeoTree through inheriting class MCS.PocketPC,Record, the parent class should have an ancestor class as System.Object.

    It's suggested:

    m_Tiers = Global.GetLocationInfo().GetTierList();

    comboTier1.Items.Add(m_Tiers[0]);

    // Find the corresponding string in the combobox or simply use a variable to record index

    comboTier1.SelectedIndex = comboTier1.FindString(m_Tier[0].ToString());

    We sincerely hope this can be of help. If anything is unclear, please don't hesitate to get in touch. Any comments and further feedback from you will be welcome and highly valued.

    Cheers,

    Cleo



  • Muksh

    Yes, that helps.

    Thank you, but I need to implement this on a SmartDevice, and ComboBox controls don't have the FindString() property, so i think it is best if I use teh SelectedText property.

    Thanks

    Tryst


  • li ning

    Hi, and thanks for the reply.

    I have now implemented the ToString() method on the RecGeoTree Class, but am still not getting the ComboBox to select the correct Item.

    Is the following assignment correct Or should I be using a different Property of the ComboBox

    comboTier1.SelectedValue = ((TblGeoTree.RecGeoTree)m_Tiers[0]).ToString();


    Thanks

    Tryst


  • Michael McCallum

    It seems to work if I use the following...

    comboTier1.Text = ((TblGeoTree.RecGeoTree)m_Tiers[0]).ToString();

    which I find quite strange since I thought I would be only changing the text of what is displayed by the ComboBox.

    Tryst


  • Alistair Black

    Hi,
    You just have to override the toString method and you can hold other data in that class or even a reference to your RecGeoTree. Something like this:

    class ComboBoxObject
    {
    private string m_text;
    private RecGeoTree m_obj;

    ComboBoxObject(string text,
    RecGeoTree obj) { m_text = text; m_obj = obj; }

    public override string ToString()
    {
    return m_text;
    // or: return m_obj.StrElementDescription;
    }
    }

    // or

    class RecGeoTree : MCS.PocketPC.Record
    {
    // your stuff here

    public override string ToString()
    {
    return m_strElementDescription;
    }
    }

    // untested code

  • abdm

    Tryst wrote:
    Thank you, but I need to implement this on a SmartDevice, and ComboBox controls don't have the FindString() property, so i think it is best if I use teh SelectedText property.


    You can use the ComboBox.Items.IndexOf method to get the index of a specified item. This method uses the Object.Equals method to determ if the specified Object is in the list. So you can override the Object.Equals method if you want to implement some extra logic.


    m_Tiers = Global.GetLocationInfo().GetTierList();

    comboTier1.Items.Add(m_Tiers[0]);

    // Find the corresponding string in the combobox or simply use a variable to record index
    comboTier1.SelectedIndex = comboTier1.Items.IndexOf( m_Tier[0] );




  • How do you cast an object to a ComboBox Item to Select it....