Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- // var black = Color.Black;
- var colours = new string[] { "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Grey", "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))
- {
- // MessageBox.Show("Enter resistor value (use R, K, M ,G notation for .): ");
- 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 < 1)
- {
- val *= 100;
- third = 9;
- }
- else if (val < 10)
- {
- val *= 10;
- third = 10;
- }
- res = val.ToString();
- if (res.Count() > 24)
- MessageBox.Show("Invalid value");
- 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]);
- }
- if (comboBox1.Text == "4")
- {
- if (comboBox2.Text == "E24 (5%)")
- label18.BackColor = Color.Gold;
- if (comboBox2.Text == "E12 (10%)")
- label18.BackColor = Color.Silver;
- }
- }
- else
- MessageBox.Show("Invalid value!");
- }
Add Comment
Please, Sign In to add comment