Introduction
Mathematical programs can be made more interesting when they use graphics to display their results. I use the graphics in C# to draw a representative diagram of the calculation. This calculation is used for Process Control. An instrumentation transmitter is calibrated to output 4 - 20 Milliamps when measuring a typical column of water. Let's say this column of water is a 0" - 120" span of H2O. The calculation will tell us what delta Milliamps should be output for the given percentage process span, or what delta process span is needed to give us the output delta Milliamps. This will allow us to calibrate our instrument. We generally check calibration at 0 %, 25 %, 50 %, 75 %, and 100 % levels of the process span.
Explicit Conversion
A numerical value must be a string
value to be displayed. When that value is used in a calculation, it must be Converted Explicitly. I use a double
variable type as it allows decimal places that will be produced by division.
delta1 = Double.Parse(txtDelta1.Text);
delta3 = span2 * ( delta1/ span1);
Data Validation
Interestingly enough, those same lines of code will take care of most of our Data Validation for us. If Double.Parse
throws an exception, the code in our catch
block will run.
catch(Exception w)
{
MessageBox.Show(w.ToString(), "Important", MessageBoxButtons.OK,
MessageBoxIcon.Stop);
}
The next part of our Data Validation is to ensure that our delta falls within the limits of our span. Otherwise, division by zero, or even memory overflow could occur. Actually, at one time, people considered both of these errors to be one and the same thing.
if ( double.Parse( txtDelta1.Text ) >= 0 )
{
if ( double.Parse( txtDelta1.Text ) <= span1 )
{
blue = false;
delta1 = Double.Parse(txtDelta1.Text);
delta3 = span2 * ( delta1/ span1);
span4 = Double.Parse(txtSpan2a.Text);
txtDelta2.Text = delta3.ToString();
Answere2 = delta3 + span4;
lblAnswere2.Text = txtDelta2.Text.ToString() +
" + " + txtSpan2a.Text.ToString() +
" = " + Answere2.ToString();
lblAnswere2Unit.Visible = true;
}
else
{
MessageBox.Show("Delta must be within Span limits",
"Important", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
else
{
MessageBox.Show("Delta must be within Span limits",
"Important", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
Graphics
Graphics can be manipulated by calculation as well. Here, we convert explicitly to Int32
type variable. We do this because this conversion process will round a double
type variable to an integer value.
Graphics graph = this.CreateGraphics();
Pen pen = new Pen(Color.Red);
if( blue == true)
{
int line2 = Convert.ToInt32(delta2/ span2 * 200) + 350;
graph.DrawLine( pen, line2, 435, line2, 615);
}
else
{
int line = Convert.ToInt32(delta1/ span1 * 200) + 350;
graph.DrawLine( pen, line, 435, line, 615);
}
Conclusion
There is a lot left to do with this program, but I hope this helps you through "conversion hell". Feel free to develop software as you desire. This code is your code.
History
- 11th February, 2007: Initial post