How would I go about creating a textbox help label (cuebanner/cuetext)? - vb.net

I wanted to create a textbox which has a help label inside the box which then disappears when the box has characters entered into it. I found one way of doing it which involves loading the form with text inside the textbox in the colour grey and then removing it when the user clicks on the box... The problem with this is i wanted to use a string.IsNullOrEmpty(textboxIP) but when the user hasn't typed anything into the box, the program sees the box as not empty as it has the pre-loaded writing in it. This is the code I used to remove the text on user click...
Dim WatermarkIP As String = "Yes"
Dim WatermarkPing As String = "Yes"
Private Sub textboxIP_Enter(sender As Object, e As EventArgs) Handles textboxIP.Enter
If WatermarkIP = "Yes" Then
textboxIP.Clear()
textboxIP.ForeColor = Color.Black
WatermarkIP = "No"
End If
End Sub
Private Sub textboxPing_Enter(sender As Object, e As EventArgs) Handles textboxPing.Enter
If WatermarkPing = "Yes" Then
textboxPing.Clear()
textboxPing.ForeColor = Color.Black
WatermarkPing = "No"
End If
End Sub
Does anyone know of a better way of creating a greyed out help/hint label inside the textbox which IS NOT counted as text inside the box, does not have to be deleted by the user before they can type in the box and is maybe a bit simpler?

I've found it finally!
This will give you that watermark text on your textbox controls:
Imports:
Imports System.Runtime.InteropServices
Global Declarations in your main class:
Private Const EM_SETCUEBANNER As Integer = &H1501
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared 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
Functions in your main class:
Private Sub SetCueText(ByVal control As Control, ByVal text As String)
SendMessage(control.Handle, EM_SETCUEBANNER, 0, text)
End Sub
Usage (usually in form_load event):
SetCueText(TextBox1, "blah")
SetCueText(TextBox2, "blahblah")
Hope this helps :)
Credit: http://www.vbforums.com/showthread.php?638105-CueBanner-Watermark-text-for-Textboxes

Related

Set the exact position of a Textbox's Scrollbar

I have a multi-line textbox with both its horizontal and vertical scrollbars visible, and WordWrap set to False. I load a large textfile in the textbox to allow the user to edit it.
The editing features automation which requires me to store the value of the entire textbox into a value, process it and then store it back into the textbox. This all is working great, but by setting a string into the textbox, it first clears it and then fills it again. This action sets the scrollbar position to the top-left corner.
I have tried to use Textbox1.scrolltocaret in my update routine which makes the cursor visible again, but it doesn't scroll to the exact position the control was set to before.
Also, I can't find out how to call textbox1.scrolltocaret every time the user moves using the arrowkey.
How can I store and restore the scrollbar locations?
Here's my code:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If Me.bPauseUpdate = True Then Exit Sub
Me.bPauseUpdate = True
Me.CursorLocation = TextBox1.SelectionStart
Dim aWorkingText() As String = Me.TextBox1.Lines
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Code that changes aWorkingText lives here, but is not relevant for this question
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
TextBox1.Lines = aWorkingText
Me.bPauseUpdate = False
TextBox1.SelectionStart = Me.CursorLocation
Textbox1.scrolltocaret
End Sub
I looked into using Textbox1.Scrollbars but they are only for controlling whether the scrollbars are visible.
I tried to make it work with the following code, but that does not allow me to actually set the value even though the IDE says I should be able to do so:
Me.ScrollbarX = TextBox1.AutoScrollOffset.X
Me.ScrollbarY = TextBox1.AutoScrollOffset.Y
.
.
.
TextBox1.AutoScrollOffset.X = Me.ScrollbarX
TextBox1.AutoScrollOffset.Y = Me.ScrollbarY
I even tried this, but it yields the same result.
TextBox1.AutoScrollOffset.X = New Point(Me.ScrollBarX)
TextBox1.AutoScrollOffset.Y = New Point(Me.ScrollBarY)
You can use the GetScrollPos Win32 API to get the current vertical and horizontal scrollbar position of the TextBox control.
Imports System.Runtime.InteropServices
<DllImport("user32.dll", CharSet:=CharSet.Auto)> Friend Shared Function GetScrollPos(hWnd As IntPtr, nBar As Integer) As Integer
End Function
Friend Enum SBOrientation As Integer
SB_HORZ = &H0
SB_VERT = &H1
End Enum
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Horizontal scroll position
GetScrollPos(TextBox1.Handle, SBOrientation.SB_HORZ)
'Vertical scroll position
GetScrollPos(TextBox1.Handle, SBOrientation.SB_VERT)
End Sub
Use the SendMessage Win32 API to set the TextBox scroll position
<DllImport("user32.dll")> Friend Shared Function SendMessage(hWnd As IntPtr, msg As UInteger, wParam As UInteger, lParam As UInteger) As IntPtr
End Function
Friend Const SB_THUMBPOSITION As UInteger = 4
Friend Enum WindowMessage As Integer
WM_HSCROLL = &H114
WM_VSCROLL = &H115
End Enum
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Set Vertical scroll position
Dim vPos = 16
Dim wparam1 = CUInt(vPos) << 16 Or (SB_THUMBPOSITION And &HFFFF)
SendMessage(TextBox1.Handle, CUInt(WindowMessage.WM_VSCROLL), CUInt(wparam1), CUInt(0))
'Set Horizontal scroll position
Dim hPos = 100
Dim wparam2 = CUInt(hPos) << 16 Or (SB_THUMBPOSITION And &HFFFF)
SendMessage(TextBox1.Handle, CUInt(WindowMessage.WM_HSCROLL), CUInt(wparam2), CUInt(0))
End Sub

