Draw crosshairs on the screen - ZedGraph Here's some part of my code is use to draw objects on the screen :

Class members:

private double cursorX = 0;
private double cursorY = 0;
private LineObj cursorOldY = new LineObj();
private LineObj cursorOldX = new LineObj();
private int index1 = 0;

private double cursor2X = 0;
private double cursor2Y = 0;
private LineObj cursor2OldY = new LineObj();
private LineObj cursor2OldX = new LineObj();
private int index2 = -1;

ZedGraph.CurveItem curve = null;


Drawing function :

private void drawCursors(bool first, bool second, bool fromKeyboard = false, Point mousePoint = new Point()) 
{
    GraphPane p = lineGraphControl1.GraphPane;
    if (p.CurveList.Count == 0 || curve == null)
        return;

    ZedGraph.Axis axis = curve.IsY2Axis ? (Axis)p.Y2AxisList[curve.YAxisIndex] : (Axis)p.YAxisList[curve.YAxisIndex];
    ZedGraph.PointPair point = new PointPair(0.0, 0.0);
    if (first)
    {
        p.GraphObjList.Remove(cursorOldY);
        p.GraphObjList.Remove(cursorOldX);
        if (fromKeyboard)
           point = curve.Points[index1];
        else if (lineGraphControl1.Graph.XAxis.Mode == XAxisMode.Parameter)
        {
            ZedGraph.CurveItem outputCurve;
            p.FindNearestPoint(mousePoint, curve, out outputCurve, out index1);
            if (index1 &gt;= 0)
               point = curve.Points[index1];
        }
        else
        {
           if (!mousePoint.IsEmpty)
               p.ReverseTransform(mousePoint, out cursorX, out cursorY);
           point = findPoint(curve, cursorX);
        }
        cursorX = point.X;
        cursorY = transform(point.Y, axis);
        cursorOldY = new LineObj(cursorX, p.YAxis.Scale.Min, cursorX, p.YAxis.Scale.Max);
        cursorOldX = new LineObj(p.XAxis.Scale.Max, cursorY, p.XAxis.Scale.Min, cursorY);

       if (cursorX &gt; p.XAxis.Scale.Min &amp;&amp; cursorX &lt; p.XAxis.Scale.Max)
           p.GraphObjList.Add(cursorOldY);

       if (cursorY &gt; p.YAxis.Scale.Min &amp;&amp; cursorY &lt; p.YAxis.Scale.Max)
           p.GraphObjList.Add(cursorOldX);
   }
   if (second)
   {
       p.GraphObjList.Remove(cursor2OldY);
       p.GraphObjList.Remove(cursor2OldX);
       if (fromKeyboard)
           point = curve.Points[index2];
       else if (lineGraphControl1.Graph.XAxis.Mode == XAxisMode.Parameter)
       {
           ZedGraph.CurveItem outputCurve;
           p.FindNearestPoint(mousePoint, curve, out outputCurve, out index2);
           if (index2 &gt;= 0)
               point = curve.Points[index2];
       }
       else
       {
           if (!mousePoint.IsEmpty)
               p.ReverseTransform(mousePoint, out cursor2X, out cursor2Y);
           point = findPoint(curve, cursor2X);
           if (index2 == -1)
           {
               index2 = (int) Math.Round(curve.NPts * 0.75, 0);
               point = curve.Points[index2];
           }
       }

       cursor2X = point.X;
       cursor2Y = transform(point.Y, axis);
       cursor2OldY = new LineObj(cursor2X, p.YAxis.Scale.Min, cursor2X, p.YAxis.Scale.Max);
       cursor2OldX = new LineObj(p.XAxis.Scale.Max, cursor2Y, p.XAxis.Scale.Min, cursor2Y);

       if (cursor2X &gt; p.XAxis.Scale.Min &amp;&amp; cursor2X &lt; p.XAxis.Scale.Max)
           p.GraphObjList.Add(cursor2OldY);
       if (cursor2Y &gt; p.YAxis.Scale.Min &amp;&amp; cursor2Y &lt; p.YAxis.Scale.Max)
           p.GraphObjList.Add(cursor2OldX);
    }
}


Is (structurally speaking) there some optimizations I missed ?
Mis-Matching curly braces are not a good thing, you do it all over the place, it makes your code hard to read


   if (fromKeyboard)
       point = curve.Points[index1];
    else if (lineGraphControl1.Graph.XAxis.Mode == XAxisMode.Parameter)
    {
        ZedGraph.CurveItem outputCurve;
        p.FindNearestPoint(mousePoint, curve, out outputCurve, out index1);
        if (index1 &gt;= 0)
           point = curve.Points[index1];
    }
    else
    {



it should look like this

   if (fromKeyboard)
   {
       point = curve.Points[index1];
   }
   else if (lineGraphControl1.Graph.XAxis.Mode == XAxisMode.Parameter)
   {
        ZedGraph.CurveItem outputCurve;
        p.FindNearestPoint(mousePoint, curve, out outputCurve, out index1);
        if (index1 &gt;= 0)
           point = curve.Points[index1];
   }
   else
   {


Indentation only on an if statement all by itself is fine, I personally don't like it, but when an else statement is added to the if statement, it starts to muddy the waters around your if statement and make it look sloppy.



The single, one-line if statements should also have a buffer zone after them as well.