Jump to content

Clear text inside label

Hello everyone,

 

So i'm a student in the IT branch and i'm running into some problems with visual studio, i would like to empty a label text when there is text in it.

 

Example: i have a checkbox is checked it say's needs: this but when i uncheck it the label wouldn't clear text inside the label so this stays in the label.

 

This is my code:

        private void selectie_Load(object sender, EventArgs e)
        {
            lblEisen.Text = "";
            lblOpleiding.Text = "";

        }

        private void chbNiveau2_CheckedChanged(object sender, EventArgs e)
        {
            if (chbNiveau2.Checked == true)
            {
                lblEisen.Text = "Minimaal BBL diploma.";
                lblOpleiding.Text = "Maakt niet uit.";
                chbNiveau3.Checked = false;
                chbNiveau4.Checked = false;

            }else
            {
                lblEisen.Text = ToString();
                lblOpleiding.Text = ToString();
            }
            

        }

        private void chbNiveau3_CheckedChanged(object sender, EventArgs e)
        {
            if (chbNiveau3.Checked == true)
            {
                lblEisen.Text = "Minimaal KBL diploma.";
                lblOpleiding.Text = "Maakt niet uit of ICT Niveau 2 behaald.";
                chbNiveau2.Checked = false;
                chbNiveau4.Checked = false;
            }else
            {
                lblEisen.Text = ToString();
                lblOpleiding.Text = ToString();

            }

        private void chbNiveau4_CheckedChanged(object sender, EventArgs e)
        {
            if (chbNiveau4.Checked == true)
            {
                lblEisen.Text = "Minimaal KBL of TL diploma.";
                lblOpleiding.Text = " Metaal en Elektro of ICT Niveau 3 behaald.";
                chbNiveau2.Checked = false;
                chbNiveau3.Checked = false;
            }else
            {
                lblEisen.Text = ToString();
                lblOpleiding.Text = ToString();
            }

Could u guys see what i'm doing wrong.

 

Sorry for the bad coding i just started with C# but i want to get the hang of it.

Link to comment
Share on other sites

Link to post
Share on other sites

You can clear it by setting it to the empty string. Here's a few different ways to write it but it all basically does the same thing.

Textbox1.Text = "";
Textbox2.Text = string.Empty;
Textbox3.Text = String.Empty;

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, madknight3 said:

You can clear it by setting it to the empty string. Here's a few different ways to write it but it all basically does the same thing.


Textbox1.Text = "";
Textbox2.Text = string.Empty;
Textbox3.Text = String.Empty;

 

And must i place it inside the private void chechbox?

Link to comment
Share on other sites

Link to post
Share on other sites

40 minutes ago, Briareos7 said:

And must i place it inside the private void chechbox?

Yes, you want to replace this

lblEisen.Text = ToString();
lblOpleiding.Text = ToString();

With one of the options I mentioned. Example

lblEisen.Text = "";
lblOpleiding.Text = "";

Using ToString() like you are isn't correct. It needs to be called on an object.

Link to comment
Share on other sites

Link to post
Share on other sites

i've tried that but the text still won't go away when unchecked

 

this is how my code looks like now:

        private void selectie_Load(object sender, EventArgs e)
        {
            lblEisen.Text = "";
            lblOpleiding.Text = "";

        }

        private void chbNiveau2_CheckedChanged(object sender, EventArgs e)
        {
            if (chbNiveau2.Checked == true)
            {
                lblEisen.Text = "Minimaal BBL diploma.";
                lblOpleiding.Text = "Maakt niet uit.";
                chbNiveau3.Checked = false;
                chbNiveau4.Checked = false;

            }else
            {
                lblEisen.Text = "";
                lblOpleiding.Text = "";
            }
            

        }

        private void chbNiveau3_CheckedChanged(object sender, EventArgs e)
        {
            if (chbNiveau3.Checked == true)
            {
                lblEisen.Text = "Minimaal KBL diploma.";
                lblOpleiding.Text = "Maakt niet uit of ICT Niveau 2 behaald.";
                chbNiveau2.Checked = false;
                chbNiveau4.Checked = false;
            }else
            {
                lblEisen.Text = "";
                lblOpleiding.Text = "";

            }

        private void chbNiveau4_CheckedChanged(object sender, EventArgs e)
        {
            if (chbNiveau4.Checked == true)
            {
                lblEisen.Text = "Minimaal KBL of TL diploma.";
                lblOpleiding.Text = " Metaal en Elektro of ICT Niveau 3 behaald.";
                chbNiveau2.Checked = false;
                chbNiveau3.Checked = false;
            }else
            {
                lblEisen.Text = "";
                lblOpleiding.Text = "";
            }

        private void lblEisen_Click(object sender, EventArgs e)
        {

        }

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Briareos7 said:

i've tried that but the text still won't go away when unchecked

If you set a breakpoint in your method and step through, does it reach the code that clears the text boxes?

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

If you want to change your label's text use .content

 

label1.content= "";

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×