/[OhmsLawCalc]/trunk/OhmsLawCalc/Form1.cs
ViewVC logotype

Contents of /trunk/OhmsLawCalc/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 22 - (show annotations) (download)
Wed Jan 29 07:57:25 2014 UTC (5 years, 11 months ago) by william
File size: 5488 byte(s)

1 
2 #define SHOW_DEBUG_MENU // when defined the debug menu will be shown
3 #define ENABLE_API_TESTING // when defined will enable API_TESTING
4 using Enterprise.Logging;
5 using libOhmsLawCalc;
6 using OhmsLawCalc.UI;
7 using System;
8 using System.Collections.Generic;
9 using System.ComponentModel;
10 using System.Data;
11 using System.Drawing;
12 using System.Text;
13 using System.Windows.Forms;
14
15 namespace OhmsLawCalc
16 {
17 public partial class Form1 : Form
18 {
19 public Form1()
20 {
21 InitializeComponent();
22 NativeMethods.AttachToConsole(true);
23 gLog.CreateLog("OhmsLawCalc.log", false, LogLevel.kLogLevel_All_NoProgress, null);
24 #if SHOW_DEBUG_MENU
25 #warning SHOW_DEBUG_MENU is defined -- please undefine it after your are done testing the API
26 mnuDebug.Visible = true;
27 #else
28 mnuDebug.Visible = false;
29 #endif
30 }
31
32 private void mnuItemExit_Click(object sender, EventArgs e)
33 {
34 this.Close();
35 }
36
37 private void mnuItemTestAPI_Click(object sender, EventArgs e)
38 {
39 #if !ENABLE_API_TESTING
40 // ENABLE_API_TESTING is not defined
41 MessageBox.Show("API Testing has not been enabled.", "Feature Unavailable", MessageBoxButtons.OK, MessageBoxIcon.Warning);
42 return;
43 #else
44 // ENABLE_API_TESTING is defined
45 #warning ENABLE_API_TESTING is defined -- please undefine it after your are done testing the API
46
47 // test API
48 OhmsLawAPITest test = new OhmsLawAPITest();
49 test.TestAPI();
50 #endif
51 }
52
53 private TabPage GetCurrentTab()
54 {
55 return tbCalc.SelectedTab;
56 }
57
58 private void btnCalcCurrent_Click(object sender, EventArgs e)
59 {
60 CalcTabPage(GetCurrentTab());
61 }
62
63 private void btnCalcResistance_Click(object sender, EventArgs e)
64 {
65 CalcTabPage(GetCurrentTab());
66 }
67
68 private void btnCalcVoltage_Click(object sender, EventArgs e)
69 {
70 CalcTabPage(GetCurrentTab());
71 }
72
73 private void btnCalcPower_Click(object sender, EventArgs e)
74 {
75 CalcTabPage(GetCurrentTab());
76 }
77
78 private void btnCurrentReset_Click(object sender, EventArgs e)
79 {
80 ResetTabPage(GetCurrentTab());
81 }
82 private void btnResistanceReset_Click(object sender, EventArgs e)
83 {
84 ResetTabPage(GetCurrentTab());
85 }
86 private void btnVoltageReset_Click(object sender, EventArgs e)
87 {
88 ResetTabPage(GetCurrentTab());
89 }
90 private void btnPowerReset_Click(object sender, EventArgs e)
91 {
92 ResetTabPage(GetCurrentTab());
93 }
94
95 private void ResetTabPage(TabPage tp)
96 {
97 foreach (var c in tp.Controls)
98 {
99 var p = (c as IResetInput);
100 if (p != null)
101 {
102 p.Reset();
103 }
104 }
105 }
106
107 private void CalcTabPage(TabPage tp)
108 {
109 List<OhmsLawValue> values = new List<OhmsLawValue>();
110
111 OhmsLawValueType type = OhmsLawValueType.None;
112 if (tp.Tag != null)
113 {
114 type = (OhmsLawValueType)tp.Tag;
115 }
116 foreach (var c in tp.Controls)
117 {
118 var p = (c as ICalc);
119 if (p != null)
120 {
121 OhmsLawValueType _type; //not used
122 var v = p.GetValue(out _type);
123 if (v.Value != 0)
124 {
125 values.Add(v);
126 }
127 }
128 }
129 if (values.Count == 0) { return; }
130 var result = OhmsLaw.CreateInstance(type, values);
131
132 ISetValue sv = GetResultControlForTabPage(tp);
133 if (sv != null)
134 {
135 sv.SetValue(result);
136 }
137 }
138
139 private ISetValue GetResultControlForTabPage(TabPage tp)
140 {
141 OhmsLawValueType type = OhmsLawValueType.None;
142 Control ctl = new Control();
143 if (tp.Tag != null)
144 {
145 type = (OhmsLawValueType)tp.Tag;
146 }
147 foreach (var c in tp.Controls)
148 {
149 var p = (c as ICalc);
150 if (p != null)
151 {
152 OhmsLawValueType _type;
153 var v = p.GetValue(out _type);
154 if (_type == type)
155 {
156 ctl = (c as Control);
157 break;
158 }
159 }
160 }
161
162 if (ctl != null)
163 {
164 return (ctl as ISetValue);
165 }
166 return null;
167 }
168
169 private void Form1_Load(object sender, EventArgs e)
170 {
171 var tp = tbCalc.TabPages["tpCurrent"];
172 tp.Tag = OhmsLawValueType.Current;
173 tp = tbCalc.TabPages["tpResistance"];
174 tp.Tag = OhmsLawValueType.Resistance;
175 tp = tbCalc.TabPages["tpVoltage"];
176 tp.Tag = OhmsLawValueType.Voltage;
177 tp = tbCalc.TabPages["tpPower"];
178 tp.Tag = OhmsLawValueType.Power;
179 }
180 }
181 }

  ViewVC Help
Powered by ViewVC 1.1.22