2016-12-05 18 views
-1

私はDatetimePicker.Visible = Falseを設定しました.Textboxをクリックすると、Textboxの中に表示されます。私のコードは、1つのテキストボックスでは動作しますが、もう1つではありません。テキストボックスに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"の2つのテキストボックスをフォームに配置し、 "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

関連する問題