고수민
고수민

Reputation: 33

Cant draw line in loop

In mainForm (Form1), I'm calling another form's method. and there it calls onPaint method by using this.Refresh(). and in onPaint method, there is a for loop that draws line. But DrawLine is not working

here's the code.

private void onPaintobject sender, PaintEventArgs e)
    {
        try
        {
            DrawXYBse(e.Graphics);

            if(logModels != null)
                SetDots(e.Graphics); 
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
        }

    }

    public void SetDots(Graphics g)
    {
        g.DrawLine(Pens.Orange, 80f, 80f, 80.1f, 80.1f);
        foreach (LogModel logModel in logModels)
        {
            g.DrawLine(Pens.Orange, logModel.DX + 80f, logModel.DY + 80f, logModel.DX + 80.1f, logModel.DY + 80.1f);
        }
    }

    public void CallOnPaint(List<LogModel> logModels)
    {
        this.logModels = logModels;
        //call onPaint()
        this.Refresh();
    }

Upvotes: 0

Views: 366

Answers (1)

Access Denied
Access Denied

Reputation: 9461

I've looked at your code and assume that lines are invisible because they are too small 0.1 pixel change in dimension. (g.DrawLine(Pens.Orange, logModel.DX + 80f, logModel.DY + 80f, logModel.DX + 80.1f, logModel.DY + 80.1f)) I changed it to 10 and they are now on the form. (Maybe you forgot to subscribe to Paint event or maybe do not call CallOnPaint):

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Paint += onPaint;
    }

    List<LogModel> logModels;

    private void onPaint(object sender, PaintEventArgs e)
    {
        try
        {
            DrawXYBse(e.Graphics);

            if (logModels != null)
                SetDots(e.Graphics);
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
        }

    }

    private void DrawXYBse(Graphics g)
    { }

    public void SetDots(Graphics g)
    {
        g.DrawLine(Pens.Orange, 80f, 80f, 80.1f, 80.1f);
        foreach (LogModel logModel in logModels)
        {
            g.DrawLine(Pens.Orange, logModel.DX + 80f, logModel.DY + 80f, logModel.DX + 90.1f, logModel.DY + 90.1f);
        }
    }

    public void CallOnPaint(List<LogModel> logModels)
    {
        this.logModels = logModels;
        //call onPaint()
        this.Refresh();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        CallOnPaint(new List<LogModel>()
        {
            new LogModel(){DX = 20, DY =20},
            new LogModel(){DX = 40, DY =30},
            new LogModel(){DX = 60, DY =40},
            new LogModel(){DX = 80, DY =50},
            new LogModel(){DX = 90, DY =60},
            new LogModel(){DX = 100, DY =70},
        });
    }
}
public class LogModel
{
    public float DX { get; set; }
    public float DY { get; set; }

}

Upvotes: 1

Related Questions