Notification texts go here Contact Us

Bagaimana cara mengubah teks default menjadi abu-abu di kotak teks? [Windows Forms]

Bagaimana cara merubah teks default windows forms menjadi abu-abu di kotak teks? Berikut contoh dan penjelasannya.


Bagaimana cara mengubah teks default menjadi abu-abu di kotak teks?

Studi kasus:

Saat saya meng-klik kotak teks, saya ingin teks default menghilang. Apakah ada properti lain yang dapat digunakan untuk tujuan ini?

Pemecahan masalah:

Berikut ini Akan mengatur teks default dalam warna abu-abu. Juga Saat Anda membiarkan kotak teks kosong, atur lagi teks default ke kotak teks.

private void Form1_Load(object sender, EventArgs e)
    {
        this.textBox2.Enter += new EventHandler(textBox2_Enter); 
        this.textBox2.Leave += new EventHandler(textBox2_Leave);
        textBox2_SetText();
    }

    protected void textBox2_SetText()
    {
        this.textBox2.Text = "Default Text...";
        textBox2.ForeColor = Color.Gray;
    }

    private void textBox2_Enter(object sender, EventArgs e)
    {
        if (textBox2.ForeColor == Color.Black)
            return;
        textBox2.Text = "";
        textBox2.ForeColor = Color.Black;
    }
    private void textBox2_Leave(object sender, EventArgs e)
    {
        if (textBox2.Text.Trim() == "")
            textBox2_SetText();
    }
Contoh hasil:

Posting Komentar

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.