none
GDI Integer Problem RRS feed

  • Frage

  • Hallo zusammen, ich programmiere ein Anwendung um ein Geschwindikeits-Zeit Schaubild zu erstellen

      Private SollZeichnen As Boolean
    
      Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
    
        If SollZeichnen Then
    
          Dim y1 As Integer = 330
          Dim x1 As Integer = +20
          Dim y2 As Integer = 330 - CInt(TextBox4_v.Text) - 20
          Dim x2 As Integer = CInt(TextBox3_t.Text) + 20
          Dim y3 As Integer = y2 + CInt(TextBox4_v.Text) + 20
          Dim x3 As Integer = +20
          Dim x4 As Integer = +20
          Dim y4 As Integer = 350 - CInt(TextBox4_v.Text) - 20
    
          'y2 = Convert.ToInt32(TextBox2_v.Text)
          'x2 = Convert.ToInt32(TextBox3_t.Tex
    
          If RadioButton2_drei.Checked Then
    
            x3 = CInt(x2 / 2) + 20
            x4 = (x3 + (x2 / 2))
            'y=mx+b
            'm=(y1-y2) / (x1-x2)
            'b=y1-m*x1
            Dim m As Double = (y1 - y2) / (x1 - x3)
            Dim b As Double = y2 - (m * x3)
    
            x1 = +20
            'y1 = m * x1 + b
            y2 = m * x3 + b
            'x2 = 640 - 10
            'y3 = X3 + b
          End If
    
    
          If RadioButton2_drei.Checked = False Then
    
            Dim m As Double = (y1 - y2) / (x1 - x2)
            Dim b As Double = y2 - (m * x2)
            'x2 = +20
            x1 = +20
            y4 = m * x2 + b
          End If
    
    
    
          Dim g As Graphics
          Dim p As Pen
    
          p = New Pen(System.Drawing.Color.Black, 4)
          g = e.Graphics
    
          With g
    
            .DrawLine(p, 0, 330, 640, 330) 'x
            .DrawLine(p, 20, 340, 20, 0) 'y
    
            Dim v1 As Integer = 0
            Dim xa As Integer = 350 / 25
            'Dim z2 As Integer = 335 - 325
            Dim z1 As Integer = 0
            Dim yb As Integer = xa * 128
    
            For z1 = 20 To yb Step 5
    
              .DrawLine(New Pen(System.Drawing.Color.Gray, 1), z1, 0, z1, 335)
    
              v1 += 1
    
            Next
    
            Dim ya As Integer = 640 / 20
            Dim z2 As Integer = 0
            Dim x8 As Integer = ya * 10.5
    
            For z2 = 20 To x8 Step 5
              .DrawLine(New Pen(System.Drawing.Color.Gray, 1), 0, z2, Me.Height, z2)
            Next
    
            Dim strText As String = "1"
            Dim strText2 As String = "2"
            Dim strText3 As String = "3"
            Dim strText4 As String = "4"
            Dim strText5 As String = "5"
    
            Dim ft As New Font("Arial", 12, FontStyle.Bold) 'Or FontStyle.Underline)
            .DrawString(strText, ft, Brushes.Black, New Point(30, 335))
            .DrawString(strText2, ft, Brushes.Black, New Point(50, 335))
            .DrawString(strText3, ft, Brushes.Black, New Point(70, 335))
            '.DrawString(strText4, ft, Brushes.Black, New Point(120, 335))
            '.DrawString(strText5, ft, Brushes.Black, New Point(100, 335))
    
    
            If RadioButton2_drei.Checked Then
              .DrawLine(New Pen(System.Drawing.Color.Red, 2), x1, y1, x3, y2)
              .DrawLine(New Pen(System.Drawing.Color.Red, 2), x3, y2, x4, y3)
            End If
    
            If RadioButton2_drei.Checked = False Then
              .DrawLine(New Pen(System.Drawing.Color.Red, 2), x1, y1, x2, y4)
            End If
    
          End With
    
        End If
    
      End Sub

     

    Das Problem ist jetzt wenn ich in CInt(TextBox3_t.Text) den Wert 1 ein trage, rechnet er ja auf 0,5 und das geht ja bei Integer nicht.

    Frage wie und wo muss ich eine Umkonvertierung einfügen. Danke für die Hilfe!

    Mittwoch, 26. Mai 2010 12:37

