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

Annotation of /trunk/OhmsLawCalc/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 61 - (hide annotations) (download)
Sun Mar 15 06:13:32 2015 UTC (4 years, 10 months ago) by william
File size: 5587 byte(s)

1 william 26 //#define SHOW_DEBUG_MENU // when defined the debug menu will be shown
2     //#define ENABLE_API_TESTING // when defined will enable API_TESTING
3 william 11 using Enterprise.Logging;
4 william 12 using libOhmsLawCalc;
5 william 20 using OhmsLawCalc.UI;
6 william 9 using System;
7 william 3 using System.Collections.Generic;
8     using System.ComponentModel;
9     using System.Data;
10     using System.Drawing;
11 william 61 using System.IO;
12 william 3 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 william 10 NativeMethods.AttachToConsole(true);
23 william 51 gLog.CreateLog("OhmsLawCalc.log", false, LogLevel.kLogLevel_All_NoProgress, new LogWriter.LogStream());
24 william 11 #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 william 3 }
31 william 11
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 william 23 // test API
48 william 12 OhmsLawAPITest test = new OhmsLawAPITest();
49     test.TestAPI();
50 william 11 #endif
51     }
52 william 20
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 william 23 ResetTabPage(GetCurrentTab());
81 william 20 }
82     private void btnResistanceReset_Click(object sender, EventArgs e)
83     {
84     ResetTabPage(GetCurrentTab());
85 william 23 }
86 william 20 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 william 21
111     OhmsLawValueType type = OhmsLawValueType.None;
112     if (tp.Tag != null)
113     {
114     type = (OhmsLawValueType)tp.Tag;
115     }
116 william 20 foreach (var c in tp.Controls)
117     {
118     var p = (c as ICalc);
119     if (p != null)
120     {
121 william 22 OhmsLawValueType _type; //not used
122     var v = p.GetValue(out _type);
123 william 20 if (v.Value != 0)
124     {
125     values.Add(v);
126     }
127     }
128     }
129 william 21 if (values.Count == 0) { return; }
130     var result = OhmsLaw.CreateInstance(type, values);
131 william 22
132 william 23 result = OhmsLaw.AutoConvertResult(result);
133    
134     ISetValue sv = GetResultControlForTabPage(tp);
135     if (sv != null)
136     {
137     sv.SetValue(result);
138     }
139 william 20 }
140 william 21
141 william 22 private ISetValue GetResultControlForTabPage(TabPage tp)
142     {
143     OhmsLawValueType type = OhmsLawValueType.None;
144     Control ctl = new Control();
145     if (tp.Tag != null)
146     {
147     type = (OhmsLawValueType)tp.Tag;
148     }
149     foreach (var c in tp.Controls)
150     {
151     var p = (c as ICalc);
152     if (p != null)
153     {
154     OhmsLawValueType _type;
155     var v = p.GetValue(out _type);
156     if (_type == type)
157     {
158     ctl = (c as Control);
159     break;
160     }
161     }
162     }
163    
164     if (ctl != null)
165     {
166     return (ctl as ISetValue);
167     }
168     return null;
169     }
170    
171 william 21 private void Form1_Load(object sender, EventArgs e)
172     {
173     var tp = tbCalc.TabPages["tpCurrent"];
174     tp.Tag = OhmsLawValueType.Current;
175     tp = tbCalc.TabPages["tpResistance"];
176     tp.Tag = OhmsLawValueType.Resistance;
177     tp = tbCalc.TabPages["tpVoltage"];
178     tp.Tag = OhmsLawValueType.Voltage;
179     tp = tbCalc.TabPages["tpPower"];
180     tp.Tag = OhmsLawValueType.Power;
181     }
182 william 3 }
183     }

  ViewVC Help
Powered by ViewVC 1.1.22