-2

I've set DatetimePicker.Visible=False, and when I click on Textbox I want It to be displayed inside of Textbox. My code works for 1 Textbox, but not the other one:

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

What is wrong here ?

LuckyLuke82
  • 586
  • 1
  • 18
  • 58
  • What are you trying to accomplish? – Ňɏssa Pøngjǣrdenlarp Dec 05 '16 at 14:50
  • there is no "Exit Select" after your case statement (break in case of c#) – Karthik Ganesan Dec 05 '16 at 14:51
  • @Plutonix, I want to display same DTP inside some Textboxes. And I will use this DTP to enter It's value into textbox. – LuckyLuke82 Dec 05 '16 at 14:56
  • @KarthikGanesan, Exit select doesn't do anything. It's strange but my code actually works If I delete 1 Textbox and add new one. It's like a bug. – LuckyLuke82 Dec 05 '16 at 14:57
  • Trying to stuff one Control inside another doesnt make them act like one control. You can easily make the DTP look/act almost like a text control using the `ShowUpDown` property. Its not clear what role the TextControl plays if you dont want it to actually get the input – Ňɏssa Pøngjǣrdenlarp Dec 05 '16 at 15:00
  • @HansPassant, this worked ! But why, I'm hiding DTP every time I click on Textbox. And why did It work only on 1 specific Textbox and not the other ? – LuckyLuke82 Dec 05 '16 at 15:05
  • @Plutonix, ShowUpDown is not an option for me. Everything is basically because of design issues. I have some DTP's on my form for edit and I change background colour of all controls if they are in "non-editable" state....And you cannot change DTP backcolour easily. – LuckyLuke82 Dec 05 '16 at 15:07
  • 1
    @KarthikGanesan : `Exit Select` isn't needed there. VB.NET's `Case` statements doesn't fall-through. See [**Why should I use exit select?**](http://stackoverflow.com/questions/1924128/why-should-i-use-exit-select) – Visual Vincent Dec 05 '16 at 15:10

1 Answers1

1

There is no need for the case statement: you do the same things regardless of the actual textbox.

I put two textboxes named "TxtTest" and "Txt2" on a form and added a DateTimePicker named "MyDTP". With the following code it behaved as you appear to want:

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
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84