Wednesday, 25 June 2014

WaterMark TextBox In Windows Applications Using C# and VB.Net

6/25/2014 - By Pranav Singh 2

In this article I will explain how you can Watermark textbox in Windows Application Using c#.net and Vb.Net. This article will explain you How to add watermark property in Textbox of WinForm or in a desktop application using C#.net. Shadow text in a windows application using C#.Net and VB.Net.

Now for this application we will create a windows application and add the below code as shown below.

C#.Net
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DemoWindowsApplication
{
    public partial class WaterMark : Form
    {
        public WaterMark()
        {
            InitializeComponent();
            textBox1.ForeColor = Color.LightGray;
            textBox1.Text = "Please Enter Name";
            this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
            this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);

            textBox2.ForeColor = Color.LightGray;
            textBox2.Text = "Please Enter Address";
            this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
            this.textBox2.Enter += new System.EventHandler(this.textBox2_Enter);
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                textBox1.Text = "Please Enter Name";
                textBox1.ForeColor = Color.Gray;
            }
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (textBox1.Text == "Please Enter Name")
            {
                textBox1.Text = "";
                textBox1.ForeColor = Color.Black;
            }
        }

        private void textBox2_Enter(object sender, EventArgs e)
        {
            if (textBox2.Text == "Please Enter Address")
            {
                textBox2.Text = "";
                textBox2.ForeColor = Color.Black;
            }
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                textBox2.Text = "Please Enter Address";
                textBox2.ForeColor = Color.Gray;
            }
        }
    }
}

VB.Net
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Namespace DemoWindowsApplication
    Partial Public Class WaterMark
        Inherits Form
        Public Sub New()
            InitializeComponent()
            textBox1.ForeColor = Color.LightGray
            textBox1.Text = "Please Enter Name"
            Me.textBox1.Leave += New System.EventHandler(Me.textBox1_Leave)
            Me.textBox1.Enter += New System.EventHandler(Me.textBox1_Enter)

            textBox2.ForeColor = Color.LightGray
            textBox2.Text = "Please Enter Address"
            Me.textBox2.Leave += New System.EventHandler(Me.textBox2_Leave)
            Me.textBox2.Enter += New System.EventHandler(Me.textBox2_Enter)
        End Sub

        Private Sub textBox1_Leave(ByVal sender As Object, ByVal e As EventArgs)
            If textBox1.Text = "" Then
                textBox1.Text = "Please Enter Name"
                textBox1.ForeColor = Color.Gray
            End If
        End Sub

        Private Sub textBox1_Enter(ByVal sender As Object, ByVal e As EventArgs)
            If textBox1.Text = "Please Enter Name" Then
                textBox1.Text = ""
                textBox1.ForeColor = Color.Black
            End If
        End Sub

        Private Sub textBox2_Enter(ByVal sender As Object, ByVal e As EventArgs)
            If textBox2.Text = "Please Enter Address" Then
                textBox2.Text = ""
                textBox2.ForeColor = Color.Black
            End If
        End Sub

        Private Sub textBox2_Leave(ByVal sender As Object, ByVal e As EventArgs)
            If textBox2.Text = "" Then
                textBox2.Text = "Please Enter Address"
                textBox2.ForeColor = Color.Gray
            End If
        End Sub
    End Class
End Namespace

In above code I have fired event in when mouse cursor leave the textbox and enter in the textbox, we are assigning the value and making the color of textbox text change. Which provide watermark effect.

Now run the application

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

  1. Is it possible to have both the grey text and cursor at the same time?

    ReplyDelete
    Replies
    1. Yes we can. Just apply color attribute to textbox as gray

      Delete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top