2017-10-10 32 views
0

C#으로 작성된 내 Windows Form 응용 프로그램에는 DateTimePicker 컨트롤이 있습니다. 컨트롤의 텍스트 부분을 중앙에 배치하고 싶지만 가능합니까? 현재 텍스트의 왼쪽에 텍스트가 있고, 오른쪽에 많은 공간이 있습니다.DateTimePicker의 텍스트를 가운데에 맞출 수 있습니까?

enter image description here

나는

myDateTimePicker.TextAlign = ContentAlignment.MiddleCenter; 

을 시도했지만 오류 준 :

'System.Windows.Forms.DateTimePicker' does not contain a definition for 'TextAlign' and no extension method 'TextAlign' accepting a first argument of type 'System.Windows.Forms.DateTimePicker' could be found (are you missing a using directive or an assembly reference?)

내가 일 듯 모든 속성을 찾을 수 없습니다, 나는 해결책을 어디서든 찾을 수 없습니다를 인터넷에서도. 이것은 전혀 가능하지 않습니까?

+0

이전 WinForms에서는 불가능할 것 같습니다. 컨트롤을 스타일/사용자 정의 할 수있는 WPF로 전환하는 것이 좋습니다. – Xiaoy312

+2

언제나 가능하지만 항상 간단하지는 않습니다. 예를 들어 OnPaint 핸들러를 재정의 할 수는 있지만 기본 페인트에 의존 할 수 없으므로 모든 상황을 그릴 수 있습니다. –

답변

2

당신은 항상 DateTimePicker를 서브 클래 싱하여 OnPaint 핸들러를 재정의 할 수 있습니다 : 당신은 어떻게 든 즉석에서해야 할 수도 있으므로

public class CenteredDateTimePicker : DateTimePicker 
{ 
    public CenteredDateTimePicker() 
    { 
     SetStyle(ControlStyles.UserPaint, true); 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), ClientRectangle, new StringFormat 
     { 
      Alignment = StringAlignment.Center, 
      LineAlignment = StringAlignment.Center 
     }); 
    } 
} 

그러나, 스크롤바가, 여기에 그려지지 않습니다 ...

를 불행하게도, 당신은 '수 Reference SourceDateTimePicker.OnPaint의 정의를 찾으십시오.