I would like to add placeholder text similar to what you see on HTML Inputs. I am using Net Advantage 2008 v1 in Visual Studio 2010, targeting 3.5. I can do this with standard Windows Controls with the following:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load CueProvider.SetCueText(TextBox1, "blah") CueProvider.SetCueText(TextBox2, "blahblah") CueProvider.SetCueText(UltraTextEditor1, "InfraCool") 'Does not work CueProvider.SetCueText(UltraTextEditor2, "UltraCool") 'Does not work End Sub
This is in a separate class:
Imports System.Runtime.InteropServicesPublic Class CueProvider 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 Public Shared Sub SetCueText(ByVal control As Control, ByVal text As String) SendMessage(control.Handle, EM_SETCUEBANNER, 0, text) End SubEnd Class
Textbox1 and TextBox2 are just standard Winforms textbox controls. UltraTexteditor1 and UltraTextEditor2 are Infragistics UltraTextEditors. The first two controls behave as expected, the Infragistics editors show no cue banner. I know there are other ways to override the class and set and remove the "placeholder" text, but I've already got a thousand of these in my app. Is there another property to use, or does this function appear in later releases?
Thank you for your time and attention to this.
Hi,
I'm not familiar with the EM_SETCUEBANNER message. I've never used it. I looked it up on MSDN and it says that this is supposed to work on an "edit control". I'm not entirely sure what that means or it has any significance. Does Windows look for certain types of controls, maybe? If so, then this obviously won't work on anything except the standard DotNet controls that are wrappers for window classes like TextBox.
Have you tried this on any other controls? Does it work on a Label control, for example?
According to MSDN, the SendMessage call will return true or false if the message succeeds or fails, respectively. Have you checked the return value to see if it thinks it was successful?
Hi Mike, Thanks for the reply. It comes back as false (0) when called on Infragistics UltraTextEditor, but (1) when called on the standard Textboxes. I've seen articles describing it on ComboBoxes, too. I was wondering if the underlying Control is not the same as the handle I am passing in.
It returns 0 when called on a label, too.
If it doesn't work on the Inbox Label control, then my guess is that Windows checks for certain window classes explicitly.
If that's the case, you might be able to get it to work. When the UltraTextEditor goes into edit mode, it displays a derived TextBox control over itself. So you could set AlwaysInEditMode to true so that the TextBox is always there and the pass in the window handle the TextBox to SendMessage. To get the TextBox, you can use ultraTextEditor1.Controls[0].
The down side is that, with the TextBox always displayed, you will lose a little bit of functionality, since the TextBox cannot show gradients and such. But that's probably not a big deal.
I would like to use the Watermark feature in WinForm UltraTextEditor control.
With this method you described here, I managed to emulate it.
My question: Is there any other method to show a background text ?
How can I send the message when the control is ReadOnly? ( we have a case when we want to show the background text in ReadOnly mode too)
Is there any method to access the TextBox control itself in ReadOnly mode?
I'm not sure what version of infragistics you are using, but the text editor has a property called NullText. You can set it to whatever text you want displayed and then simply change the NullTextAppearance.ForeColor to Gray so that it looks like a water mark.
Here is a sample code -
Me.utextbox.NullText = "###" Me.utextbox.NullTextAppearance.ForeColor = Color.Gray
As long as there is no text in the control, the text you set will be displayed. As soon as the user clicks on the text box, this text will disappear.
Thanks
Karthik Subramanian