Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put green tick or red cross in winforms?

Tags:

c#

winforms

icons

is there a way that i can put a green tick or a red cross besides a label in windows forms? basically i need to show if configuration success or not. I am using c#.

Thanks.

like image 882
Liban Avatar asked Dec 02 '22 19:12

Liban


1 Answers

Pretty easy to do.

You can add both images, or even labels as I'm using in this example, beside your text label and then toggle manually the Visible property.

In this example, I'm using a button click to show the tick/cross :

    private void button1_Click(object sender, EventArgs e)
    {
        lblCheck.Visible = false;
        lblCross.Visible = false;

        if (CheckConfiguration())
        {
            lblCheck.Visible = true;
        }
        else
        {
            lblCross.Visible = true;
        }
    }

In my designer, lblCheck is a label containing the Unicode Character ✓ (\u2713) with color set as Green, and lblCross a label containing X with color set as red, exactly at the same spot.

Or, you can go with only one label and change the Text & ForeColor property dynamically, like this :

        lblVerif.Text = string.Empty;

        if (CheckConfiguration())
        {
            lblVerif.Text = "✓";
            lblVerif.ForeColor = Color.Green;
        }
        else
        {
            lblVerif.Text = "X";
            lblVerif.ForeColor = Color.Red;
        }

For both ways, it looks like this :

http://i.stack.imgur.com/8F35Y.png

like image 98
Pierre-Luc Pineault Avatar answered Dec 04 '22 09:12

Pierre-Luc Pineault