2016-12-05 3 views
-1

DatetimePicker.Visible = False로 설정했으며 TextBox를 클릭하면 텍스트 상자 안에 표시됩니다. 내 코드는 하나의 텍스트 상자에서 작동하지만 다른 텍스트 상자에서는 작동하지 않습니다.텍스트 상자에 DateTimePicker 표시

Private Sub Show_DTP(Ctl As Control) 

     Dim x As Integer = 0 
     Dim y As Integer = 0 
     Dim Width As Integer = 0 
     Dim height As Integer = 0 
     Dim rect As Rectangle 

     Select Case Ctl.Name 

      Case "TxtTest" 
       rect = TxtTest.DisplayRectangle() 
       x = rect.X + TxtTest.Left 
       y = rect.Y + TxtTest.Top 
       Width = rect.Width + 4 
       height = rect.Height 

       With My_DTP 
        .SetBounds(x, y, Width, height) 
        .Visible = True 
        .Focus() 
       End With 

      Case "Txt2" 
       rect = Txt2.DisplayRectangle() 
       x = rect.X + Txt2.Left 
       y = rect.Y + Txt2.Top 
       Width = rect.Width + 4 
       height = rect.Height 

       With My_DTP 
        .SetBounds(x, y, Width, height) 
        .Visible = True 
        .Focus() 
       End With 

     End Select 

End Sub 

Private Sub Text_Click(sender As Object, e As EventArgs) Handles TxtTest.Click, Txt2.Click 
     Dim Txt As System.Windows.Forms.TextBox = DirectCast(sender, TextBox) 
     My_DTP.Visible = False 
     Show_DTP(Txt) 
End Sub 

무엇이 잘못 되었나요?

+0

무엇을하려고합니까? – Plutonix

+0

사례 명세서 (C#의 경우 중단) 뒤에 "Exit Select"가 없습니다. –

+0

@Plutonix, 일부 텍스트 상자 내에 동일한 DTP를 표시하려고합니다. 그리고이 DTP를 사용하여 텍스트 상자에 값을 입력합니다. – LuckyLuke82

답변

1

case 문은 필요하지 않습니다. 실제 텍스트 상자와 상관없이 동일한 작업을 수행합니다.

"TxtTest"및 "Txt2"라는 두 개의 텍스트 상자를 양식에 넣고 "MyDTP"라는 DateTimePicker를 추가했습니다. 다음 코드를 사용하면 원하는대로 동작합니다.

Option Infer On 
Option Strict On 

Public Class Form1 

    Private Sub Show_DTP(target As TextBox) 

     Dim rect As Rectangle = target.DisplayRectangle() 
     Dim x As Integer = rect.X + target.Left 
     Dim y As Integer = rect.Y + target.Top 
     Dim width = rect.Width + 4 
     Dim height = rect.Height 

     With MyDTP 
      .SetBounds(x, y, Width, height) 
      .Visible = True 
      .Focus() 
     End With 

    End Sub 

    Private Sub Text_Click(sender As Object, e As EventArgs) Handles TxtTest.Click, Txt2.Click 
     Dim Txt As System.Windows.Forms.TextBox = DirectCast(sender, TextBox) 
     MyDTP.Visible = False 
     Show_DTP(Txt) 

    End Sub 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     MyDTP.Visible = False 

    End Sub 

End Class 
+0

감사합니다. 귀하의 제안도 효과가 있습니다! – LuckyLuke82