2016-03-30 16 views
1

私は月と年のみ表示する必要があるdatetimeピッカーdtOtMonthを持っています。私はフォームロード時に次のように書式を設定しました。C#実行時の日付書式エラーVS2005アプリケーション

dtOtMonth.Format = DateTimePickerFormat.Custom; 
dtOtMonth.CustomFormat = "MM/yyyy"; 

実行時に上下の矢印で日付を編集しようとすると、次のエラーが表示されます。

年、月、および日のパラメータは、表現できないDateTimeを表します。

System.ArgumentOutOfRangeExceptionが未処理の メッセージ=「年、月、日のパラメータは、非表現のDateTimeを記述する。」ましたSystem.Windows.Forms.DateTimePickerでSystem.Windows.Forms.DateTimePicker.SysTimeToDateTime(SYSTEMTIME S) でSystem.DateTime.DateToTicks(のInt32年、のInt32月、のInt32日)で := "mscorlib" のStackTrace ソースSystem.Windows.Forms.Control.ControlNativeWindowでSystem.Windows.Forms.DateTimePicker.WndProcでSystem.Windows.Forms.DateTimePicker.WmReflectCommand(メッセージ& M) (メッセージ& M) で.WmDateTimeChange(メッセージ& M) .OnMessage(メッセージ& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(メッセージ& m)System.Windows.Forms.UnsafeNativeMethods.SendMessageでSystem.Windows.Forms.NativeWindow.DebuggableCallback(のIntPtr hWndは、 のInt32 MSG、のIntPtr WPARAM、LPARAMのIntPtr) (HandleRefのhWnd、 のInt32 MSG、のIntPtr wParamに、のIntPtr lParamに)でSystem.Windows.Forms.ControlでSystem.Windows.Forms.Control.ReflectMessageInternal(のIntPtr hWndは、 メッセージ& M)でSystem.Windows.Forms.Control.SendMessageで (MSGのInt32、のIntPtr WPARAM、LPARAMのIntPtr) .WmNotify(メッセージ& m)at System.Windows.Forms.Control.WndProc(メッセージ& m) at System.Windows.Forms.GroupBox.WndProc(メッセージ&m)012 System.Windows.Forms.NativeWindow.DebuggableCallbackでSystem.Windows.Forms.Control.ControlNativeWindow.OnMessage(メッセージ& M)System.Windows.Forms.Control.ControlNativeWindow.WndProcで (メッセージ& M) (のIntPtrのhWndで、 のInt32 MSG、のIntPtr WPARAM、LPARAMのIntPtr)System.Windows.Forms.UnsafeNativeMethods.CallWindowProcで (のIntPtr WNDPROC、のIntPtr hWndは、MSGのInt32、のIntPtr wParamに、のIntPtr lParamに) System.Windows.Forms.NativeWindow.DefWndProcで(メッセージ& m) at System.Windows.Forms.Control.DefWndProc(メッセージ& m) at System.Windows.Forms.Control.WmKeyChar(メッセージ& M)System.Windows.Forms.Control.ControlNativeWindow.OnMessageでSystem.Windows.Forms.Control.WndProc(メッセージ& M)System.Windows.Forms.DateTimePicker.WndProcで (メッセージ& M) で(メッセージSystem.Windows.Forms.Control.ControlNativeWindow.WndProcで& M) (System.Windows.Forms.NativeWindow.DebuggableCallbackでメッセージ& M) (のIntPtr hWndは、 のInt32 MSG、のIntPtr WPARAM、LPARAMのIntPtr)システムで 。 Windows.Forms.UnsafeNativeMethods。DispatchMessageW(MSG & MSG)System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoopで (のInt32 dwComponentID、のInt32理由、のInt32 pvLoopData)System.Windows.Forms.Applicationで 。 Attendence_ModuleでSystem.Windows.Forms.Application.Run(フォームのMainForm) でSystem.Windows.Forms.Application.ThreadContext.RunMessageLoop(のInt32 理由、ApplicationContextのコンテキスト) でThreadContext.RunMessageLoopInner(のInt32 理由、ApplicationContextのコンテキスト) .Program.Main()in E:\ HR System \ HRProject \ Attendence_Module \ Attendence_Module \ Program.cs: at System.AppDomain._nExecu teAssembly System.AppDomain.ExecuteAssemblyに(アセンブリアセンブリ、文字列[]引数) Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyで(文字列assemblyFile、証拠assemblySecurity、文字列[]引数) ()System.Threading.ThreadHelperで 。 System.Threading.ThreadHelper.ThreadStartでSystem.Threading.ExecutionContext.Run(のExecutionContextのExecutionContext、ContextCallbackコールバック、オブジェクトの状態)でThreadStart_Context(オブジェクトの状態) ()

+0

私は、WinFormsのプロジェクトを開いた - 'datetimepicker'をドラッグ - ' Form_Load'イベント、およびすべての作品にあなたのコードをコピーしました。それはあなたのコードのいくつかの他のものになることができますか? –

+0

以下を参照してくださいhttp://stackoverflow.com/a/1608804/1071091 – Captain0

+2

WOWは何かを発見しました.. VS-2005 ???急いで2015年に切り替える! –

答えて

1

OPはときだけ月が変更エラーがoccurrs、発見されたとして、(例えば、「2月31日を」)が存在しない日付に変更することも可能です。

エラーを防ぐには、いくつかの方法があります。 1つの方法は、DateTimePickerが常にその月の最初の日を使用することです。ここDateTimePickerの値のDayプロパティは常にこの例では1に設定されていることを確認作る簡単な例ですが、私は出力を示すために、読み取り専用テキストボックスを使用します。このコードを使用して

public MyTestForm() 
{ 
    InitializeComponent(); 

    SetDateTimePickerFirstOfMonth(); 
    UpdateTextbox(); 
} 

private void SetDateTimePickerFirstOfMonth() 
{ 
    DateTime current = dtOtMonth.Value; 
    if (current.Day != 1) 
    { 
     DateTime newValue = new DateTime(current.Year, current.Month, 1); 
     dtOtMonth.Value = newValue; 
    } 
} 

private void UpdateTextbox() 
{ 
    int year = dtOtMonth.Value.Year; 
    int month = dtOtMonth.Value.Month; 

    txtResult.Text = string.Format("Year is {0} and month is {1}", year, month); 
} 

private void dtOtMonth_ValueChanged(object sender, EventArgs e) 
{ 
    SetDateTimePickerFirstOfMonth(); 
    UpdateTextbox(); 
} 

を、I矢印キーを使用してDateTimePickerの年または月を変更することができ、特定の日付が選択されていても、DateTimePicker値が常に選択した月の最初に設定されます。

デモアプリケーションのスクリーンショットです。テキストボックスは、DateTimePickerの値が変更されるとすぐに値を変更します。

Example Winform

+0

ここに私がしたことがあります。ありがとうございました –

+0

@code_Finderそれは素晴らしいです! :)あなたが問題を解決するのに役立ちましたら、受け入れられたとしてanwerを自由にマークしてください。 –

