在文本框中显示 DateTimePicker [英] Show DateTimePicker in Textbox

查看:27
本文介绍了在文本框中显示 DateTimePicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了 DatetimePicker.Visible=False,当我点击文本框时,我希望它显示在文本框内.我的代码适用于 1 个文本框,但不适用于另一个:

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

这里有什么问题?

推荐答案

不需要 case 语句:不管实际的文本框如何,你都做同样的事情.

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

我在表单上放置了两个名为TxtTest"和Txt2"的文本框,并添加了一个名为MyDTP"的 DateTimePicker.使用以下代码,它表现得如您所愿:

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

这篇关于在文本框中显示 DateTimePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