使用Twitter之类的文本框并提供默认文本 [英] using twitter like textboxes and giving default text

查看:109
本文介绍了使用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[^]

推荐答案

您可以执行以下操作,创建一个包含2个文本的新表单框上,这只是一个快速演示,您必须处理事件等,才能使其按自己的意愿进行.创建一个继承自文本框的新类,然后在需要该功能的任何地方使用它,将是更好的选择.

此处的文本框1输入了一些默认文本,在按键上将其清除以允许用户输入.如果用户将控件移出控件,将其留空,则它将使用默认字符串重新填充文本字段,同样,如果用户输入了表单,并且已经被修改,则光标将放置在用户上一个条目的末尾(您可以当然选择所有文字或其他内容)

You can do something like this, create a new form with 2 text boxes on it, this is just a quick demo, you would have to play with the events etc. to get it do to what you want. You would also be better to create a new class that inherits from textbox and then use it anywhere you want the functionality;

Here text box 1 has some default text entered, on a keypress it clear to allow user entry. If the user moves off the control leaving it empty, it repopulates the text field with the default string, likewise if the user enters the form, and it is already been modified the cursors is placed at the end of the users previous entry (you could of course select all text or whatever)

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之类的文本框并提供默认文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