Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to develop a Digital Clock in a Windows Form that allows the user to choose the TimeZone they want to display from a ListBox. How can I convert the SelectedItem in the event handler to the corresponding TimeZone? Thx.

MIDL
lb1.Items.Add("(UTC-12:00) International Date Line West");
           lb1.Items.Add("(UTC-11:00) Coordinated Universal Time-11");
           lb1.Items.Add("(UTC-11:00) Samoa");
           lb1.Items.Add("(UTC-10:00) Hawaii");

        public void timer1_Tick(object sender, EventArgs e)
        {
            DateTime timeUtc = DateTime.UtcNow;
            TimeZoneInfo HstZone = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time");
            TimeZoneInfo CstZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
            DateTime HstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, HstZone);
            DateTime CstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, CstZone);

            if (sender == timer1)
            {
                LocalDisplay.Text = DateTime.Now.ToString();
                Clock2.Text = DateTime.UtcNow.ToString();
                Clock4.Text = HstTime.ToString();
                Clock3.Text = CstTime.ToString();
                
            }
        }
Posted
Updated 15-Jul-11 11:41am
v2
Comments
Dr.Walt Fair, PE 15-Jul-11 17:42pm    
Fixed the code formatting due to a misplaced </pre> tag.

you can add a TimeZoneInfo objects into the listBox

then get the seleteditem object ( TimeZoneInfo object ) from the listbox.
 TimeZoneInfo HstZone ; 
....
lb1.Items.Add(HstZone.ToString() );


...
public void timer1_Tick(object sender, EventArgs e)
       {
     TimeZoneInfo selectedtimeZone  = (TimeZoneInfo)lb1.SelectedItem ; 

}



good luck.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 15-Jul-11 23:04pm    
This is the reasonable answer; my 5.
Your method could be slightly improved in part of string presentation of the list.

Please see my solution.
--SA
The correct and practical method so far is the one by Salmen.

In case the strings shown in dialog box are not perfectly nice, this method could be slightly improved.
First, you need to understand the simple fact: list boxes and combo boxes can populate lists with objects of any type. When the objects are accessed, items should be cast to the type of the object added, as Salmen demonstrated. The only problem is string representation of the item. It is obtained by .NET from what the method object.ToString returns.

Hence, the general method is: to create appropriate type for the list item and, class or structure, and override its ToString method:

C#
internal struct ItemHelper {
   internal ItemHelper(TimeZoneInfo info) { this.fInfo = info; }
   public override string ToString() { return FormatInfo(this.fInfo); }
   internal TimeZoneInfo Info { get fInfo; }
   private TimeZoneInfo fInfo;
   private static FormatInfo(TimeZoneInfo info) {
      return /* info formatted the right way */;
   } //FormatInfo
} //struct ItemHelper


Add items of this type to the list items and cast them back to ItemHelper when time zone info is needed.

—SA
 
Share this answer
 
Comments
Salmen Essridi 16-Jul-11 7:34am    
it really beneficial, My 5 for your explication.
Sergey Alexandrovich Kryukov 16-Jul-11 15:18pm    
Thank you, Salmen.
--SA
77Jeff 16-Jul-11 14:16pm    
JOAT-MON seemed to have the specifics that I was looking for in order to get the code to work. Your explanation was helpful and I was able to implement it as well.
Thx for the post.
Sergey Alexandrovich Kryukov 16-Jul-11 15:18pm    
If so, will you accept the answer formally (green button)? You can accept more than one.
Thank you.
--SA
Rather than adding individual time zones, you should get all the time zones on the system:
C#
lb1.Items.AddRange ( TimeZoneInfo.GetSystemTimeZones ().ToArray () );
lb1.DisplayMember = "DisplayName";

Then wherever you need to show the time zone that is selected:
C#
this.textBox1.Text = ( ( TimeZoneInfo )lb1.SelectedItem ).ToString ();

And to set the time on the digital clock:
C#
yourDigitalClockControl.Value = TimeZoneInfo.ConvertTimeFromUtc ( DateTime.UtcNow, ( TimeZoneInfo )lb1.SelectedItem );
 
Share this answer
 
v4
Comments
77Jeff 15-Jul-11 19:38pm    
I am still trying to get the SelectedIndexChanged Event to show the selected timezone with its UtcOffset in a TextBox under the timer1_Tick Event. Thanks for the code reduction.
JOAT-MON 15-Jul-11 20:01pm    
I modified my answer to show how this can be done.
77Jeff 15-Jul-11 20:10pm    
Thanks, that got me through most of it.
JOAT-MON 15-Jul-11 20:13pm    
No problem. I added one more item to show how to convert the time for setting you digital clock control.
77Jeff 15-Jul-11 20:29pm    
Thanks again, just cleaning up the mess now... works well.
Simple statement :

textBox1.Text = ( ( TimeZoneInfo )lb1.SelectedItem ).ToString ();

good luck :) .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900