本人有一个朋友是学电路的。这几天,找到了我 想让我给写一个电阻色环查询器(根据电阻上面表明的色环颜色,判断电阻的大小)。由于,不是电路的翻阅的许多资料,才知道怎么根据色环查阻值。今天,把源码发上来与大家学习一下。注:由于时间仓促只完成的4环型的,且代码不太规范 【高手无喷】
namespace DianZuChaXunQi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> l;
List<Color> fontColor;
List<int> lis;
List<string> historyList = new List<string>();
int f = 0;
int s = 0;
int t = 0;
int f4 = 0;
string history = null;
private void Form1_Load(object sender, EventArgs e)
{
fontColor = new List<Color>();
fontColor.AddRange(new Color[] {Color.Black,Color .Brown ,Color .Red ,Color .Orange ,Color .Yellow ,Color .Green
,Color .Blue ,Color.Purple ,Color.Gray ,Color .White ,Color .Gold ,Color .Silver });
ComboBoxItemAdd(1);
int[] num = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 10 };
lis = new List<int>();
lis.AddRange(num);
}
private void ComboBoxItemAdd(int style)
{
string str_Color = "黑.棕.红.橙.黄.绿.蓝.紫.灰.白.金.银";
string[] colorList = str_Color.Split(new char[] { '.' });
l = new List<string>();
l.AddRange(colorList);
switch (style)
{
case 1:
l.RemoveAt(10);
l.RemoveAt(l.Count - 1);
break;
case 2:
l.RemoveAt(0);
l.RemoveAt(2);
l.RemoveAt(2);
l.RemoveAt(5);
l.RemoveAt(5);
fontColor.RemoveAt(0);
fontColor.RemoveAt(2);
fontColor.RemoveAt(2);
fontColor.RemoveAt(5);
fontColor.RemoveAt(5);
break;
default:
break;
}
yi.Items.Clear();
foreach (string cs in l)
{
this.yi.Items.Add(cs);
}
}
private void yi_SelectedIndexChanged(object sender, EventArgs e)
{
}
private bool FeiFaXuanZe(int a, int b)
{
bool bb = true;
if (yi.SelectedIndex == a || yi.SelectedIndex == b)
{
MessageBox.Show("选择非法请重选!");
bb = false;
}
else
{
bb = true;
}
return bb;
}
private void yi_SelectedValueChanged(object sender, EventArgs e)
{
if (this.l1.Text == "无")
{
l1.Text = yi.SelectedItem.ToString();
f = lis[yi.SelectedIndex];
l1.ForeColor = fontColor[yi.SelectedIndex];
history += l1.Text;
}
else
{
if (l2.Text == "无")
{
l2.Text = yi.SelectedItem.ToString();
s = lis[yi.SelectedIndex];
l2.ForeColor = fontColor[yi.SelectedIndex];
history += l2.Text;
}
else
{
if (l3.Text == "无")
{
l3.Text = yi.SelectedItem.ToString();
t = lis[yi.SelectedIndex];
l3.ForeColor = fontColor[yi.SelectedIndex];
ComboBoxItemAdd(2);
history += l3.Text;
}
else
{
if (l4.Text == "无")
{
l4.Text = yi.SelectedItem.ToString();
history += l4.Text;
lis.Remove(0);
lis.Remove(3);
lis.Remove(4);
lis.Remove(8);
lis.Remove(9);
f4 = lis[yi.SelectedIndex];
l4.ForeColor = fontColor[yi.SelectedIndex];
}
else
{ return; }
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string word = "无";
if (l1.Text == word || l2.Text == word || l3.Text == word || l4.Text == word)
{
MessageBox.Show("你还有没有选择的色环!请继续选择!!");
}
else
{
this.label5.Text = Fun(f, s, t, f4);
if (history != null)
{
if (historyList.Contains(history) == false )
{
historyList.Add(history);
ListViewItem lst = new ListViewItem(history);
lst.SubItems.Add(label5.Text);
this.listView1.Items.Add(lst);
}
}
fun1(sender, e);
}
}
private string Fun(int a, int b, int c, int d)
{
Dictionary<int, float> WuCha = new Dictionary<int, float>();
WuCha.Add(1, 1);
WuCha.Add(2, 2);
WuCha.Add(5, 0.5f);
WuCha.Add(6, 5);
WuCha.Add(7, 5);
WuCha.Add(10, 5);
WuCha.Add(11, 10);
string request = (a * 10 + b).ToString() + "e" + c.ToString() + "±" + WuCha[d] + "%";
return request;
}
private void button2_Click(object sender, EventArgs e)
{
fun1(sender ,e);
}
private void fun1(object sender, EventArgs e)
{
Form1_Load(sender, e);
Label[] lab = { l1, l2, l3, l4 };
foreach (Label tt in lab)
{
tt.Text = "无";
tt.ForeColor = System.Drawing.SystemColors.GradientActiveCaption;
}
this.history = null;
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (MessageBox.Show("是否确认删除!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
this.listView1.Items.Clear();
historyList.Clear();
}
else
return;
}