Reputation: 3
I'll start by saying I'm not that experienced in C# and I'm writing a Windows Forms Application that writes to an XML file that PowerShell can later read the elements from at different points during configuration.
I created a ComboBox that enumerates the Time Zone DisplayNames, which works great.
ReadOnlyCollection<TimeZoneInfo> systemTimeZones;
systemTimeZones = TimeZoneInfo.GetSystemTimeZones();
cbTimeZone.DataSource = systemTimeZones;
cbTimeZone.BindingContext = new BindingContext();
I'm trying to get the StandardName of the selected DisplayName Time Zone in the ComboBox. I've found TimeZoneInfo.Local.StandardName but that's not what I'm looking for since I'm not concerned with the current system information. I want to use a SelectedIndexChanged event for the ComboBox to display the StandardName in a label. It seems there has to be a simple way that I've just missed the past 2 days of reading.
Upvotes: 0
Views: 614
Reputation: 874
use the TimeZoneInfo class e.g.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
foreach (var zone in TimeZoneInfo.GetSystemTimeZones())
{
cbTimeZones.Items.Add(zone.DisplayName);
}
}
private void cbTimeZones_SelectedIndexChanged(object sender, EventArgs e)
{
var zone = TimeZoneInfo.GetSystemTimeZones().Single(x => x.DisplayName == ((ComboBox)sender).SelectedItem.ToString());
lblTimeZone.Text = TimeZoneInfo.FindSystemTimeZoneById(zone.Id).StandardName;
}
}
Upvotes: 1