using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Inet.Viewer.Data;
using Inet.Viewer.Resources;
namespace Inet.Viewer.WinForms.Prompt
{
///
/// Panel for a time prompt field.
///
public partial class TimePromptField : PromptControl
{
private static readonly CustomPromptValue customPromptValue = new CustomPromptValue();
private DateTime oldValueIfNoValueNotChecked;
///
/// Creates the panel.
///
/// prompt field this panel is based one
/// validation delegate for checking prompt values
/// flag indicating the no value checkbox should be hidden
public TimePromptField(PromptData promptData, ValidationDelegate validationDelegate, bool hideNoValue)
{
this.PromptData = promptData;
InitializeComponent();
timePicker.ValueChanged += HandleChanged;
PromptValue alreadySelectedValue = promptData.Values;
if (alreadySelectedValue != null && alreadySelectedValue.Value is TimeSpan)
{
timePicker.Value = ToDateTime((TimeSpan)alreadySelectedValue.Value);
}
else if (!hideNoValue)
{
chkNoValue.Checked = true;
}
if (hideNoValue)
{
chkNoValue.Visible = false;
}
chkNoValue.Text = strings.Prompt_None_Long;
ErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
ControlForErrors = chkNoValue.Visible ? (Control)this.chkNoValue : (Control)this.timePicker;
}
///
/// Converts a TimeSpan to a DateTime.
///
/// the time span
/// the date time
public static DateTime ToDateTime(TimeSpan timeSpan)
{
return new DateTime(1900, 1, 1, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
}
///
/// Converts the time of a DateTime to a TimeSpan.
///
/// the date time
/// the time span
public static TimeSpan ToTimeSpan(DateTime dt)
{
return new TimeSpan(dt.Hour, dt.Minute, dt.Second);
}
///
/// Returns the chosen value for this panel. Never null: if null value, will return null wrapped in a SinglePromptValue.
///
internal override PromptValue Value
{
get
{
return new SinglePromptValue(ToTimeSpan(this.timePicker.Value), null, PromptData.Type);
}
set
{
this.timePicker.Value = ToDateTime((TimeSpan)value.Value);
}
}
///
/// Called when the "no value" checkbox is checked: sets the value to null
///
/// button being checked
/// args of the event
private void chkNoValue_CheckedChanged(object sender, EventArgs e)
{
if (this.chkNoValue.Checked)
{
oldValueIfNoValueNotChecked = this.timePicker.Value;
this.timePicker.Enabled = false;
}
else
{
if (oldValueIfNoValueNotChecked == null)
{
oldValueIfNoValueNotChecked = new DateTime();
}
this.timePicker.Value = oldValueIfNoValueNotChecked;
this.timePicker.Enabled = true;
}
}
}
}