Reputation: 2004
I have a dropdownlist with with a few options. When the user changes one of these options in the dropdownlist, I want to create a note. The problem is I want the note to say what change was made. So for example this code creates a note when the currency dropdownlist is changed. So if the current value is "Euro" and the user changes it to "Sterling" I want the note to say "currency changed from Euro to Sterling" etc. I know by using selectedItem I can get the new value selected, but how to I get the value that was in the dropdownlist before the change was made?
if (cust.Currency.ToString() != ddlCustomerCurrency.SelectedItem.Text)
{
Customer.Notes.InsertNote(cust.ID, Company.Current.CompanyID, DateTime.Now, "Currency changed from '" + /*Previous value */ + "' to '" + ddlCustomerCurrency.SelectedItem.Text + "'", CurrentUser.UserID, 1);
}
Upvotes: 0
Views: 2031
Reputation: 23867
You can save the current value to ddlDropDown's tag property, so you would always have the last selected value as 'previous value' there.
Sample:
void Main()
{
var currencies = new List<Currency>() {
new Currency { Code="EUR", Name="Euro"},
new Currency { Code="USD", Name="US Dollars"},
new Currency { Code="AUD", Name="Australian Dollars"},
new Currency { Code="JPY", Name="Japanese Yen"},
};
var customer = new Customer { Id=1, Currency="", Notes=new List<string>()};
Form f = new Form {Text="Sample"};
ComboBox ddlCustomerCurrency = new ComboBox { Top=10, Left=10, Tag="",
DataSource=currencies, DisplayMember="Name"};
Button b = new Button {Text = "Show Customer Notes", Top=60, Left = 10};
f.Controls.Add(ddlCustomerCurrency);
f.Controls.Add(b);
ddlCustomerCurrency.SelectedIndexChanged += (sender, args) => {
var cmb = sender as ComboBox;
if (cmb != null)
{
var currency = cmb.SelectedItem as Currency;
var oldValue= cmb.Tag;
if ( currency != null && oldValue != currency.Name )
{
customer.Notes.Add( string.Format(
"\nOld Currency:{0}, New Currency:{1}, Ticks:{2}",
oldValue, currency.Name, DateTime.Now.Ticks) );
cmb.Tag = currency.Name;
}
}
};
b.Click += (sender,args) => {
if (customer.Notes.Any ())
MessageBox.Show( customer.Notes.Last () );
};
f.Show();
}
class Currency
{
public string Code { get; set; }
public string Name { get; set; }
}
class Customer
{
public int Id { get; set; }
public string Currency { get; set; }
public List<string> Notes { get; set; }
}
Upvotes: 0
Reputation: 3609
You can store the initial and subsequent values of the dropdown in-memory. Then, every time the OnChange event is called, simply look at the "current" value and compare it to the value sent to the OnChange handler.
EDIT: something like this might do the trick:
public class MyClass
{
private string _currentSelectedCurrency;
public void DdlCustomerCurrency_OnChange(object sender, EventArgs e)
{
if (cust.Currency.ToString() != ddlCustomerCurrency.SelectedItem.Text)
{
Customer.Notes.InsertNote(cust.ID, Company.Current.CompanyID, DateTime.Now, "Currency changed from '" + _currentSelectedCurrency +"' to '" + ddlCustomerCurrency.SelectedItem.Text + "'", CurrentUser.UserID, 1);
_currentSelectedCurrency = ((DropDownList) sender).Text
}
}
}
Upvotes: 2