Antworten

  • Hi Kay,
    das sieht ja schlimm aus. Ich habe aus WLM über die NNTP-Brigde, Version 2 von Codeplex.com gepostet, da ich mit der alten NNTP-Bridge keine Verbindung aufbauen konnte (ich habe hier eine sehr langsame und störanfällige Verbindung). Gibt es ein Forum für Test-Postings?
     
    Hier nochmals mein Beitrag jetzt über die alte NNTP-Bridge:
     
    Ich würde das Problem so lösen (vor allem Dispose/Using nutzen, um den Speicher nicht anwachsen zu lassen):
     
    Public Class Form1
     
     Private TextBox3_t As New TextBox With {.Dock = DockStyle.Top, .Text = "1"}
     Private TextBox4_v As New TextBox With {.Dock = DockStyle.Top, ..Text = "20"}
     Private RadioButton2_drei As New CheckBox With {.Dock = DockStyle.Top, .Text = "RadioButton2_drei"}
     Private PictureBox1 As New PictureBox With {.Dock = DockStyle.Fill}
     Private WithEvents btn As New Button With {.Dock = DockStyle.Top, .Text = "Neuzeichnen"}
     
     Private Sub Form1_Load(ByVal sender As System.Object, _
           ByVal e As System.EventArgs) Handles MyBase.Load
     Me.Controls.AddRange(New Control() {btn, TextBox4_v, TextBox3_t, RadioButton2_drei, PictureBox1})
     AddHandler PictureBox1.Paint, AddressOf PictureBox1_Paint
     End Sub
     
     Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
     PictureBox1.Refresh()
     End Sub
     
     Private SollZeichnen As Boolean = True
     
     Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
     Dim pt1, pt2, pt3 As Point
     
     If SollZeichnen Then
      Dim v As Integer = 1
      Integer.TryParse(TextBox4_v.Text, v)
      Dim t As Integer = 1
      Integer.TryParse(TextBox3_t.Text, t)
     
      Dim y1 As Integer = 330
      Dim x1 As Integer = +20
      Dim y2 As Integer = 330 - v - 20
      Dim x2 As Integer = t + 20
      Dim y3 As Integer = y2 + v + 20
      Dim x3 As Integer = +20
      Dim x4 As Integer = +20
      Dim y4 As Integer = 350 - v - 20
     
      If RadioButton2_drei.Checked Then
      x3 = (x2 \ 2) + 20
      x4 = x3 + (x2 \ 2)
      Dim m = (y1 - y2) / (x1 - x3)
      Dim b = y2 - (m * x3)
      x1 = +20
      y2 = CInt(m * x3 + b)
      Else
      Dim m = (y1 - y2) / (x1 - x2)
      Dim b = y2 - (m * x2)
      x1 = +20
      y4 = CInt(m * x2 + b)
      End If
      '
      Dim g As Graphics = e.Graphics
      With g
      Using p As New Pen(System.Drawing.Color.Black, 4)
       .DrawLine(p, 0, 330, 640, 330) 'x
       .DrawLine(p, 20, 340, 20, 0) 'y
      End Using
      Using p As New Pen(System.Drawing.Color.Black, 0)
       ' vertikal
       .ResetTransform()
       pt1 = New Point(25, 0)
       pt2 = New Point(25, 335)
       For z1 = 1 To 124
       .DrawLine(p, pt1, pt2)
       .TranslateTransform(5, 0)
       Next
       ' horizontal
       .ResetTransform()
       pt1 = New Point(15, 5)
       pt2 = New Point(640, 5)
       For z1 = 1 To 65
       .DrawLine(p, pt1, pt2)
       .TranslateTransform(0, 5)
       Next
      End Using
      '
      .ResetTransform()
      Dim ft As New Font("Arial", 12, FontStyle.Bold) 'Or FontStyle.Underline)
      For i = 1 To 30
       .DrawString(i.ToString, ft, Brushes.Black, New Point(30, 335))
      .TranslateTransform(20, 0)
      Next
      '
      .ResetTransform()
      Using p As New Pen(System.Drawing.Color.Red, 2)
       pt1 = New Point(20, 330)
       If RadioButton2_drei.Checked Then
       pt2 = New Point(x3, y2)
       pt3 = New Point(x4, y3)
       .DrawLine(p, pt1, pt2)
       .DrawLine(p, pt2, pt3)
       Else
       pt2 = New Point(x2, y4)
       .DrawLine(p, pt1, pt2)
       End If
      End Using
      End With
     End If
     End Sub
     
    End Class

     

    С  уважением

    Петер

     

    aus Moskau.

    Donnerstag, 27. Mai 2010 09:59
  • Hallo,

    die GDI+ Funktionen können auch mit Single umgehen.
    Du solltest die Integer Variablen als Single definieren, die Du für die Rechnenabschnitt benötigst;
    wo Du einen Point verwendest PointF , für Rectangle RectangleF .

    Den Abschnitt für die Textausgabe kannst Du auf Integer belassen,
    da dort nicht gerechnet wird.

    Konvertieren kannst Du via CSng, CType(..., Single), siehe
    Implizite und explizite Konvertierungen

    Gruß Elmar

    Mittwoch, 26. Mai 2010 13:17

