NEWS

Hướng dẫn tạo watermark trong Textbox giống thuộc tính placeholder trong thiết kế web

Hướng dẫn tạo watermark trong Textbox giống thuộc tính placeholder trong thiết kế web
Đăng bởi: Thảo Meo - Lượt xem: 31784 10:39:14, 25/11/2016DEVEXPRESS   In bài viết

Hôm nay, mình xin tiếp tục hướng dẫn các bạn cách tạo Watermark trong textbox. 

Giống thuộc tính placeholder trong textbox của html5.

Vậy Watermark là gì?

Trên một số tài liệu hay hình ảnh các bạn thường hay thấy các dòng chữ mờ mờ ghi thương hiệu hay tác giả tạo ra tài liệu, hình ảnh – đó chính là Watermark. Trên hình 1  một ví dụ về Watermark trong Word.

Giao diện Demo ứng dụng:

text_water_mark_csharp_vb_net

Code Csharp:

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;
            }
        }
    }
}

Code 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

Chúc các bạn thành công!

Hãy nhớ like and share nha các bạn!

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

Hướng dẫn tạo watermark trong Textbox giống thuộc tính placeholder trong thiết kế web
Đăng bởi: Thảo Meo - Lượt xem: 31784 10:39:14, 25/11/2016DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.