Search code examples
vb.netbunifu

How to toggle or code the Placeholder / Watermark Property on a textbox in Bunifu UI


I can't somehow find the property for putting a watermark or a placeholder on a textbox using Bunifu UI. I need it for credential textboxes. I found a source code which only works on a normal textbox but not on Bunifu Textbox.

Here is my code :

Imports System.Runtime.InteropServices

Public Class Login

Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.CenterToParent()

    BunifuTextbox2._TextBox.PasswordChar = "*"
    SetCueText(BunifuTextbox1, "Username")
    SetCueText(BunifuTextbox2, "Password")
End Sub

Private Sub BunifuTextbox1_OnTextChange(sender As Object, e As EventArgs) Handles BunifuTextbox1.OnTextChange

End Sub
End Class

Public Module CueBannerText
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As Int32
End Function
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
Private Const EM_SETCUEBANNER As Integer = &H1501


Public Sub SetCueText(cntrl As Control, text As String)
    If TypeOf cntrl Is Bunifu.Framework.UI.BunifuDropdown Then
        Dim Edit_hWnd As IntPtr = FindWindowEx(cntrl.Handle, IntPtr.Zero, "Edit", Nothing)
        If Not Edit_hWnd = IntPtr.Zero Then
            SendMessage(Edit_hWnd, EM_SETCUEBANNER, 0, text)
        End If
    ElseIf TypeOf cntrl Is Bunifu.Framework.UI.BunifuTextbox Then
        SendMessage(cntrl.Handle, EM_SETCUEBANNER, 0, text)
    End If
End Sub
End Module

HERE IS THE ORIGINAL SOURCE CODE I FOUND :

Imports System.Runtime.InteropServices

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
   SetCueText(TextBox1, "Enter Name here")
End Sub
End Class

Public Module CueBannerText
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal lParam As String) As Int32
End Function
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
Private Const EM_SETCUEBANNER As Integer = &H1501


Public Sub SetCueText(cntrl As Control, text As String)
   If TypeOf cntrl Is ComboBox Then
        Dim Edit_hWnd As IntPtr = FindWindowEx(cntrl.Handle, IntPtr.Zero, "Edit", Nothing)
        If Not Edit_hWnd = IntPtr.Zero Then
            SendMessage(Edit_hWnd, EM_SETCUEBANNER, 0, text)
        End If
    ElseIf TypeOf cntrl Is TextBox Then
        SendMessage(cntrl.Handle, EM_SETCUEBANNER, 0, text)
    End If
End Sub
End Module

Solution

  • As noted the BunifuTextBox contains a regular WinForms text box. The code you found was designed for such a text box, thus all you need to do is set the cue banner for the underlying text box of the BunifuTextBox:

    SetCueText(BunifuTextbox1._TextBox, "Username")
    

    (this uses the original SetCueText() code that you found)