gpt4 book ai didi

vb.net - GhostText 使用 VB.Net

转载 作者:行者123 更新时间:2023-12-04 18:13:52 27 4
gpt4 key购买 nike

Ghost Text

大家好,我尝试创建一个 鬼文字在文本框上使用标签。我正在使用 VB.Net2005。我用这段代码完成了这个:

Public Class frmDataEntry

Private Sub PhantomTextLastName()
If txtLastName.Text = "" Then
lblLastName.Visible = True
Else
lblLastName.Visible = False
End If
End Sub

Private Sub PhantomTextFirstName()
If txtFirstName.Text = "" Then
lblFirstName.Visible = True
Else
lblFirstName.Visible = False
End If
End Sub

Private Sub PhantomTextMiddleInitial()
If txtMiddleInitial.Text = "" Then
lblMiddleInitial.Visible = True
Else
lblMiddleInitial.Visible = False
End If
End Sub

Private Sub txtLastName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLastName.Click
lblLastName.Text = "Last Name"
End Sub

Private Sub txtLastName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtLastName.KeyDown
PhantomTextLastName()
End Sub

Private Sub txtLastName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLastName.TextChanged
PhantomTextLastName()
End Sub

Private Sub lblLastName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblLastName.Click
txtLastName.Focus()
End Sub

Private Sub txtFirstName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFirstName.Click
lblFirstName.Text = "First Name"
End Sub

Private Sub txtFirstName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFirstName.KeyDown
PhantomTextFirstName()
End Sub

Private Sub txtFirstName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFirstName.TextChanged
PhantomTextFirstName()
End Sub

Private Sub lblFirstName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblFirstName.Click
txtFirstName.Focus()
End Sub

Private Sub lblMiddleInitial_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblMiddleInitial.Click
txtMiddleInitial.Focus()
End Sub

Private Sub txtMiddleInitial_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMiddleInitial.Click
lblMiddleInitial.Text = "Middle I."
End Sub

Private Sub txtMiddleInitial_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMiddleInitial.KeyDown
PhantomTextMiddleInitial()
End Sub

Private Sub txtMiddleInitial_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMiddleInitial.TextChanged
PhantomTextMiddleInitial()
End Sub
End Class

有什么办法可以减少这段代码,这样当我尝试添加另一个文本框时,我就不必重新输入一堆代码了。我有使用模块和类的基本知识,但我真的不知道如何在这个项目中应用它。我是新手,如果你有任何教程可以帮助我解决这个问题,请给我链接。在此先感谢上帝保佑。

最佳答案

创建 usercontrol .您的用户控件背后的代码可能类似于:

 Public Class GhostTextbox

Private _ghostText As String
Public Property GhostText As String
Get
Return _ghostText
End Get
Set(ByVal Value As String)
_ghostText = Value
End Set
End Property

Public Property ActualText As String
Get
Return Me.TextBox1.Text
End Get
Set(ByVal Value As String)
Me.TextBox1.Text = Value
End Set
End Property

Private Sub PhantomText()
If TextBox1.Text = "" Then
Label1.Visible = True
Else
Label1.Visible = False
End If
End Sub

Private Sub TextBox1_Click(sender As Object, e As System.EventArgs) Handles TextBox1.Click
Label1.Text = GhostText
End Sub

Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
PhantomText()
End Sub

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
PhantomText()
End Sub

Private Sub GhostTextbox_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Label1.Text = GhostText
End Sub
End Class

然后,使用这个自定义控件而不是仅仅使用 TextBox .您需要做的就是设置 GhostText您添加的每个新控件的属性,而不是再次重做相同的逻辑。

关于vb.net - GhostText 使用 VB.Net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7498708/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com