Mk0.Software.Widerstandsfar.../Mk0.Software.Widerstandsfar.../Form1.cs

234 lines
8.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Mk0.Software.Widerstandsfarbcode
{
public partial class Form1 : Form
{
private Dictionary<string, string> farbCodes = new Dictionary<string, string>();
private static string[] sizes = { " Ω", " kΩ", " MΩ" };
public Form1()
{
InitializeComponent();
comboBoxAnzahl.SelectedIndex = 1;
PopulateColorComboBoxes();
}
private void PopulateColorComboBoxes()
{
farbCodes.Add("Black", "Schwarz");
farbCodes.Add("Brown", "Braun");
farbCodes.Add("Crimson", "Rot");
farbCodes.Add("OrangeRed", "Orange");
farbCodes.Add("Yellow", "Gelb");
farbCodes.Add("Green", "Grün");
farbCodes.Add("Blue", "Blau");
farbCodes.Add("Purple", "Lila");
farbCodes.Add("Gray", "Grau");
farbCodes.Add("WhiteSmoke", "Weiß");
farbCodes.Add("Goldenrod", "Gold");
farbCodes.Add("Silver", "Silber");
comboBoxColor1.DataSource = new BindingSource(farbCodes, null);
comboBoxColor1.SelectedIndex = 0;
comboBoxColor2.DataSource = new BindingSource(farbCodes, null);
comboBoxColor2.SelectedIndex = 0;
comboBoxColor3.DataSource = new BindingSource(farbCodes, null);
comboBoxColor3.SelectedIndex = 0;
comboBoxColor4.DataSource = new BindingSource(farbCodes, null);
comboBoxColor4.SelectedIndex = 0;
comboBoxColor5.DataSource = new BindingSource(farbCodes, null);
comboBoxColor5.SelectedIndex = 0;
comboBoxColor6.DataSource = new BindingSource(farbCodes, null);
comboBoxColor6.SelectedIndex = 0;
}
private void ComboBoxAnzahl_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBoxAnzahl.SelectedIndex)
{
case 0:
groupBoxRing4.Enabled = false;
groupBoxRing5.Enabled = false;
groupBoxRing6.Enabled = false;
break;
case 1:
groupBoxRing4.Enabled = true;
groupBoxRing5.Enabled = false;
groupBoxRing6.Enabled = false;
break;
case 2:
groupBoxRing4.Enabled = true;
groupBoxRing5.Enabled = true;
groupBoxRing6.Enabled = false;
break;
case 3:
groupBoxRing4.Enabled = true;
groupBoxRing5.Enabled = true;
groupBoxRing6.Enabled = true;
break;
default:
break;
}
CalculateResistor();
}
private void ComboBoxColor_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
Color co = Color.FromName(cb.SelectedValue.ToString());
cb.Parent.BackColor = co;
if (co == Color.Black || co == Color.Blue)
{
cb.Parent.ForeColor = Color.White;
}
else
{
cb.Parent.ForeColor = Color.Black;
}
CalculateResistor();
}
private void CalculateResistor()
{
switch (comboBoxAnzahl.SelectedIndex)
{
case 0: //3 ringe (1+2 wert, 3 multiplikator, toleranz fix 20%)
CalculateResistorValue(comboBoxColor1.SelectedIndex, comboBoxColor2.SelectedIndex, 999, comboBoxColor3.SelectedIndex, 20, 999);
break;
case 1: //4 ringe (1+2 wert, 3 multiplikator, 4 toleranz)
CalculateResistorValue(comboBoxColor1.SelectedIndex, comboBoxColor2.SelectedIndex, 999, comboBoxColor3.SelectedIndex, comboBoxColor4.SelectedIndex, 999);
break;
case 2: //5 ringe (1+2+3 wert, 4 multiplikator, 5 toleranz)
CalculateResistorValue(comboBoxColor1.SelectedIndex, comboBoxColor2.SelectedIndex, comboBoxColor3.SelectedIndex, comboBoxColor4.SelectedIndex, comboBoxColor5.SelectedIndex, 999);
break;
case 3: //6 ringe (1+2+3 wert, 4 multiplikator, 5 toleranz, 6 temperatur-koeffizient)
CalculateResistorValue(comboBoxColor1.SelectedIndex, comboBoxColor2.SelectedIndex, comboBoxColor3.SelectedIndex, comboBoxColor4.SelectedIndex, comboBoxColor5.SelectedIndex, comboBoxColor6.SelectedIndex);
break;
default:
break;
}
}
private void CalculateResistorValue(int wert1, int wert2, int wert3, int multiplikator, int toleranz, int tempkoeff)
{
if (wert3 == 999) //3 oder 4 ringe
{
if (wert1 == 10 || wert1 == 11) { wert1 = 0; } //keine berechnung für silber oder gold im ersten ring
if (wert2 == 10 || wert2 == 11) { wert2 = 0; } //keine berechnung für silber oder gold im zweiten ring
labelResistor.Text = Readable(Multiplikator(((wert1 * 10) + wert2), multiplikator), toleranz, tempkoeff);
}
else //5 oder 6 ringe
{
if (wert1 == 10 || wert1 == 11) { wert1 = 0; } //keine berechnung für silber oder gold im ersten ring
if (wert2 == 10 || wert2 == 11) { wert2 = 0; } //keine berechnung für silber oder gold im zweiten ring
if (wert3 == 10 || wert3 == 11) { wert3 = 0; } //keine berechnung für silber oder gold im dritten ring
labelResistor.Text = Readable(Multiplikator((((wert1 * 100) + (wert2 * 10)) + wert3), multiplikator), toleranz, tempkoeff);
}
}
private string Readable(decimal wert, int toleranz, int tempkoeff)
{
if (wert == 0) { return "gültigen Code wählen"; }
int counter = 0;
while (Math.Round(wert/1000) >=1)
{
wert /= 1000;
counter++;
}
string val = string.Format("{0:n2}{1}", wert, sizes[1]);
//toleranz
switch (toleranz)
{
case 1:
return val + " ± 1 %" + TempKoeff(tempkoeff);
case 2:
return val + " ± 2 %" + TempKoeff(tempkoeff);
case 5:
return val + " ± 0,5 %" + TempKoeff(tempkoeff);
case 6:
return val + " ± 0,25 %" + TempKoeff(tempkoeff);
case 7:
return val + " ± 0,1 %" + TempKoeff(tempkoeff);
case 8:
return val + " ± 0,05 %" + TempKoeff(tempkoeff);
case 10:
return val + " ± 5 %" + TempKoeff(tempkoeff);
case 11:
return val + " ± 10 %" + TempKoeff(tempkoeff);
case 20:
return val + " ± 20 %" + TempKoeff(tempkoeff);
default:
return "gültigen Code wählen";
}
}
private string TempKoeff(int tempkoeff)
{
switch (tempkoeff)
{
case 0:
return ", 200 ppm";
case 1:
return ", 100 ppm";
case 2:
return ", 50 ppm";
case 3:
return ", 15 ppm";
case 4:
return ", 25 ppm";
case 6:
return ", 10 ppm";
case 7:
return ", 5 ppm";
case 8:
return ", 1 ppm";
default:
return "";
}
}
private decimal Multiplikator(decimal wert, int multiplikator)
{
switch (multiplikator)
{
case 0:
return wert * 1;
case 1:
return wert * 10;
case 2:
return wert * 100;
case 3:
return wert * 1000;
case 4:
return wert * 10000;
case 5:
return wert * 100000;
case 6:
return wert * 1000000;
case 7:
return wert * 10000000;
case 8:
return wert * 0; //keine berechnung für grau
case 9:
return wert * 0; //keine berechnung für weiß
case 10:
return wert * 0.1m;
case 11:
return wert * 0.01m;
default:
return wert * 0; //keine berechnung
}
}
}
}