Notification texts go here Contact Us Buy Now!

[C#] How to Edit Text in Label control Runtime

[C#] How to Edit Text in Label control Runtime, hung quang binh [C#] How to Edit Text in Label control Runtime
Hi mọi người, sau một hồi loay hoay tìm hiểu phương thức chỉnh text trên ứng dụng thì mình thấy bài viết này cũng khá hay, nên copy của anh này đem về chia sẽ lại cho mọi người, công nhận phương thức này hay thật, nhưng có nhược điểm đó là chưa lưu trữ được đoạn text mà mình chỉnh sữa đó lại :D, nếu lưu vào settings trong chương trình thì cũng ổn nhưng mình thấy nó không hay lắm.
[C#] How to Edit Text in Label control Runtime
Khi các bạn double click vào Label thì label sẽ chuyển thành TextBox cho các bạn chỉnh sửa text.
Và khi các bạn double click vào vùng Form thì nó sẽ chuyển ngược lại từ Textbox về Label.
 

FULL CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LabelEditRunTime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.DoubleClick += unClickLabel;
            label1.MouseDown += Label1_MouseDown;
            label1.MouseMove += Label1_MouseMove;


        }

        private void Label1_MouseMove(object sender, MouseEventArgs e)
        {
           
            if (e.Button == MouseButtons.Left)
            {
                label1. Left += e.X - mdLoc.X;
                label1.Top += e.Y - mdLoc.Y;
            }
        }

        private void Label1_MouseDown(object sender, MouseEventArgs e)
        {
         
            mdLoc = e.Location;
        }

        Point mdLoc;      

        private void label1_Click(object sender, EventArgs e)
        {
            TextBox tb = null;
            if (label1.Controls.Count > 0)  
            {
                tb = ((TextBox)label1.Controls[0]);
                
                tb.Size = new Size(label1.Size.Width + 15, label1.Size.Height + 15);
                if (tb.Visible) { label1.Text = tb.Text; tb.Hide(); return; };
            }
            else if (sender == null) return;  
            else 
            {
                tb = new TextBox();
                tb.BackColor = SystemColors.Control;
                tb.Parent = label1;    
                tb.Size = new Size(label1.Size.Width + 15, label1.Size.Height + 15); 
                tb.LostFocus += (ss, ee) => { label1.Text = tb.Text; tb.Hide(); };
            }
            tb.BorderStyle = BorderStyle.None;
            tb.Text = label1.Text;  
            tb.Show();
        }

        private void unClickLabel(object sender, EventArgs e) { label1_Click(null, null); }
    }
}

Chúc các bạn thành công với thủ thuật đơn giản và hay này. 
 Theo LapTrinhVB.Net

About the Author

Chia sẽ kiến thức và tư duy sáng tạo của bản thân mỗi ngày cho cộng đồng IT Việt Nam và Thế giới. Chia sẽ thủ thuật lập trình Csharp, Visual Basic, Python, Php, Javascript, Css, Html. Tối ưu hóa Windows, Microsoft, Facebook, Youtube, Chrome...v.v. …

7 comments

  1. THỦ THUẬT HAY MỌI NGƯỜI ĐỂ Ý VÀ ÁP DỤNG CHO CHƯƠNG TRÌNH CỦA BẢN THÂN NHÉ
  2. vào trang chủ tải nhanh, sao trang bài viết tải lâu quá e ?
    1. đúng rồi bạn, mình vào cũng vậy? chắc e nó tối ưu trang chủ chứ chưa quan tâm nhiều đến trang bài viết hay sao ấy:D
    2. e đang tối ưu tiếp c :D, để tăng seo google lên chút :D
  3. không hiểu gì nhưng thấy tốc độ load trang chủ thì nhanh nhưng vào trang bài viết hơi lâu đó e ?
    1. (y) m` loại bỏ những thứ k cần đến ngoài trang chủ r`, nhưng ở trang bài viết thì nhiều js quá nên tối ưu chưa đc a :(
  4. amazing good template <3
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.
NextGen Digital Welcome to WhatsApp chat
Howdy! How can we help you today?
Type here...