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

Annotation of /trunk/OhmsLawCalc/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22