C# 实现label的文本可编辑效果

之前一个项目用到许多个label,现在需要点击label实现对label文本进行编辑,可以直接把label换成textbox,但由于label所用较多,改起来较为麻烦,故此用以下方法。

思想:label本身的文本是不可编辑的,故此使用一个textbox,在点击label后,将textbox的位置设置为和label一样,同时将label隐藏,在textbox中输入文本后,将值再赋值给label,textbox隐藏,label显示,即可实现label的编辑效果。

写了个测试的小例子:

效果如下图:


 

public partial class Form1 : Form
    {
        /// <summary>
        /// 用于保存label编辑之前的文本
        /// </summary>
        string oldstr;
        /// <summary>
        /// 用于表示当前编辑的是哪个label的标志位
        /// </summary>
        int lbl;
        Control[] LabelIO;
        public Form1()
        {
            InitializeComponent();
            textBox1.Visible = false;
            LabelIO = new Control[] {label1,label2,label3,label4 ,label5};
        }

        private void label1_Click(object sender, EventArgs e)
        {
            lbl = 0;
            textBox1.Visible = true;
            textBox1.Location = label1.Location;
        }

        private void label2_Click(object sender, EventArgs e)
        {
            lbl = 1;
            textBox1.Visible = true;
            textBox1.Location = label2.Location;
        }

        private void label3_Click(object sender, EventArgs e)
        {
            lbl = 2;
            textBox1.Visible = true;
            textBox1.Location = label3.Location;
        }

        private void label4_Click(object sender, EventArgs e)
        {
            lbl = 3;
            textBox1.Visible = true;
            textBox1.Location = label4.Location;
        }

        private void label5_Click(object sender, EventArgs e)
        {
            lbl = 4;
            textBox1.Visible = true;
            textBox1.Location = label5.Location;
        }

        private void textBox1_LocationChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < LabelIO.Length; i++)
            {
                if (textBox1.Location == LabelIO[i].Location)
                {
                    oldstr = LabelIO[i].Text;
                    textBox1.Text = LabelIO[i].Text;
                    textBox1.Width = LabelIO[i].Width;
                    textBox1.SelectAll();
                }
                if (lbl==i)
                {
                    LabelIO[i].Visible = false;
                }
                else
                {
                    LabelIO[i].Visible = true;
                }
            }
            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string str = textBox1.Text.Replace(" ",""); //避免只输入空格,导致显示空文本的情况
            if (textBox1.Text==null||textBox1.Text==""||str=="")
            {
                LabelIO[lbl].Text = oldstr;
            }
            else
            {
                LabelIO[lbl].Text = textBox1.Text;
            }
            
        }

    }

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值