1

、値をインクリメントする日を必要とします。それは日付を解析しているようだし、日がないのでできない。

+0

正確には、私が見つけたものです –

1

最後にバグが見つかりました。

日時ピッカーでは、デフォルトの日付を初期設定します。月と年だけを使用しても、デフォルトの日付(ここでは現在の日付)が使用されます。

したがって、月を変更すると、選択した日付は毎月同じです。したがって、今日は2016年3月30日です。月を1つ減らすと2月、日付の時刻ピッカーは日付を30に初期化しようとします2月にエラーが発生します。

私の場合、月と年を使用する限り、私は日付を離れています。

は、私が31日29日、30日、好きではない、毎月毎月の1日目、ST(1)を持つフォームのLoadイベントに月の1回目の日付を初期化することによって、これを解決ここで

dtOtMonth.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); 
dtOtMonth.Format = DateTimePickerFormat.Custom; 
dtAdvanceStartDate.CustomFormat = "MM/yyyy"; 

いくつか私はこれが.NETのバグだとわかった非常に有用なリンクです。

https://connect.microsoft.com/VisualStudio/feedback/details/553375/system-argumentoutofrangeexception-year-month-and-day-parameters-describe-an-un

https://social.msdn.microsoft.com/Forums/windows/en-US/69b10b5b-a034-426e-a045-2bed8a1637a3/bug-in-datetimepicker-control?forum=winforms

+0

喜んで問題を解決しましたが、非常に頑強に聞こえません。年と月のみが必要な場合は、DateTimeを使用する代わりに、これらの値を2つの別々の整数値として格納することができます。ここでは、DateTimePickerからこれらの値を抽出する方法の例です:http://stackoverflow.com/a/29454651/717088 –

+1

素敵な&貴重なフィードバック.....私はVSアプリケーションを扱っています –

+0

私は答えを追加しましたWinformsプロジェクトでの実行方法の例を示します。 –