使用无效和更新时图像闪烁

时间:2019-01-21 22:58:39

标签: vb.net winforms

我正在学习VB.net。我真的还不知道很多事情。
我到处都在搜索,找不到适合我问题的解决方案。

我的程序中有两个表格。
在第一个中,我要求用户输入数据,并且还要进行一些计算。
然后将这些结果传递给第二种形式,在其中我画线。

This is the initial form

This is the second form. This one displays the results

根据用户在第一个表格中引入的数据,我希望在第二个表格中看到最终的更改;这样可以将数据从第一个发送到第二个...
图形的问题在于它保留了第一个初始值,并且不会再次更改(我不喜欢这样)

Initial values, the graphics are doing ok

I am changing the values, but the graphics won't reset

我搜索了许多不同的帖子,然后放入了invalidate()和update(),第二种形式上的图形终于更新了。问题是有很多闪烁: Video of the program flickering

这是有问题的表单...尽管...我不知道是否可以通过要求Form1在第二个表单中进行绘制来解决此问题。

Public Class Form1

    Dim Diam_Barr As Double
    Dim Dist_Prim As Double
    Dim Dist_Seg As Double
    Dim Sag_Rin As Double
    Dim Sag_Cara As Double
    Dim Rin_3 As Double
    Dim Rin_9 As Double
    Dim Rin_6 As Double
    Dim Cara_3 As Double
    Dim Cara_6 As Double
    Dim Cara_9 As Double
    Dim NewCara_9 As Double
    Dim NewRin_9 As Double
    Dim Y0 As Double
    Dim Y1 As Double
    Dim Y2 As Double
    Dim Z0 As Double
    Dim Z1 As Double
    Dim Z2 As Double
    Dim m_Y As Double
    Dim m_Z As Double
    Dim SecondForm As New Form3

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Diam_Barr = DiamBarr.Text
        Dist_Prim = DistPrim.Text
        Dist_Seg = DistSeg.Text
        Sag_Rin = SagRin.Text
        Sag_Cara = SagCara.Text
        Cara_3 = Cara3.Text
        Cara_9 = Cara9.Text
        Rin_3 = Rin3.Text
        Rin_9 = Rin9.Text

        Cara_6 = Cara_3 + Cara_9
        Rin_6 = Rin_3 + Rin_9
        Cara6.Text = Cara_6
        Rin6.Text = Rin_6
        Y0 = (Rin_6 - Sag_Rin) / 2
        m_Y = (Cara_6 - Sag_Cara) / -Diam_Barr

        NewCara_9 = Cara_9 - Cara_3
        NewRin_9 = Rin_9 - Rin_3

        Z0 = NewRin_9 / 2
        m_Z = -NewCara_9 / Diam_Barr

        Y1 = (m_Y * Dist_Prim) + Y0
        Y2 = (m_Y * Dist_Seg) + Y0
        Z1 = (m_Z * Dist_Prim) + Z0
        Z2 = (m_Z * Dist_Seg) + Z0

        'Declarando la nueva forma y pasándole valores, también se redondean los mismos para que quepan en las cajitas de texto

        SecondForm.PendY.Text = Math.Round(m_Y, 2, MidpointRounding.AwayFromZero)
        SecondForm.PendZ.Text = Math.Round(m_Z, 2, MidpointRounding.AwayFromZero)

        SecondForm.PtY0.Text = Math.Round(Y0, 2, MidpointRounding.AwayFromZero)
        SecondForm.PtY1.Text = Math.Round(Y1, 2, MidpointRounding.AwayFromZero)
        SecondForm.PtY2.Text = Math.Round(Y2, 2, MidpointRounding.AwayFromZero)

        SecondForm.PtZ0.Text = Math.Round(Z0, 2, MidpointRounding.AwayFromZero)
        SecondForm.PtZ1.Text = Math.Round(Z1, 2, MidpointRounding.AwayFromZero)
        SecondForm.PtZ2.Text = Math.Round(Z2, 2, MidpointRounding.AwayFromZero)

        SecondForm.PtX2.Text = Math.Round(Dist_Seg, 2, MidpointRounding.AwayFromZero)

        SecondForm.Show()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        End
    End Sub
End Class

这是第二种形式:

Public Class Form3
    'Omitimos el evento de load y lo cambiamos por el evento de pintar
    'Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Private Sub Form3_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

        'Sacamos coordenadas para dibujar unas cajitas
        Dim PtA1 As New Point(245, 23)
        Dim PtA2 As New Point(673, 23)
        Dim PtA3 As New Point(673, 142)
        Dim PtA4 As New Point(245, 142)

        Dim PtB1 As New Point(245, 166)
        Dim PtB2 As New Point(673, 166)
        Dim PtB3 As New Point(673, 285)
        Dim PtB4 As New Point(245, 285)

        'puntos para dibujar las pendiente simuladas (para vertical)
        Dim PtCmax As New Point(250, 28)
        Dim PtCmin As New Point(250, 137)
        Dim PtCmed As New Point(250, 82.5)
        Dim PtDmax As New Point(673, 28)
        Dim PtDmin As New Point(673, 137)
        Dim PtDmed As New Point(673, 82.5)

        'puntos para dibujar las pendiente simuladas (para horizontal)
        Dim PtEmax As New Point(250, 171)
        Dim PtEmin As New Point(250, 280)
        Dim PtEmed As New Point(250, 225.5)
        Dim PtFmax As New Point(673, 171)
        Dim PtFmin As New Point(673, 280)
        Dim PtFmed As New Point(673, 225.5)

        'Dibujando los marcos
        e.Graphics.DrawLine(Pens.Gray, PtA1, PtA2)
        e.Graphics.DrawLine(Pens.Gray, PtA2, PtA3)
        e.Graphics.DrawLine(Pens.Gray, PtA3, PtA4)
        e.Graphics.DrawLine(Pens.Gray, PtA4, PtA1)

        e.Graphics.DrawLine(Pens.Gray, PtB1, PtB2)
        e.Graphics.DrawLine(Pens.Gray, PtB2, PtB3)
        e.Graphics.DrawLine(Pens.Gray, PtB3, PtB4)
        e.Graphics.DrawLine(Pens.Gray, PtB4, PtB1)

        Dim Pend_Y As Double
        Dim Pend_Z As Double

        Pend_Y = Convert.ToDouble(PendY.Text)
        Pend_Z = Convert.ToDouble(PendZ.Text)

        Invalidate()
        Update()

        If Pend_Y < 0 Then
            e.Graphics.DrawLine(Pens.Blue, PtCmax, PtDmin)
        ElseIf Pend_Y > 0 Then
            e.Graphics.DrawLine(Pens.Blue, PtCmin, PtDmax)
        ElseIf Pend_Y = 0 Then
            e.Graphics.DrawLine(Pens.Blue, PtCmed, PtDmed)
        End If

        If Pend_Z < 0 Then
            e.Graphics.DrawLine(Pens.Red, PtEmax, PtFmin)
        ElseIf Pend_Z > 0 Then
            e.Graphics.DrawLine(Pens.Red, PtEmin, PtFmax)
        ElseIf Pend_Z = 0 Then
            e.Graphics.DrawLine(Pens.Red, PtEmed, PtFmed)
        End If
    End Sub
End Class

0 个答案:

没有答案