How to add a text and value to NSComboBox?

JudefJudef AEMember
edited April 2016 in Xamarin.iOS

Hello,

I have list of Employees with Name and EmployeeNo, how can we add this to NsComboxBox something like we do in Window dropdownlist as Text and value. I could only add the text using NsCombobox.Add("EmployeeName").

Tagged:

Best Answer

  • KMullinsKMullins US Xamurai
    Accepted Answer

    Hi @Judef,

    Sorry this has taken so long but it turned out to be a more difficult solution that I initially expected. I've updated the MacDatabase sample app already and I plan to get the Xamarin.Mac documentation updated today.

    Until that time, here is a quick example. First, instead of managing ComboBox items using the internal list via the Add method, you need to use a NSComboBoxDataSource instance and set the Autocompletes and Uses Data Source flags for your NSComboBox in Xcode's Interface Builder.

    Next, create class that inherits from NSComboBoxDataSource. For example, this class works with any SQLite database:

    using System;
    using System.Data;
    using System.IO;
    using Mono.Data.Sqlite;
    using Foundation;
    using AppKit;
    
    namespace MacDatabase
    {
        public class ComboBoxDataSource : NSComboBoxDataSource
        {
            #region Private Variables
            private SqliteConnection _conn = null;
            private string _tableName = "";
            private string _IDField = "ID";
            private string _displayField = "";
            private nint _recordCount = 0;
            #endregion
    
            #region Computed Properties
            public SqliteConnection Conn {
                get { return _conn; }
                set { _conn = value; }
            }
    
            public string TableName {
                get { return _tableName; }
                set { 
                    _tableName = value;
                    _recordCount = GetRecordCount ();
                }
            }
    
            public string IDField {
                get { return _IDField; }
                set {
                    _IDField = value; 
                    _recordCount = GetRecordCount ();
                }
            }
    
            public string DisplayField {
                get { return _displayField; }
                set { 
                    _displayField = value; 
                    _recordCount = GetRecordCount ();
                }
            }
    
            public nint RecordCount {
                get { return _recordCount; }
            }
            #endregion
    
            #region Constructors
            public ComboBoxDataSource (SqliteConnection conn, string tableName, string displayField)
            {
                // Initialize
                this.Conn = conn;
                this.TableName = tableName;
                this.DisplayField = displayField;
            }
    
            public ComboBoxDataSource (SqliteConnection conn, string tableName, string idField, string displayField)
            {
                // Initialize
                this.Conn = conn;
                this.TableName = tableName;
                this.IDField = idField;
                this.DisplayField = displayField;
            }
            #endregion
    
            #region Private Methods
            private nint GetRecordCount ()
            {
                bool shouldClose = false;
                nint count = 0;
    
                // Has a Table, ID and display field been specified?
                if (TableName !="" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT count({IDField}) FROM [{TableName}]";
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read ()) {
                                // Read count from query
                                var result = (long)reader [0];
                                count = (nint)result;
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return the number of records
                return count;
            }
            #endregion
    
            #region Public Methods
            public string IDForIndex (nint index)
            {
                NSString value = new NSString ("");
                bool shouldClose = false;
    
                // Has a Table, ID and display field been specified?
                if (TableName != "" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT {IDField} FROM [{TableName}] ORDER BY {DisplayField} ASC LIMIT 1 OFFSET {index}";
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read ()) {
                                // Read the display field from the query
                                value = new NSString ((string)reader [0]);
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return results
                return value;
            }
    
            public string ValueForIndex (nint index)
            {
                NSString value = new NSString ("");
                bool shouldClose = false;
    
                // Has a Table, ID and display field been specified?
                if (TableName != "" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT {DisplayField} FROM [{TableName}] ORDER BY {DisplayField} ASC LIMIT 1 OFFSET {index}";
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read ()) {
                                // Read the display field from the query
                                value = new NSString ((string)reader [0]);
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return results
                return value;
            }
    
            public string IDForValue (string value)
            {
                NSString result = new NSString ("");
                bool shouldClose = false;
    
                // Has a Table, ID and display field been specified?
                if (TableName != "" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT {IDField} FROM [{TableName}] WHERE {DisplayField} = @VAL";
    
                        // Populate parameters
                        command.Parameters.AddWithValue ("@VAL", value);
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read ()) {
                                // Read the display field from the query
                                result = new NSString ((string)reader [0]);
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return results
                return result;
            }
            #endregion 
    
            #region Override Methods
            public override nint ItemCount (NSComboBox comboBox)
            {
                return RecordCount;
            }
    
            public override NSObject ObjectValueForItem (NSComboBox comboBox, nint index)
            {
                NSString value = new NSString ("");
                bool shouldClose = false;
    
                // Has a Table, ID and display field been specified?
                if (TableName != "" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT {DisplayField} FROM [{TableName}] ORDER BY {DisplayField} ASC LIMIT 1 OFFSET {index}";
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read ()) {
                                // Read the display field from the query
                                value = new NSString((string)reader [0]);
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return results
                return value;
            }
    
            public override nint IndexOfItem (NSComboBox comboBox, string value)
            {
                bool shouldClose = false;
                bool found = false;
                string field = "";
                nint index = NSRange.NotFound;
    
                // Has a Table, ID and display field been specified?
                if (TableName != "" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT {DisplayField} FROM [{TableName}] ORDER BY {DisplayField} ASC";
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read () && !found) {
                                // Read the display field from the query
                                field = (string)reader [0];
                                ++index;
    
                                // Is this the value we are searching for?
                                if (value == field) {
                                    // Yes, exit loop
                                    found = true;
                                }
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return results
                return index;
            }
    
            public override string CompletedString (NSComboBox comboBox, string uncompletedString)
            {
                bool shouldClose = false;
                bool found = false;
                string field = "";
    
                // Has a Table, ID and display field been specified?
                if (TableName != "" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Escape search string
                    uncompletedString = uncompletedString.Replace ("'", "");
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT {DisplayField} FROM [{TableName}] WHERE {DisplayField} LIKE @VAL";
    
                        // Populate parameters
                        command.Parameters.AddWithValue ("@VAL", uncompletedString + "%");
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read ()) {
                                // Read the display field from the query
                                field = (string)reader [0];
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return results
                return field;
            }
            #endregion
        }
    }
    

    The overrides are of the most interest here. For example, ItemCount returns the number of items in our backing store. ObjectValueForItem returns the text of the item displayed to the user for an individual row. IndexOfItem returns the row number for a text value. CompletedString returns the first matching text value for a partially typed in value.

    The following code wires up the datasource to the ComboBox and responds to user interaction:

            // Configure Employee selector dropdown
            EmployeeSelector.DataSource = new ComboBoxDataSource (Conn, "People", "Name");
    
            // Wireup events
            EmployeeSelector.Changed += (sender, e) => {
                // Get ID
                var id = DataSource.IDForValue (EmployeeSelector.StringValue);
                LoadSelectedPerson (id);
            };
    
            EmployeeSelector.SelectionChanged += (sender, e) => {
                // Get ID
                var id = DataSource.IDForIndex (EmployeeSelector.SelectedIndex);
                LoadSelectedPerson (id);
            };
    
            // Auto select the first person
            EmployeeSelector.StringValue = DataSource.ValueForIndex (0);
            Person = new PersonModel (Conn, DataSource.IDForIndex(0));
    

    First, it provides a database connection, the table name and the field to display when populating the ComboBox. Next, it wires up the autocomplete and selection from the dropdown list events. Finally, it initially populates the ComboBox with the first record from the database.

    Please let me know if you have any questions.

    Kevin

Answers

  • intvsteveintvsteve USMember ✭✭

    Did you examine the Values property, along with the Add(NSOjbect) and Add(NSObject[]) methods? Those, along with DataSource, provide a flexible way so you can have arbitrary NSObject data be associated with the item that's chosen. You may need to make a wrapper NSObject type.

  • KMullinsKMullins USMember, Xamarin Team Xamurai

    Hi @Judef,

    Please take a look at the Working with Menu Controls section of our Standard Controls document. If you are still having an issue, please let me know and I'll see if I can't help you out.

    Thanks,

    Kevin

  • JudefJudef AEMember

    Hi Kevin, thanks for the link. I went through it, but I could manage to add only text to NsCombobox. Below is my code. Currently I am appending employeeno as to employeename i.e drpUser.Add(new NSString(EmployeeName + "(" + EmployeeNo + ")"), I want employeeno to be hidden from the view, something like value of the text. Whenever the user selects a text in the combobox in the SelectionChanged event I want to fetch the corresponding employeeno?

                    drpUser.RemoveAll ();
    
                    for (IndexCounter = 0; IndexCounter < datasetEmp.Rows.Count; IndexCounter++) {
                        drpUser.Add (new NSString (dtEmp.Rows [IndexCounter].ItemArray.GetValue (1).ToString () + "(" + dtEmp.Rows [IndexCounter].ItemArray.GetValue (0).ToString () + ")"));
                    }
    
  • KMullinsKMullins USMember, Xamarin Team Xamurai

    Hi @Judef,

    Let me take a look at it and see if I can't whip up a quick working example for you. Please let me know if you need anything else.

    Thanks,

    Kevin

  • KMullinsKMullins USMember, Xamarin Team Xamurai
    Accepted Answer

    Hi @Judef,

    Sorry this has taken so long but it turned out to be a more difficult solution that I initially expected. I've updated the MacDatabase sample app already and I plan to get the Xamarin.Mac documentation updated today.

    Until that time, here is a quick example. First, instead of managing ComboBox items using the internal list via the Add method, you need to use a NSComboBoxDataSource instance and set the Autocompletes and Uses Data Source flags for your NSComboBox in Xcode's Interface Builder.

    Next, create class that inherits from NSComboBoxDataSource. For example, this class works with any SQLite database:

    using System;
    using System.Data;
    using System.IO;
    using Mono.Data.Sqlite;
    using Foundation;
    using AppKit;
    
    namespace MacDatabase
    {
        public class ComboBoxDataSource : NSComboBoxDataSource
        {
            #region Private Variables
            private SqliteConnection _conn = null;
            private string _tableName = "";
            private string _IDField = "ID";
            private string _displayField = "";
            private nint _recordCount = 0;
            #endregion
    
            #region Computed Properties
            public SqliteConnection Conn {
                get { return _conn; }
                set { _conn = value; }
            }
    
            public string TableName {
                get { return _tableName; }
                set { 
                    _tableName = value;
                    _recordCount = GetRecordCount ();
                }
            }
    
            public string IDField {
                get { return _IDField; }
                set {
                    _IDField = value; 
                    _recordCount = GetRecordCount ();
                }
            }
    
            public string DisplayField {
                get { return _displayField; }
                set { 
                    _displayField = value; 
                    _recordCount = GetRecordCount ();
                }
            }
    
            public nint RecordCount {
                get { return _recordCount; }
            }
            #endregion
    
            #region Constructors
            public ComboBoxDataSource (SqliteConnection conn, string tableName, string displayField)
            {
                // Initialize
                this.Conn = conn;
                this.TableName = tableName;
                this.DisplayField = displayField;
            }
    
            public ComboBoxDataSource (SqliteConnection conn, string tableName, string idField, string displayField)
            {
                // Initialize
                this.Conn = conn;
                this.TableName = tableName;
                this.IDField = idField;
                this.DisplayField = displayField;
            }
            #endregion
    
            #region Private Methods
            private nint GetRecordCount ()
            {
                bool shouldClose = false;
                nint count = 0;
    
                // Has a Table, ID and display field been specified?
                if (TableName !="" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT count({IDField}) FROM [{TableName}]";
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read ()) {
                                // Read count from query
                                var result = (long)reader [0];
                                count = (nint)result;
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return the number of records
                return count;
            }
            #endregion
    
            #region Public Methods
            public string IDForIndex (nint index)
            {
                NSString value = new NSString ("");
                bool shouldClose = false;
    
                // Has a Table, ID and display field been specified?
                if (TableName != "" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT {IDField} FROM [{TableName}] ORDER BY {DisplayField} ASC LIMIT 1 OFFSET {index}";
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read ()) {
                                // Read the display field from the query
                                value = new NSString ((string)reader [0]);
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return results
                return value;
            }
    
            public string ValueForIndex (nint index)
            {
                NSString value = new NSString ("");
                bool shouldClose = false;
    
                // Has a Table, ID and display field been specified?
                if (TableName != "" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT {DisplayField} FROM [{TableName}] ORDER BY {DisplayField} ASC LIMIT 1 OFFSET {index}";
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read ()) {
                                // Read the display field from the query
                                value = new NSString ((string)reader [0]);
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return results
                return value;
            }
    
            public string IDForValue (string value)
            {
                NSString result = new NSString ("");
                bool shouldClose = false;
    
                // Has a Table, ID and display field been specified?
                if (TableName != "" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT {IDField} FROM [{TableName}] WHERE {DisplayField} = @VAL";
    
                        // Populate parameters
                        command.Parameters.AddWithValue ("@VAL", value);
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read ()) {
                                // Read the display field from the query
                                result = new NSString ((string)reader [0]);
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return results
                return result;
            }
            #endregion 
    
            #region Override Methods
            public override nint ItemCount (NSComboBox comboBox)
            {
                return RecordCount;
            }
    
            public override NSObject ObjectValueForItem (NSComboBox comboBox, nint index)
            {
                NSString value = new NSString ("");
                bool shouldClose = false;
    
                // Has a Table, ID and display field been specified?
                if (TableName != "" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT {DisplayField} FROM [{TableName}] ORDER BY {DisplayField} ASC LIMIT 1 OFFSET {index}";
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read ()) {
                                // Read the display field from the query
                                value = new NSString((string)reader [0]);
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return results
                return value;
            }
    
            public override nint IndexOfItem (NSComboBox comboBox, string value)
            {
                bool shouldClose = false;
                bool found = false;
                string field = "";
                nint index = NSRange.NotFound;
    
                // Has a Table, ID and display field been specified?
                if (TableName != "" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT {DisplayField} FROM [{TableName}] ORDER BY {DisplayField} ASC";
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read () && !found) {
                                // Read the display field from the query
                                field = (string)reader [0];
                                ++index;
    
                                // Is this the value we are searching for?
                                if (value == field) {
                                    // Yes, exit loop
                                    found = true;
                                }
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return results
                return index;
            }
    
            public override string CompletedString (NSComboBox comboBox, string uncompletedString)
            {
                bool shouldClose = false;
                bool found = false;
                string field = "";
    
                // Has a Table, ID and display field been specified?
                if (TableName != "" && IDField != "" && DisplayField != "") {
                    // Is the database already open?
                    if (Conn.State != ConnectionState.Open) {
                        shouldClose = true;
                        Conn.Open ();
                    }
    
                    // Escape search string
                    uncompletedString = uncompletedString.Replace ("'", "");
    
                    // Execute query
                    using (var command = Conn.CreateCommand ()) {
                        // Create new command
                        command.CommandText = $"SELECT {DisplayField} FROM [{TableName}] WHERE {DisplayField} LIKE @VAL";
    
                        // Populate parameters
                        command.Parameters.AddWithValue ("@VAL", uncompletedString + "%");
    
                        // Get the results from the database
                        using (var reader = command.ExecuteReader ()) {
                            while (reader.Read ()) {
                                // Read the display field from the query
                                field = (string)reader [0];
                            }
                        }
                    }
    
                    // Should we close the connection to the database
                    if (shouldClose) {
                        Conn.Close ();
                    }
                }
    
                // Return results
                return field;
            }
            #endregion
        }
    }
    

    The overrides are of the most interest here. For example, ItemCount returns the number of items in our backing store. ObjectValueForItem returns the text of the item displayed to the user for an individual row. IndexOfItem returns the row number for a text value. CompletedString returns the first matching text value for a partially typed in value.

    The following code wires up the datasource to the ComboBox and responds to user interaction:

            // Configure Employee selector dropdown
            EmployeeSelector.DataSource = new ComboBoxDataSource (Conn, "People", "Name");
    
            // Wireup events
            EmployeeSelector.Changed += (sender, e) => {
                // Get ID
                var id = DataSource.IDForValue (EmployeeSelector.StringValue);
                LoadSelectedPerson (id);
            };
    
            EmployeeSelector.SelectionChanged += (sender, e) => {
                // Get ID
                var id = DataSource.IDForIndex (EmployeeSelector.SelectedIndex);
                LoadSelectedPerson (id);
            };
    
            // Auto select the first person
            EmployeeSelector.StringValue = DataSource.ValueForIndex (0);
            Person = new PersonModel (Conn, DataSource.IDForIndex(0));
    

    First, it provides a database connection, the table name and the field to display when populating the ComboBox. Next, it wires up the autocomplete and selection from the dropdown list events. Finally, it initially populates the ComboBox with the first record from the database.

    Please let me know if you have any questions.

    Kevin

  • KMullinsKMullins USMember, Xamarin Team Xamurai

    All,

    I've updated the Xamarin.Mac documentation to include the new information on working with NSComboBox in a Xamarin.Mac app. See for more information:

    As usual, let me know if there are any question.

    Kevin

  • JudefJudef AEMember

    Thank you Kevin, this helped a lot. Much appreciated.

  • MunirMustafaMunirMustafa USMember

    HI Kevin,
    I am new of mac forms and having some issues. It will be great if I am able to get your help to sort out some queries.

    All the above stuff is very informative and working fine. But I need some more things to be cleared. Can you please
    tell me how ?

    1 - Order the combobox item.
    2- Can we bind combobox data source with query/List or dataset.
    3- Can we pass the datatable or list instead of TableName if not how order table items

    EmployeeSelector.DataSource = new ComboBoxDataSource (Conn, "People", "Name")

    Best Regards

    Munir

  • TroyChardTroyChard CAMember ✭✭

    As an alternative to the accepted answer. Just maintain a separate lookup of your objects.

                // Ensure NSComboBox behavior changed from "Editable" to "Selectable"
                MyCombo.RemoveAll();
    
                List<object> comboLookup = new List<object>();
    
                MyCombo.Add(new NSString("Alpha"));
                comboLookup.Add( "AlphaValue" );
    
                MyCombo.Add(new NSString("Bravo"));
                comboLookup.Add( "BravoValue");
    
                MyCombo.Add(new NSString("Charlie"));
                comboLookup.Add( "CharlieValue" );
    
                MyCombo.SelectItem(0);
    
                MyCombo.SelectionChanged += (object sender, EventArgs e) =>
                {
                    int i = (int)MyCombo.SelectedIndex;
                    this.MyLabel.StringValue = $"SelectedIndex:{i} Value:{comboLookup[i]}";
                };
    

    Note: Presumably this approach could be encapsulated in a derived class ( I leave as exercise for reader ;) )

Sign In or Register to comment.