|
The C# Column
The Timezone application - II
In
the last article we had declared the location class containing data members
to hold hours, minutes, city name and x-y coordinates of the city on the map,
and then populated the array in the Form1 class.
Now comes the real part.
private void Form1_MouseMove ( object sender, MouseEventArgs
e )
{
// Previous code
int i = search ( new Point ( e.X, e.Y ) ) ;
Rectangle m_rect = new Rectangle( ) ;
if ( i != -1 )
{
m_rect = this.ClientRectangle
;
Point dpt = new
Point ( 0, 0 ) ;
if ( e.X + 150
< m_rect.Width )
dpt.X = e.X + 5
;
else
dpt.X = e.X
- 155 ;
if ( e.Y + 20 < m_rect.Height
)
dpt.Y = e.Y
+ 5 ;
else
dpt.Y = e.Y - 20 ;
string str = calculate
( i ) ;
l.Left = dpt.X ;
l.Top = dpt.Y ;
l.Size = new Size ( 150, 20
) ;
l.Text = str ;
l.Visible = true ;
}
else
l.Visible = false ;
}
We have called the search( ) method that returns the index of the array element
whose coordinates match with the coordinates of the current mouse position.
We have then calculated the suitable place where the label of size 150 x 20
can be displayed. The left-top position is stored in the dpt object. Next we
have called the calculate( ) method. This method customises the string containing
information of the city and returns the same.
Once we have the information, what remains is to display this information on
the label. We must first create the label. For this, add the following code
in the Load event handler.
l = new Label( ) ;
l.Visible = false ;
Controls.Add ( l ) ;
The following statements specify the position, size and text of the label. The
label is invisible initially. We have to make it visible using the Visible property.
l.Left = dpt.X ;
l.Top = dpt.Y ;
l.Size = new Size ( 150, 20 ) ;
l.Text = str ;
l.Visible = true ;
If the mouse is positioned at some obscure place in the world whose entry is
not present in the array the search( ) method returns a value -1. In such a
case the label is hidden by setting the Visible property to false.
Let’s now look at the two helper functions, search( ) and calculate( ).
They are given below.
int search ( Point p )
{
Rectangle r = new Rectangle ( p.X , p.Y , 5, 5 ) ;
Point pt ;
for ( int i = 0 ; i < loc.Length ; i++ )
{
pt = new Point ( loc [ i ].x, loc [ i ].y
) ;
if ( r.Contains ( pt ) )
return i ;
}
return -1 ;
}
To this method we have passed the current mouse position. We have run a for
loop to see whether coordinates of any city fall within the specified area.
If they do, we have returned the index of the element.
This index is then passed to the calculate( ) method.
string calculate ( int i )
{
int m_hour = loc [ i ].hours ;
int m_minute = loc [ i ].minutes ;
DateTime t = DateTime.Now ;
t = t.AddHours ( m_hour ) ;
t = t.AddMinutes ( m_minute ) ;
string week_day = t.DayOfWeek.ToString( ) ;
string str = week_day + “ “ + t.Hour + “:”
+
t.Minute + “ “ + loc [ i ].city ;
return str ;
}
In this method, we have read the hour and minutes of the specified array element.
Remember that these hour and minute values specify the time with respect to
the Indian Standard Time. We have added this time to the current time to get
the exact time at the city. Lastly, we have obtained the day of week and constructed
the string with all the information we have. This string is returned back to
the MouseMove event handler.
Run the program and move the mouse on the form. You will
get the pop-ups as shown in the following snap shot.
 |
Yashavant Kanetkar, one of the first
Express Computer columnists, is an established software expert, speaker
and author with several best-sellers to his credit, including titles like
“Let Us C” and the “Fundas” series. Contact him at kanetkar@dcubesoft.com |
|