|
Glad you found the article useful!
/ravi
|
|
|
|
|
|
Thanks, Mohit! Will update the article shortly.
/ravi
|
|
|
|
|
I changed the Set & Clear methods to accept the type DevExpress.XtraEditors.TextEdit, but it is not working. No errors, just no watermark. Do you know what else I might need?
|
|
|
|
|
CueProvider works by sending a Windows message to the edit control. It's likely that the DevExpress TextEdit control eats this message. I recommend contacting DevExpress support. They may have a workaround or (better yet) offer their own means of setting the cue text.
/ravi
|
|
|
|
|
You have to access the MaskBox [^] property to send the message to.
Wollen Sie ganz einfach Ihre eigene Homepage erstellen, ohne HTML-Kenntnisse, einfach, professionell und mit viel Freude? Probieren Sie unser Desktop Content Management System (CMS) Zeta Producer für Windows aus. Komplett mit eigenem Shop, Gästebuch, Weblog, Bildergalerien, Integration von YouTube-Videos. Wir haben eine aktive Anwender-Community, schnellen Support, sympathische Support-Mitarbeiter.
|
|
|
|
|
Elegant solution. However, I also need cue banners to show in TextBoxes set as ReadOnly. Currently the banner does not show in this circumstance, presumably because if you can't enter something, it's considered useless to give a hint as to what to enter. In my case, though you cannot type in the textbox, it is still interactive through double-clicking, and I would like to advertise that. Is there some elegant way to have the window show the cue for any blank textbox, editable or not?
|
|
|
|
|
Liko721 wrote: Is there some elegant way to have the window show the cue for any blank textbox, editable or not? Dunno 'bout elegant but perhaps you could fake read-onlyness by refusing to accept typed input in a textbox that's actually not read-only?
/ravi
|
|
|
|
|
Thank for this article !
I want to know if it's possible to write a text into a MaskedTextBox that has a mask only for numeric key? I want to add "Ext." for extension into my maskedTextBox of a phone number.
Thank in advance
Martin
|
|
|
|
|
Hi Martin,
I'm not sure. What happens when you set the MaskedTextBox 's Text property at run time?
/ravi
|
|
|
|
|
The cue is setted only if the maskedtextbox has not a mask.
Martin
|
|
|
|
|
Ah - looks like MaskedTextBox handles EM_SETCUEBANNER . Which makes sense.
/ravi
|
|
|
|
|
Sorry mate,
demo not working. I have WinXP, .NET 2.0, .NET 3.5 installed...
Busi
|
|
|
|
|
Do you have the calls to set/clear the cue text in your form's OnLoad handler (or an equivalent safe location)? If you like, email me your project and I'll take a look at it.
Thanks,
/ravi
|
|
|
|
|
I might have found the solution as to why the cue is not displayed. I haven't looked at the original code supplied by Ravi, I used a slightly modified version of the CueProvider extender provider code supplied by Fabrice instead (modified to use a generic Dictionary instead of a Hashtable ), but on my computer the cue text could be set and displayed immediately in the Visual Studio designer while it did not display when the program was running. The reason was the test in SetCueInternal requiring that the handle for the text box is created before the cue can be set. Changing the method slightly to call the text box objects CreateControl method if the handle had not been created already solved it and the cue displays correctly both in the designer and the executable.
|
|
|
|
|
I don't think this is the problem the other folks have encountered. One poster was initially setting the text in the text box's OnEnter handler.
/ravi
|
|
|
|
|
Windows XP, VS2008, Frame work 2.0 on my PC, just nothing happen?
::Qing
|
|
|
|
|
Where are you calling the method?
/ravi
|
|
|
|
|
There's a funny with comctl32.dll (IIRC) and how/if it's declared in the manifest.
Try calling Application.EnableVisualStyles immediately before Application.Run, it sorted out an uncooperative Framework 2 app for me.
Application.EnableVisualStyles
Application.Run(new Form1)
Add the brackets and semi-colons for C#.
|
|
|
|
|
|
Can you be more explicit? Where are you calling the method(s)? They should typically be called in your Form 's OnLoad handler.
Thanks,
/ravi
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections;
namespace Controls
{
[ProvideProperty("CueText", typeof(TextBoxBase))]
public class CueProvider : Component, IExtenderProvider, ISupportInitialize
{
public bool CanExtend(object extendee)
{
return (extendee is TextBox || extendee is TextBoxBase);
}
private Hashtable _cueTexts = new Hashtable();
[DefaultValue(""), Localizable(true)]
public string GetCueText(TextBoxBase ctl)
{
if (this._cueTexts[ctl] == null)
return "";
return (string) this._cueTexts[ctl];
}
public void SetCueText(TextBoxBase ctl, string value)
{
if (ctl != null)
{
_cueTexts[ctl] = value;
SetCueInternal(ctl, value);
}
}
private const int EM_SETCUEBANNER = 0x1501;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
///
/// Sets a text box's cue text.
///
/// <param name="textBox" />The text box.
/// <param name="cue" />The cue text.
private static void SetCueInternal(TextBoxBase textBox,string cue)
{
// Test XP
if (Environment.OSVersion.Version.Major >= 5 && textBox.IsHandleCreated)
SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, cue);
}
///
/// Clears a text box's cue text.
///
/// <param name="textBox" />The text box
private static void ClearCue(TextBoxBase textBox)
{
// Test XP
if (Environment.OSVersion.Version.Major >= 5 && textBox.IsHandleCreated)
SendMessage(textBox.Handle, EM_SETCUEBANNER, 0, string.Empty);
}
#region ISupportInitialize Members
public void BeginInit()
{
}
public void EndInit()
{
foreach (DictionaryEntry item in _cueTexts)
{
if (item.Key is TextBoxBase)
SetCueInternal((TextBoxBase)item.Key, (string)item.Value);
}
}
#endregion
}
}
modified on Wednesday, November 5, 2008 7:24 AM
|
|
|
|
|
|
Nice article. For the .NET 3.5 version, you can make use of extension methods to make this feel more natural.
Also, look at using TextBoxBase instead of TextBox and also at UpDownBase (you will need to loop through the child controls looking for the textbox) and GetComboBoxInfo to set a cue on a ComboBox.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
Scott Dorman wrote: extension methods
Actually, that was my original intention, but I wanted to support folks who were using a pre-VS2008 version of the IDE. I think I'll move it to an extension method anyway - it's crying out to be moved!
Thanks,
/ravi
|
|
|
|