Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Participant
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Location
    Hawaii
    Posts
    376
    Rep Power
    17

    C# forms - ComboBox selected value


    Situation

    The form loads, pulling data from AD about a user.
    ComboBox has list of selectable items. I want the comboBox to have the "display" of the value of the item returned from Active Directory as selected in the box. A bit wordy, but read on and it will make sense, or just look at the code

    Much like an option tag of HTML. <option value="Mr">(Civilian/Contractor) Mr</option> So "(Civilian/Contractor) Mr" is the "display", and "Mr" is the value.

    In my case, the query from Active Directory returns the "value", "Mr", and I want to have the comboBox have "(Civilian/Contractor) Mr" selected.


    Here is what I have so far. This is copied from different classes to give a quick view of what is going on:


    Form itself:

    C# Code:
    //make dropdown not editable
                rankBox.DropDownStyle = ComboBoxStyle.DropDownList;
     
                //get list
                ArrayList al = new ArrayList();
                al = au.getPersonalTitles();
     
                //populate list into comboBox
                rankBox.Items.AddRange(al.ToArray());

    This part works, dropdown is there with full list.

    Here is the method getPersonalTitles:

    C# Code:
    public ArrayList getPersonalTitles()
            {
                ArrayList al = new ArrayList();
     al.Add(new WindowsApplication1.ADATUtils.AddValue("(Civilian/Contractor) Mr", "Mr"));
                al.Add(new WindowsApplication1.ADATUtils.AddValue("(Civilian/Contractor) Miss", "Miss"));
    al.Add(new WindowsApplication1.ADATUtils.AddValue("(Civilian/Contractor) Mrs", "Mrs"));
     
    return al;
    }

    And the AddValue class:

    C# Code:
    public class AddValue
            {
                private string m_Display;
                private string m_Value;
                public AddValue(string Display, string Value)
                {
                    m_Display = Display;
                    m_Value = Value;
                }
                public string Display
                {
                    get { return m_Display; }
                }
                public string Value
                {
                    get { return m_Value; }
                }
                public override string ToString()
                {
                    return m_Display;
                }
            }


    So back to the form. I can query correctly, and return the value. The following is the next line in the form code to demonstrate that:

    Code:
    Console.Writeline("Title returned from AD: --" + arRank + "--");
    which returns: Title returned from AD: --Mr--
    So I am getting the value I expect from Active Directory.

    So in the comboBox I want "(Civilian/Contractor) Mr" shown as selected....

    And I don't seem to be able to get it to work.
    Ideas?

    SK
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2004
    Location
    Hampshire, UK
    Posts
    990
    Rep Power
    316
    You need to set the DisplayMember and ValueMember properties of the combobox... so

    Code:
    class MyObject
    {
      ...
    
      public String Display
      {
        get { return _display; }
      }
    
      public String Value
      {
        get { return _value; }
      }
    }
    then in your form you can do this...

    Code:
    combobox.DisplayMember = "Display";
    combobox.ValueMember = "Value";
    combobox.DataSource = myArrayOfMyObjectValues;
    Then the SelectedItem property will return a MyObject instance, which you can cast then do what you want with.

    Hope that makes sense
    Scott Perham, BSc (hons), CCNA, MCPD
  4. #3
  5. No Profile Picture
    Participant
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Location
    Hawaii
    Posts
    376
    Rep Power
    17
    Thanks for the fast reply scoper.
    I think I follow...I'll try it now and post back shortly.

    SK
  6. #4
  7. No Profile Picture
    Participant
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Location
    Hawaii
    Posts
    376
    Rep Power
    17
    Well no go.

    I tried this on the form:

    C# Code:
     //make dropdown not editable
                rankBox.DropDownStyle = ComboBoxStyle.DropDownList;
     
                //get list
                ArrayList al = new ArrayList();
                al = au.getPersonalTitles();
     
                //populate list into comboBox
                //rankBox.Items.AddRange(al.ToArray());
     
    //NEW SUGGESTION:
                rankBox.DisplayMember = "Display";
                rankBox.ValueMember = "Value";
                rankBox.DataSource = al.ToArray();
     
                rankBox.SelectedItem = arRank;


    Box is still populated with selectable items, but there is nothing "selected" it still defaults to 0 as the selectedIndex.

    ideas?

    SK
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2004
    Location
    Hampshire, UK
    Posts
    990
    Rep Power
    316
    I assume that arRank is an instance of your AddValue class... if it is you will need to make sure that the same pointer exists in the combobox. If you populate two instances of a class with exactly the same data they will still not be equal.

    There are two things you can do without modifying the way your code works... one would be to implement IEquatable in your AddValue class... (which will make you define the Equals method, which is what the combobox will use to determin if it can find the member within its items collection) or secondly change the AddValue class to a struct i am not sure if this will cause any adverse effects in your code anywhere else, but it will make the combobox work in the way you want it to.

    As an afterthougth.. you might be able to simply override the Equals method in your AddValue class like so:

    Code:
    public override bool Equals(object o)
    {
        if (!(o is AddValue)) return false;
        AddValue av = (AddValue)o;
        return av.Display == this.Display && av.Value == this.Value;
    }
    Last edited by scoper; June 20th, 2008 at 06:55 PM. Reason: Afterthought...
    Scott Perham, BSc (hons), CCNA, MCPD
  10. #6
  11. No Profile Picture
    Participant
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Location
    Hawaii
    Posts
    376
    Rep Power
    17
    Negative, the arRank variable is a string that represents what is returned by Active Directory.

    In this case "Mr".

    Hope that makes sense.

    SK
  12. #7
  13. No Profile Picture
    Participant
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Location
    Hawaii
    Posts
    376
    Rep Power
    17
    Well working it in psuedo, its like this:


    C# Code:
    if (valueReturnedFromAD == anyValueInComboBox)
    {
     return index of theMatchingValueInComboBox;
    set comboBox.SelectedIndex = returnedIndexFromAboveLine;
    }


    Just not sure how to do the "anyValueInComboBox".

    SK
  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2004
    Location
    Hampshire, UK
    Posts
    990
    Rep Power
    316
    ok... i now see the issue (took a while )

    The items collection is just a collection of objects stored in the combobox control, you define which properties it should use for display and value by using the DisplayMember and ValueMember properties as described above.... now if you want to set the selected item you have to set it to the same object which is in the collection...

    so if you have a collection of AddValue objects you need to set the SelectedItem to one of the AddValue objects in the collection... not just a string (get it?)

    In your case you can probably just use the SelectedValue or SelectedText properties

    Bear in mind, if the ValueMember points to a property returning a String then the SelectedValue you set must also be a String.

    Hopefully that will help you solve the problem
    Scott Perham, BSc (hons), CCNA, MCPD
  16. #9
  17. No Profile Picture
    Participant
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Location
    Hawaii
    Posts
    376
    Rep Power
    17
    Yeah,

    I follow you, just can't seem to get it to work.

    I changed it to loop through the arraylist as it adds the items one at a time, still no luck on making the one selected.

    C# Code:
     //get list
                ArrayList al = new ArrayList();
                al = au.getPersonalTitles();
     
                //populate list into comboBox
                //rankBox.Items.AddRange(al.ToArray());
                //MessageBox.Show("rank value for this user: " + arRank);
                //rankBox.DataSource = al.ToArray();
     
                for (int j = 0; j < al.ToArray().Length; j++) 
                {
                    if (arRank.Equals((WindowsApplication1.ADATUtils.AddValue)al[j]))
                    {
                        rankBox.SelectedIndex = j;
                    }
                    rankBox.Items.Add((WindowsApplication1.ADATUtils.AddValue)al[j]);
                }





    SK
  18. #10
  19. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2004
    Location
    Hampshire, UK
    Posts
    990
    Rep Power
    316
    Originally Posted by scryptKiddy
    if (arRank.Equals((WindowsApplication1.ADATUtils.AddValue)al[j]))
    You said that arRank was a string??

    That will always return false!

    surely it should be:

    Code:
    if (arRank.Equals(((WindowsApplication1.ADATUtils.AddValue)al[j]).Value))
    or
    Code:
    if (arRank.Equals(((WindowsApplication1.ADATUtils.AddValue)al[j]).Display))
    No?

    Also... in the code you posted, line 16 should really be before line 12 otherwise you will probably get an ArgumentOutOfRange exception
    Last edited by scoper; June 20th, 2008 at 08:39 PM.
    Scott Perham, BSc (hons), CCNA, MCPD
  20. #11
  21. No Profile Picture
    Participant
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Location
    Hawaii
    Posts
    376
    Rep Power
    17
    Hmm...interesting.

    Made a few changes, this is what I'm trying now:

    C# Code:
    for (int j = 0; j < al.ToArray().Length; j++)
                    {
                        label45.Text = al[j].ToString();
                        if (arRank.Equals(((WindowsApplication1.ADATUtils.AddValue)al[j]).Value))
                        //if (arRank.Equals(al[j].ToString()))
                        {
                            rankBox.SelectedIndex = j;
                        }
                        //rankBox.Items.Add((WindowsApplication1.ADATUtils.AddValue)al[j]);
                        rankBox.Items.Add(al[j].ToString());
                    }


    The label 45 (used for this debugging) does show "(Civilian/Contractor) Mr" But the dropdown menu is still not showing it as selected.

  22. #12
  23. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2004
    Location
    Hampshire, UK
    Posts
    990
    Rep Power
    316
    Right...

    Firstly, you will need to move line 10 before line 4...
    Secondly, you have changed the code to add a string to the item collection! why was this?

    Thirdly, i wrote this just now... and it works fine:

    c# Code:
     
    List<AddValue> _list = new List<AddValue>();
    public Form1()
    {
    	InitializeComponent();
    	_list.Add(new AddValue("val 1", "dis 1"));
    	_list.Add(new AddValue("val 2", "dis 2"));
    	_list.Add(new AddValue("val 3", "dis 3"));
    	_list.Add(new AddValue("val 4", "dis 4"));
    	_list.Add(new AddValue("val 5", "dis 5"));
     
    	comboBox1.DisplayMember = "Display";
    	comboBox1.ValueMember = "Value";
     
    	for (int i = 0; i < _list.Count; i++)
    	{
    		comboBox1.Items.Add(_list[i]);
    		if (_list[i].Value == "val 4")
    			comboBox1.SelectedIndex = i;
    	}
    }


    Although instead of the loop it should really be:

    Code:
    comboBox1.DataSource = _list;
    comboBox1.SelectedValue = "val 4";
    Step through it and find out if it ever gets to line 7 ?? I am guessing it doesn't because the code as it was in that post would throw an exception on that line... this means that the value in the AddValue class and the arRank variable contain different strings

    Comments on this post

    • scryptKiddy agrees : That will work for the selected part.
    Last edited by scoper; June 20th, 2008 at 08:57 PM.
    Scott Perham, BSc (hons), CCNA, MCPD
  24. #13
  25. No Profile Picture
    Participant
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Location
    Hawaii
    Posts
    376
    Rep Power
    17
    Awesome,

    this works now:

    C# Code:
     for (int j = 0; j < al.ToArray().Length; j++)
                    {
     
                        rankBox.Items.Add(al[j].ToString());
                        if (arRank.Equals(((WindowsApplication1.ADATUtils.AddValue)al[j]).Value))
                        {
                            rankBox.SelectedIndex = j;
                        }
                    }

    Now the form has "(Civilian/Contractor) Mr" selected, nice!

    But when I try to see what the form thinks is the "value" for what is selected (see below code), it errors:

    C# Code:
    System.NullReferenceException: Object reference not set to an instance of an object.


    Here is the line it errors on, where I call SelectedValue.ToString().

    C# Code:
    private void rankBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                label45.Text = rankBox.SelectedValue.ToString();
            }


    SO I can get it selected, but passing back in the "value" of what the user "selects" is the last part of this.

    Ideas on that?

    Thanks so far scoper!

    SK
  26. #14
  27. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2004
    Location
    Hampshire, UK
    Posts
    990
    Rep Power
    316
    You should be able to get the SelectedItem and cast it to an AddValue instance... then you can just use the .Value property - be aware though that this event will be fired when you set the selected index
    Scott Perham, BSc (hons), CCNA, MCPD
  28. #15
  29. No Profile Picture
    Participant
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Location
    Hawaii
    Posts
    376
    Rep Power
    17
    Hmm... I think I follow ya.

    But when I tried this:

    C# Code:
    private void rankBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                label45.Text = ((WindowsApplication1.ADATUtils.AddValue)rankBox.SelectedItem).Value; 
            }


    I got this error:

    Code:
    System.InvalidCastException: Unable to cast object of type 'System.String' to type 'AddValue'.
    if I change it to:
    C# Code:
    private void rankBox_SelectedIndexChanged(object sender, EventArgs e)
            {
    label45.Text = rankBox.SelectedItem.ToString();
    }


    It makes label45 change to "(Contractor/Civilian) Mr", the "display", not the "value".

    Hope that makes sense.
    Last edited by scryptKiddy; June 20th, 2008 at 09:26 PM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo