1

I am very new to WPF and C# in general. I am trying to add a textholder on my textbox, when you focus on the textbox, i want the text to disappear, and if I move to something else (aka loses focus), and the text is empty, i want the textbox to add the text I want the text back.

but I get error on GotFocus.EventHandle(RemoveText); and LostFocus.EventHandle(AddText); on the words "GotFocus" and "Lostfocus" (on the code below)

what am I missing?

I tried this next code:

ExcelPath.Text = "Please Drag Excel into here";
ExcelPath.GotFocus += GotFocus.EventHandle(RemoveText);
ExcelPath.LostFocus += LostFocus.EventHandle(AddText);

public void RemoveText(object sender, EventArgs e)
{
    if (ExcelPath.Text == "Please Drag Excel into here")
    {
        ExcelPath.Text = "";
    }
}

public void AddText(object sender, EventArgs e)
{
    if (string.IsNullOrWhiteSpace(ExcelPath.Text))
        ExcelPath.Text = "Please Drag Excel into here";
}
jamesnet214
  • 1,044
  • 13
  • 21
oded gross
  • 41
  • 1
  • 6

2 Answers2

3

This is the syntax for hooking up the event handlers:

ExcelPath.Text = "Please Drag Excel into here";
ExcelPath.GotFocus += RemoveText;
ExcelPath.LostFocus += AddText;

Get rid of GotFocus.EventHandle whatever that is.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • i actually tried that and it didn't work :( . – oded gross May 18 '21 at 07:48
  • Also I the GotFocus.EventHandle because when i was searching for answer, many gave that answer with GotFocus.EventHandle so i tried it... but it didnt work. – oded gross May 18 '21 at 07:54
  • What does "didn't work" mean? What happens? – mm8 May 18 '21 at 11:35
  • Hello, today i came with a renew power after banging my head to much yeterday, it actually worked, i just did something stupid because i guess i was tired. it worked, thank you so much for the help! – oded gross May 18 '21 at 14:03
0

You can do that with a watermark textbox. You find a lot of it in the internet. For example here: Watermark / hint text / placeholder TextBox

Or this is also very helpful: Create WPF Watermark in Pure XAML

With this help-links you do not have to try everything by urself.

Presi
  • 806
  • 10
  • 26
  • its useful, but its mainly XAML in WPF which is a problem for me, I just wanted it code wise on Forms, and if Forms have XAML i dunno actually where its (as i said i am new, i only saw XAML when i use main window but i dont use it at all) – oded gross May 18 '21 at 07:50