Question Resistor color code

Pavle

Well-known member
Joined
Oct 4, 2016
Messages
47
Programming Experience
1-3
I made resistor app that calculates resistance from colors but I don't know how to make app that coverts resistance to color.Can you help me make this?
 
private void button1_Click(object sender, EventArgs e)
{
    if (comboBox1.Text == "4")
    {
        if (textBox1.Text == "0")
        {
            MessageBox.Show("Otpornost mora biti veca od nule!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
        if(comboBox2.Text=="")
        {
            MessageBox.Show("Izaberi vrednost tolerancije!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
        var colours = new string[] { "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Grey","White", "Silver", "Gold" };
        var factorDictionary = new Dictionary<char, double>() { { 'p', 0.000000000001 }, { 'n', 0.000000001 }, { 'u', 0.000001 }, { 'm', 0.001 }, { 'R', 1 }, { 'K', 1000 }, { 'M', 1000000 }, { 'G', 1000000000 } };
        string res = string.Empty;

        while (string.IsNullOrEmpty(res))
        {
            res = textBox1.Text;
        }

        var lastChar = res.Last();
        var isUnitCorrect = factorDictionary.ContainsKey(lastChar);
        var value = res.Substring(0, res.Length - 1);
        var isValueCorrect = !value.Any(x => !char.IsDigit(x));

        if (isUnitCorrect && isValueCorrect)
        {
            double mul = factorDictionary[lastChar];
            double val = double.Parse(value) * mul;
            int third = 0;

            if (val < 0.1)
            {
                MessageBox.Show("Otpornost ne moze da bude manja od 0.1 oma!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (val < 1)
            {
                val *= 100;
                third = 10;
            }
            else if (val < 10)
            {
                val *= 10;
                third = 11;
            }

            res = val.ToString();

            if (res.Count() > 24)
            {
                MessageBox.Show("Pogresna vrednost!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                label15.BackColor = Color.FromName(colours[res[0] - '0']);
                label16.BackColor = Color.FromName(colours[res[1] - '0']);
                label17.BackColor = Color.FromName(colours[third != 0 ? third : res.Count() - 2]);
                label6.Text = textBox1.Text;
                textBox2.Text = colours[res[0] - '0'];
                textBox3.Text = colours[res[1] - '0'];
                textBox4.Text = colours[third != 0 ? third : res.Count() - 2];
            }
            if (comboBox1.Text == "4")
            {
                if (comboBox2.Text == "E24 (5%)")
                {
                    label18.BackColor = Color.Gold;
                    textBox5.Text = "Gold";
                }
                if (comboBox2.Text == "E12 (10%)")
                {
                    label18.BackColor = Color.Silver;
                    textBox5.Text = "Silver";
                }
            }
        }
        else
        {
            MessageBox.Show("Invalid value!", "Otpornost", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
}
 
Last edited by a moderator:
How to show closest standard value
It doesn't seem to me that you're putting any effort into this yourself. If I gave you a list of numbers and asked you which of them another specific number was closest to then I'm confident that you could tell me. If you can do that then you can answer your own question here. I'm done with this thread.
 
Back
Top Bottom