windowsforms-114804-controls-and-libraries-editors-and-simple-controls-token-edit-control-how-to-use-complex-objects-as-token-values.md
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));
}
}
}
'#1
'Override the ToString() method
Public Class Customer
Public Property ID() As Integer
Public Property Name() As String
Public Sub New(ByVal id As Int32, ByVal name As String)
Me.ID = id
Me.Name = name
End Sub
Public Overrides Function ToString() As String
Return ID.ToString()
End Function
End Class
Partial Public Class MainForm
Inherits 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")))
End Class
'#2
'Assign a unique token value
Public Class Customer
Public Property ID() As Integer
Public Property Name() As String
End Class
Partial Public Class MainForm
Inherits XtraForm
'...
Private Customers As New List(Of Customer)()
Customers.Add(New Customer() With {
.ID = 1,
.Name = "John"
})
Customers.Add(New Customer() With {
.ID = 2,
.Name = "Mary"
})
Customers.Add(New Customer() With {
.ID = 3,
.Name = "David"
})
For Each customer As Customer In Customers
tokenEdit1.Properties.Tokens.Add(New TokenEditToken(customer.Name, customer.ID))
Next customer
End Class