Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Use complex objects as token values

  • 2 minutes to read

The code below illustrates how to override the ToString() method for complex objects, stored as the TokenEditToken values.

//#1
//Override the ToString() method
public class Customer {
    public int ID { get; set; }
    public string Name { get; set; }

    public Customer(Int32 id, string name) {
        this.ID = id;
        this.Name = name;
    }

    public override string ToString() {
        return ID.ToString();
    }
}


public partial class MainForm : XtraForm {
    //...
        tokenEdit1.Properties.Tokens.Add(new TokenEditToken("Mary", new Customer(1, "Mary")));
        tokenEdit1.Properties.Tokens.Add(new TokenEditToken("John", new Customer(2, "John")));
        tokenEdit1.Properties.Tokens.Add(new TokenEditToken("David", new Customer(3, "David")));
}

//#2
//Assign a unique token value
public class Customer {
    public int ID { get; set; }
    public string Name { get; set; }
    }
}

public partial class MainForm : XtraForm {
    //...
        List<Customer> Customers = new List<Customer>();
        Customers.Add(new Customer() { ID = 1, Name = "John" });
        Customers.Add(new Customer() { ID = 2, Name = "Mary" });
        Customers.Add(new Customer() { ID = 3, Name = "David" });
        foreach (Customer customer in Customers) {
            tokenEdit1.Properties.Tokens.Add(new TokenEditToken(customer.Name, customer.ID));
        }
    }
}