 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
Not sure why it isn't working for you. If you email me your project (source only) I can take a look.
Thanks,
/ravi
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |