 |
|
 |
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
|
|
|
|
 |
|
|
 |
|
 |
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. ///
/// The text box.
/// 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. ///
/// 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
|
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Hi
I added the class cueProvider to another project and i do not get it to work.
No errors , just dont have any effect.
I have VS2005 and framwork 3.5
Heino
|
|
|
|
 |
|
 |
Odd. What OS are you running?
/ravi
|
|
|
|
 |
|
 |
I running XP Prof. with servicepack 3
/Heino
|
|
|
|
 |
|
 |
Not sure why it isn't working for you. If you email me your project (source only) I can take a look.
Thanks,
/ravi
|
|
|
|
 |
|
 |
I took a look at your source - you had the calls to CueProvider in the wrong place - they should be in Form1_Load(). Your app works fine when you move them there.
Thanks,
/ravi
|
|
|
|
 |
|
 |
Hi again
Nop , sorry but i already tried that and it does not work either.
That’s why I moved them.
So , I do not understand what’s wrong with my environment.
/Heino
|
|
|
|
 |
|
 |
OK, resend me the .zip and I'll take a look at it.
Thanks,
/ravi
|
|
|
|
 |