本文介绍了使用Twitter之类的文本框并提供默认文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我想开发一个具有Twitter之类的应用程序,例如文本框,并且具有默认文本,当我们在文本框中输入内容时,该文本会消失.

以下是它的图片-
http://www.freeimagehosting.net/e6f8a [ ^ ]

hello friends,

i want to develop an application which have twitter like textboxes and having default text which disappears when we enter in the textbox.

following is an image of it-
http://www.freeimagehosting.net/e6f8a[^]

推荐答案

public partial class Form1 : Form
    {

        private String defaultText = "Enter Some Text";

        public Form1()
        {
            InitializeComponent();

            textBox1.Text = defaultText;
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals(defaultText))
            {
                textBox1.SelectionStart = 0;
            }
            else
            {
                textBox1.SelectionStart = textBox1.Text.Length;
            }
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text.Length <= 0)
            {
                textBox1.Text = defaultText;
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBox1.Text.Equals(defaultText))
            {
                textBox1.Text= String.Empty;
            }
        }
    }


这篇关于使用Twitter之类的文本框并提供默认文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:18
查看更多