Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Powershell WPF GUI: ToolBox control Textbox watermark placeholder demo

microsoft powershell, wpf, windows presentation frameworks winforms, textbox control toolbox, visual studio, watermark textbox powershell

In this demo I will be showing how to put PlaceHolder text on textbox control on Powershell WPF (Windows presentation framework) GUI. here I have 3 textboxes. When I launches GUI, all the textboxes shows some default darkgray color watermark text information. When I click on them text will automatically disappear and you can start typing your own text without removing or deleting default text. If there is no text on the box or empty and when you move on to another box, It will show the default help message automatically.

I have created default XAML form with visual studio. Textbox color is Darkgray, I have found 2 events on .NET API Reference documentation by Microsoft. I have given 2 url below, It shows how to achieve watermark textbox.
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.gotfocus(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus(v=vs.110).aspx

There are two main events gotfocus and lostfocus. This defines if there is no text what will happened when I click textbox or click another textbox. Here in the first event gotfocus, When I click on the text box, if it finds text is 'Type valid First Name', Textbox becomes empty and font color is changed to black.

$FirstName.Add_GotFocus({
    if ($FirstName.Text -eq 'Type valid First Name') {
        $FirstName.Foreground = 'Black'
        $FirstName.Text = ''
    }
})

In the next when I click somewhere else on any other control, and if textbox is empty it will change the text to 'Type valid First Name' and color will change to default one Darkgray.

$FirstName.Add_LostFocus({
    if ($FirstName.Text -eq '') {
        $FirstName.Text = 'Type valid First Name'
        $FirstName.Foreground = 'Darkgray'
    }
})

You can download this script here, it is also available on github.com.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#Load required libraries
Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase, System.Windows.Forms, System.Drawing 

[xml]$xaml = @"
<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"

        Title="PlaceHolder Text Demo" Height="141" Width="300" Topmost="True">
    <Grid>
        <TextBox x:Name="FirstName" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="Type valid First Name" VerticalAlignment="Top" Width="200" Foreground="DarkGray" />
        <TextBox x:Name="LastName" HorizontalAlignment="Left" Height="23" Margin="10,38,0,0" TextWrapping="Wrap" Text="Type valid Last Name" VerticalAlignment="Top" Width="200" Foreground="DarkGray"/>
        <TextBox x:Name="Contry" HorizontalAlignment="Left" Height="23" Margin="10,66,0,0" TextWrapping="Wrap" Text="Type valid contry" VerticalAlignment="Top" Width="200" Foreground="DarkGray"/>

    </Grid>
</Window>

"@

#Read the form
$Reader = (New-Object System.Xml.XmlNodeReader $xaml) 
$Form = [Windows.Markup.XamlReader]::Load($reader) 

#AutoFind all controls
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")  | ForEach-Object { 
  New-Variable  -Name $_.Name -Value $Form.FindName($_.Name) -Force 
}

#Textbox placeholder remove default text when textbox clicked
$FirstName.Add_GotFocus({
    
    if ($FirstName.Text -eq 'Type valid First Name') {
        $FirstName.Foreground = 'Black'
        $FirstName.Text = ''
    }
})
#Textbox placeholder grayed out text when textbox clicked
$FirstName.Add_LostFocus({
    if ($FirstName.Text -eq '') {
        $FirstName.Text = 'Type valid First Name'
        $FirstName.Foreground = 'Darkgray'
    }
})

#Textbox placeholder remove default text when textbox clicked
$LastName.Add_GotFocus({
    
    if ($LastName.Text -eq 'Type valid Last Name') {
        $LastName.Foreground = 'Black'
        $LastName.Text = ''
    }
})
#Textbox placeholder grayed out text when textbox clicked
$LastName.Add_LostFocus({
    if ($LastName.Text -eq '') {
        $LastName.Text = 'Type valid Last Name'
        $LastName.Foreground = 'Darkgray'
    }
})

#Textbox placeholder remove default text when textbox clicked
$Contry.Add_GotFocus({
    
    if ($Contry.Text -eq 'Type valid contry') {
        $Contry.Foreground = 'Black'
        $Contry.Text = ''
    }
})
#Textbox placeholder grayed out text when textbox clicked
$Contry.Add_LostFocus({
    if ($Contry.Text -eq '') {
        $Contry.Text = 'Type valid contry'
        $Contry.Foreground = 'Darkgray'
    }
})

#Mandetory last line of every script to load form
$Form.ShowDialog()

Userful Articles
Part 1: Create WPF XAML powershell GUI form with Visual studio
Part 2: Powershell and WPF: Build GUI applications tutorial
Part 3: Create shorter Microsoft Powershell WPF automated clean script
Powershell PoshGUI: Convert user to SID and vice versa using
Microsoft Powershell GUI: Change Internet Options connections Lan settings proxy server grayed out

Go Back



Comment

Blog Search

Page Views

12162345

Follow me on Blogarama