How to load a specific text in textbox with greyish colour and uneditable [duplicate]

This question already has answers here:
Watermark TextBox in WinForms
(11 answers)
Creating a TextBox with watermark using ControlStyles.UserPaint shows the watermark just once at component creation
(2 answers)
Closed 2 years ago.
I am creating a login form by using Visual Basic. Is it possible for me to load a specific text in a textbox with greyish color and uneditable? (Just like the effect in Youtube search bar) What I manage to found on the internet is just hide function and change the color of the text.
Sorry for my confusing title.
Add this Module (ModExtentions) to the Project: it adds an extension method, SetCueBanner(), to TextBox Controls.
Specifying True or False, changes the cue banner behavior:
False: the cue banner is visible until the control gets focus,
True, the cue banner is visible until the first char is entered.
This internal functionality is activated sending an EM_SETCUEBANNER message to the Control.
Use it like this:
' The Cue Banner is visible until the control gets focus
TextBox1.SetCueBanner("Some text...", False)
' The Cue Banner is visible until a character is entered
TextBox1.SetCueBanner("Some text...", True)
The Module where the extension method is defined:
Imports System.Runtime.InteropServices
Public Module ModExtentions
Private Const EM_SETCUEBANNER As Integer = &H1501
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
End Function
<Extension()>
Public Sub SetCueBanner(tbox As TextBox, ByVal text As String, ByVal showOnFocus As Boolean)
SendMessage(tbox.Handle, EM_SETCUEBANNER, If(showOnFocus, 1, 0), text)
End Sub
End Module
Set the TextBox Enabled property to false. You can put in code from code but the user can't type in the box.
Set up your gray text in the Form.Load. The use the Enter and Leave events to change it to normal and back again.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.ForeColor = Color.Gray
TextBox1.Text = "Search term here..."
End Sub
Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
TextBox1.ForeColor = Color.Black
TextBox1.Text = ""
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
If String.IsNullOrWhiteSpace(TextBox1.Text) Then
TextBox1.ForeColor = Color.Gray
TextBox1.Text = "Search term here..."
End If
End Sub

Button in TextBox disappears when selecting text while Windows media player plays music

This is my first question, please be nice. Oh, and I'm not a native english speaker. :)
I've discovered some weird bug in my application. I've created a TextBox control with a button in it (code below). Following these steps will make the button disappear.
Start windows media player with some music (or video)
Start the application
Click in TextBox control and hold down left mouse button
Move your cursor around like crazy
Woosh... Button disappears.
This will not happen when you closed or paused your windows media player. I was able to reproduce this bug on a different system (Windows 7 and Windows 10). This is totally weird, because it doesn't seem logical. Windows is doing crazy stuff with the windows media player...
I'm not sure if there's a workaround. Can anybody help me with this or should I ask on Microsoft forums? I've tried to call "UpdateButton" while selection changed, but I wasn't successful.
Public Class TextBoxEx
Inherits TextBox
Const BUTTON_WIDTH As Integer = 18
Const EM_SETMARGINS As Integer = &HD3
Const EC_RIGHTMARGIN As Integer = &H2
<Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
End Function
Private btnCommand As Button
Public Sub New()
btnCommand = New Button
btnCommand.Cursor = Cursors.Default
btnCommand.Image = My.Resources.iconCancel
Me.Controls.Add(btnCommand)
Call UpdateButton()
End Sub
Private Sub UpdateButton()
Dim rightMargin As Integer = (BUTTON_WIDTH + 1) << 16
btnCommand.Size = New Size(BUTTON_WIDTH, Me.ClientSize.Height)
btnCommand.Location = New Point(Me.ClientSize.Width - BUTTON_WIDTH, 0)
Call SendMessage(Me.Handle, EM_SETMARGINS, EC_RIGHTMARGIN, rightMargin)
End Sub
Protected Overrides Sub OnResize(e As System.EventArgs)
MyBase.OnResize(e)
Call UpdateButton()
End Sub
End Class

Vertical ScrollBar does not repaint using EnableScrollBar api