Alle Antworten

  • Hallo,

    die GDI+ Funktionen können auch mit Single umgehen.
    Du solltest die Integer Variablen als Single definieren, die Du für die Rechnenabschnitt benötigst;
    wo Du einen Point verwendest PointF , für Rectangle RectangleF .

    Den Abschnitt für die Textausgabe kannst Du auf Integer belassen,
    da dort nicht gerechnet wird.

    Konvertieren kannst Du via CSng, CType(..., Single), siehe
    Implizite und explizite Konvertierungen

    Gruß Elmar

    Mittwoch, 26. Mai 2010 13:17
  • Ich würde das Problem so lösen (vor allem Dispose/Using nutzen, um den Speicher nicht anwachsen zu lassen): Public Class Form1 Private TextBox3_t As New TextBox With {.Dock = DockStyle.Top, .Text = "1"} Private TextBox4_v As New TextBox With {.Dock = DockStyle.Top, .Text = "20"} Private RadioButton2_drei As New CheckBox With {.Dock = DockStyle.Top, .Text = "RadioButton2_drei"} Private PictureBox1 As New PictureBox With {.Dock = DockStyle.Fill} Private WithEvents btn As New Button With {.Dock = DockStyle.Top, ..Text = "Neuzeichnen"} Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Me.Controls.AddRange(New Control() {btn, TextBox4_v, TextBox3_t, RadioButton2_drei, PictureBox1}) AddHandler PictureBox1.Paint, AddressOf PictureBox1_Paint End Sub Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click PictureBox1.Refresh() End Sub Private SollZeichnen As Boolean = True Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Dim pt1, pt2, pt3 As Point If SollZeichnen Then Dim v As Integer = 1 Integer.TryParse(TextBox4_v.Text, v) Dim t As Integer = 1 Integer.TryParse(TextBox3_t.Text, t) Dim y1 As Integer = 330 Dim x1 As Integer = +20 Dim y2 As Integer = 330 - v - 20 Dim x2 As Integer = t + 20 Dim y3 As Integer = y2 + v + 20 Dim x3 As Integer = +20 Dim x4 As Integer = +20 Dim y4 As Integer = 350 - v - 20 If RadioButton2_drei.Checked Then x3 = (x2 \ 2) + 20 x4 = x3 + (x2 \ 2) Dim m = (y1 - y2) / (x1 - x3) Dim b = y2 - (m * x3) x1 = +20 y2 = CInt(m * x3 + b) Else Dim m = (y1 - y2) / (x1 - x2) Dim b = y2 - (m * x2) x1 = +20 y4 = CInt(m * x2 + b) End If ' Dim g As Graphics = e.Graphics With g Using p As New Pen(System.Drawing.Color.Black, 4) .DrawLine(p, 0, 330, 640, 330) 'x .DrawLine(p, 20, 340, 20, 0) 'y End Using Using p As New Pen(System.Drawing.Color.Black, 0) ' vertikal .ResetTransform() pt1 = New Point(25, 0) pt2 = New Point(25, 335) For z1 = 1 To 124 .DrawLine(p, pt1, pt2) .TranslateTransform(5, 0) Next ' horizontal .ResetTransform() pt1 = New Point(15, 5) pt2 = New Point(640, 5) For z1 = 1 To 65 .DrawLine(p, pt1, pt2) .TranslateTransform(0, 5) Next End Using ' .ResetTransform() Dim ft As New Font("Arial", 12, FontStyle.Bold) 'Or FontStyle.Underline) For i = 1 To 30 .DrawString(i.ToString, ft, Brushes.Black, New Point(30, 335)) .TranslateTransform(20, 0) Next ' .ResetTransform() Using p As New Pen(System.Drawing.Color.Red, 2) pt1 = New Point(20, 330) If RadioButton2_drei.Checked Then pt2 = New Point(x3, y2) pt3 = New Point(x4, y3) .DrawLine(p, pt1, pt2) .DrawLine(p, pt2, pt3) Else pt2 = New Point(x2, y4) .DrawLine(p, pt1, pt2) End If End Using End With End If End Sub End Class -- С уважением Петер
    Donnerstag, 27. Mai 2010 03:51
  • Hallo Peter,
    ist da etwas durcheinander geraten auf Deiner Seite oder ist hier etwas am Forum falsch?
    Wenn letzteres, dann bitte nur kurz sagen, was Du gemacht hast, dann kümmern wir uns sofort.
    Danke im Voraus!
    Viele Grüße,
    Kay

    Ich würde das Problem so lösen (vor allem Dispose/Using nutzen, um den Speicher nicht anwachsen zu lassen): Public Class Form1 Private TextBox3_t As New TextBox With {.Dock = DockStyle.Top, .Text = "1"} Private TextBox4_v As New TextBox With {.Dock = DockStyle.Top, .Text = "20"} Private RadioButton2_drei As New CheckBox With {.Dock = DockStyle.Top, .Text = "RadioButton2_drei"} Private PictureBox1 As New PictureBox With {.Dock = DockStyle.Fill} Private WithEvents btn As New Button With {.Dock = DockStyle.Top, ..Text = "Neuzeichnen"} Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Me.Controls.AddRange(New Control() {btn, TextBox4_v, TextBox3_t, RadioButton2_drei, PictureBox1}) AddHandler PictureBox1.Paint, AddressOf PictureBox1_Paint End Sub Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click PictureBox1.Refresh() End Sub Private SollZeichnen As Boolean = True Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Dim pt1, pt2, pt3 As Point If SollZeichnen Then Dim v As Integer = 1 Integer.TryParse(TextBox4_v.Text, v) Dim t As Integer = 1 Integer.TryParse(TextBox3_t.Text, t) Dim y1 As Integer = 330 Dim x1 As Integer = +20 Dim y2 As Integer = 330 - v - 20 Dim x2 As Integer = t + 20 Dim y3 As Integer = y2 + v + 20 Dim x3 As Integer = +20 Dim x4 As Integer = +20 Dim y4 As Integer = 350 - v - 20 If RadioButton2_drei.Checked Then x3 = (x2 \ 2) + 20 x4 = x3 + (x2 \ 2) Dim m = (y1 - y2) / (x1 - x3) Dim b = y2 - (m * x3) x1 = +20 y2 = CInt(m * x3 + b) Else Dim m = (y1 - y2) / (x1 - x2) Dim b = y2 - (m * x2) x1 = +20 y4 = CInt(m * x2 + b) End If ' Dim g As Graphics = e.Graphics With g Using p As New Pen(System.Drawing.Color.Black, 4) .DrawLine(p, 0, 330, 640, 330) 'x .DrawLine(p, 20, 340, 20, 0) 'y End Using Using p As New Pen(System.Drawing.Color.Black, 0) ' vertikal .ResetTransform() pt1 = New Point(25, 0) pt2 = New Point(25, 335) For z1 = 1 To 124 .DrawLine(p, pt1, pt2) .TranslateTransform(5, 0) Next ' horizontal .ResetTransform() pt1 = New Point(15, 5) pt2 = New Point(640, 5) For z1 = 1 To 65 .DrawLine(p, pt1, pt2) .TranslateTransform(0, 5) Next End Using ' .ResetTransform() Dim ft As New Font("Arial", 12, FontStyle.Bold) 'Or FontStyle.Underline) For i = 1 To 30 .DrawString(i.ToString, ft, Brushes.Black, New Point(30, 335)) .TranslateTransform(20, 0) Next ' .ResetTransform() Using p As New Pen(System.Drawing.Color.Red, 2) pt1 = New Point(20, 330) If RadioButton2_drei.Checked Then pt2 = New Point(x3, y2) pt3 = New Point(x4, y3) .DrawLine(p, pt1, pt2) .DrawLine(p, pt2, pt3) Else pt2 = New Point(x2, y4) .DrawLine(p, pt1, pt2) End If End Using End With End If End Sub End Class -- С уважением Петер

     


    This post is powered by www.Giza-Blog.de | Sie besuchen MSDN heute zum ersten Mal?
    MSDN Online | Follow MSDN Online on Twitter | Follow Kay Giza on Twitter
    Tagesaktuelle News & Infos für Entwickler finden Sie auf MSDN Aktuell
    ~~ So melden Sie Forum-Probleme, -Bugs oder -Verbesserungsvorschläge ~~
    Donnerstag, 27. Mai 2010 09:12
  • Hi Kay,
    das sieht ja schlimm aus. Ich habe aus WLM über die NNTP-Brigde, Version 2 von Codeplex.com gepostet, da ich mit der alten NNTP-Bridge keine Verbindung aufbauen konnte (ich habe hier eine sehr langsame und störanfällige Verbindung). Gibt es ein Forum für Test-Postings?
     
    Hier nochmals mein Beitrag jetzt über die alte NNTP-Bridge:
     
    Ich würde das Problem so lösen (vor allem Dispose/Using nutzen, um den Speicher nicht anwachsen zu lassen):
     
    Public Class Form1
     
     Private TextBox3_t As New TextBox With {.Dock = DockStyle.Top, .Text = "1"}
     Private TextBox4_v As New TextBox With {.Dock = DockStyle.Top, ..Text = "20"}
     Private RadioButton2_drei As New CheckBox With {.Dock = DockStyle.Top, .Text = "RadioButton2_drei"}
     Private PictureBox1 As New PictureBox With {.Dock = DockStyle.Fill}
     Private WithEvents btn As New Button With {.Dock = DockStyle.Top, .Text = "Neuzeichnen"}
     
     Private Sub Form1_Load(ByVal sender As System.Object, _
           ByVal e As System.EventArgs) Handles MyBase.Load
     Me.Controls.AddRange(New Control() {btn, TextBox4_v, TextBox3_t, RadioButton2_drei, PictureBox1})
     AddHandler PictureBox1.Paint, AddressOf PictureBox1_Paint
     End Sub
     
     Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
     PictureBox1.Refresh()
     End Sub
     
     Private SollZeichnen As Boolean = True
     
     Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
     Dim pt1, pt2, pt3 As Point
     
     If SollZeichnen Then
      Dim v As Integer = 1
      Integer.TryParse(TextBox4_v.Text, v)
      Dim t As Integer = 1
      Integer.TryParse(TextBox3_t.Text, t)
     
      Dim y1 As Integer = 330
      Dim x1 As Integer = +20
      Dim y2 As Integer = 330 - v - 20
      Dim x2 As Integer = t + 20
      Dim y3 As Integer = y2 + v + 20
      Dim x3 As Integer = +20
      Dim x4 As Integer = +20
      Dim y4 As Integer = 350 - v - 20
     
      If RadioButton2_drei.Checked Then
      x3 = (x2 \ 2) + 20
      x4 = x3 + (x2 \ 2)
      Dim m = (y1 - y2) / (x1 - x3)
      Dim b = y2 - (m * x3)
      x1 = +20
      y2 = CInt(m * x3 + b)
      Else
      Dim m = (y1 - y2) / (x1 - x2)
      Dim b = y2 - (m * x2)
      x1 = +20
      y4 = CInt(m * x2 + b)
      End If
      '
      Dim g As Graphics = e.Graphics
      With g
      Using p As New Pen(System.Drawing.Color.Black, 4)
       .DrawLine(p, 0, 330, 640, 330) 'x
       .DrawLine(p, 20, 340, 20, 0) 'y
      End Using
      Using p As New Pen(System.Drawing.Color.Black, 0)
       ' vertikal
       .ResetTransform()
       pt1 = New Point(25, 0)
       pt2 = New Point(25, 335)
       For z1 = 1 To 124
       .DrawLine(p, pt1, pt2)
       .TranslateTransform(5, 0)
       Next
       ' horizontal
       .ResetTransform()
       pt1 = New Point(15, 5)
       pt2 = New Point(640, 5)
       For z1 = 1 To 65
       .DrawLine(p, pt1, pt2)
       .TranslateTransform(0, 5)
       Next
      End Using
      '
      .ResetTransform()
      Dim ft As New Font("Arial", 12, FontStyle.Bold) 'Or FontStyle.Underline)
      For i = 1 To 30
       .DrawString(i.ToString, ft, Brushes.Black, New Point(30, 335))
      .TranslateTransform(20, 0)
      Next
      '
      .ResetTransform()
      Using p As New Pen(System.Drawing.Color.Red, 2)
       pt1 = New Point(20, 330)
       If RadioButton2_drei.Checked Then
       pt2 = New Point(x3, y2)
       pt3 = New Point(x4, y3)
       .DrawLine(p, pt1, pt2)
       .DrawLine(p, pt2, pt3)
       Else
       pt2 = New Point(x2, y4)
       .DrawLine(p, pt1, pt2)
       End If
      End Using
      End With
     End If
     End Sub
     
    End Class

     

    С  уважением

    Петер

     

    aus Moskau.

    Donnerstag, 27. Mai 2010 09:59
  • Danke Dir!
    Robert und ich werden dies entsprechend melden und eskaliseren.
    Viele Grüße vom Kay
    This post is powered by www.Giza-Blog.de | Sie besuchen MSDN heute zum ersten Mal?
    MSDN Online | Follow MSDN Online on Twitter | Follow Kay Giza on Twitter
    Tagesaktuelle News & Infos für Entwickler finden Sie auf MSDN Aktuell
    ~~ So melden Sie Forum-Probleme, -Bugs oder -Verbesserungsvorschläge ~~
    Donnerstag, 27. Mai 2010 12:12
  • Hallo Bergfeuer,

    Haben Dir die Antworten geholfen?

    Grüße,

    Robert

    Montag, 31. Mai 2010 06:06