Jump to content

Ghost text in visual studio?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Bloodhawk

Bloodhawk

    Newbie

  • Members
  • Pip
  • 2 posts
I'm trying to make ghost text in a text box on a C# form in Visual Studio 2010, but haven't been able to figure out how to do it, if anyone can help it would be greatly appreciated.

If at all possible, I would like the form to not recognize the ghost text as actual input data (so my try/catch processes still work)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Why not use the text color as the flag. When the user starts typing in the field, change the text color to normal. Refuse to process the data of "ghosted" text.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
Take a look at WaterMark TextBox For Desktop Applications Using C#, .NET 3.5 and VS2008 - CodeProject

#4
Bloodhawk

Bloodhawk

    Newbie

  • Members
  • Pip
  • 2 posts
I figured it out with a little networking. I'll post what I did up here if anyone is interested

set whatever message you want in the textbox and set the ForeColor to GrayText in the object's default settings then add this to the code for the textbox in question:


[/HR]
private void Object_Enter(object sender, EventArgs e)
{
Object.Text = "";
Object.ForeColor = SystemColors.WindowText;
}


private void Object_Leave(object sender, EventArgs e)
{
if (Object.Text != "") //Not blank
{


}
else //missing data
{
Object.Text = "Ghost-text";
Object.ForeColor = SystemColors.GrayText;
}
}

[/HR]
After that, it's just a matter of setting your try-catch or if-else statements to ignore everything with the "ForeColor = SystemColors.GrayText" setting, as WingedPanther had pointed out.

I'm sure there are easier ways to do this, but it's what I was able to throw together in a night of tinkering. Thanks for helping. :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users