I am trying to disable a VScrollBar control using the EnableScrollBar api. When I call the api it returns as if no problems ocurred but the VScrollBar is not repainted.
To reproduce the problem create a Vb.Net windows forms project, drop a VScrollBar control and a button to the form and paste the following code:
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function EnableScrollBar(ByVal hWnd As IntPtr, ByVal nBar As Integer, ByVal value As Integer) As Boolean
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim wSBflags As UInteger = 3UI 'SB_VERT
Dim wArrows As UInteger = 3UI 'ESB_DISABLE_BOTH
Dim result As Boolean = EnableScrollBar(Me.VScrollBar1.Handle, wSBflags, wArrows)
End Sub
I tried using SendMessage to send a redraw (WM_REDRAW) and a paint (WM_PAINT) but cant get it to work. Any ideias?
Ps: If you drop a multiline textbox and use the same code it works....
SB_VERT is for the vertical scrollbar as part of the non-client area of a window. For a scroll bar control, use the SB_CTL constant.

Saving textbox name when clicked in order to input text

Hey all i am in need of some help getting my code working correctly like i am needing it to. Below is my code that when the user click on the textbox, it pops up a keyboard where they can click on any letter and it will type that letter into the textbox. Problem being is i can not seem to get the name of the text box to return so that it knows where to send the letters to.
Order in firing is:
TextBox1_MouseDown
keyboardOrPad.runKeyboardOrPad
kbOrPad.keyboardPadType
ClickLetters
Form1.putIntoTextBox
Form1
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
Call keyboardOrPad.runKeyboardOrPad("SHOW") 'Just shows the keyboard
Call kbOrPad.keyboardPadType("PAD", TextBox1)
End Sub
Public Sub putIntoTextBox(ByRef what2Put As String, ByRef whatBox As TextBox)
whatBox.Text = what2Put '<-- has error Object reference not set to an instance of an object. for the whatBox.text
End Sub
kbOrPad class
Dim theBoxName As TextBox = Nothing
Public Sub keyboardPadType(ByRef whatType As String, ByRef boxName As TextBox)
theBoxName = boxName '<-- shows nothing here
Dim intX As Short = 1
If whatType = "PAD" Then
Do Until intX = 30
Dim theButton() As Control = Controls.Find("Button" & intX, True)
theButton(0).Enabled = False
intX += 1
Loop
ElseIf whatType = "KEYB" Then
End If
End Sub
Private Sub ClickLetters(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim btn As Button = CType(sender, Button)
If btn.Text = "Backspace" Then
Else
Call Form1.putIntoTextBox(btn.Text, theBoxName) 'theBoxName taken from keyboardPadType
End If
End Sub
Some visuals for you:
Pastebin code: http://pastebin.com/4ReEnJB0
make sure that theBoxName is a Module scoped variable, then I would populate it like this giving you the flexibility of implementing a shared TextBox MouseDown Handler:
Private Sub TextBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
Dim tb As TextBox = CType(sender, TextBox)
Call keyboardPadType("PAD", tb)
End Sub
Try something like this
Public Class Form1
Dim myKborPad As New kbOrPad
Private Sub TextBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
Dim tb As TextBox = CType(sender, TextBox)
Call myKborPad.keyboardPadType("PAD", tb)
End Sub
Edit Based on your PasteBin code.
I noticed you already have an instance of your keyboardPadType declared in your Module, use that instead of what I said earlier. That code should look like:
remove:
Dim myKborPad As New kbOrPad
and use the theKbOrPad that you created in your module like this:
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
Dim tb As TextBox = CType(sender, TextBox)
Call keyboardOrPad.runKeyboardOrPad("SHOW")
Call theKbOrPad.keyboardPadType("PAD", tb)
'Call kbOrPad.keyboardPadType("PAD", tb)
End Sub
Also about the current error your are getting, you are trying to use the default instance of your Form1 , it isn't the actual Form that you are running, you can code around this by making the method you are trying to use as shared. Like this:
Public Shared Sub putIntoTextBox(ByRef what2Put As String, ByRef whatBox As TextBox)
whatBox.Text = what2Put
End Sub
But however I would actually prefer to put it into your Module like this
Public Sub putIntoTextBox(ByRef what2Put As String, ByRef whatBox As TextBox)
whatBox.Text = what2Put
End Sub
and call it like this
Call putIntoTextBox(btn.Text, theBoxName)
after making above changes your code worked.
First, you should replace the ByRef with ByVal (anytime you don't know whether you should use one or the other, use ByVal).
Secondly, I believe you don't need the method, putIntoTextBox, I think you should be able to do that directly (might be threading problems that prevent it, but I don't think that's likely based on your description). You don't show where Form1 is set (or even if it is), and that's another potential problem.
Finally, the better way to call back into the other class is to use a delegate/lambada.
(I know, no code, but you don't provide enough context for a working response, so I'm just giving text).