Click here to Skip to main content
15,888,195 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am using the following code to build a dual temperature scale in Visual Basic.
I have built the picture box in a container with a button in a separate cell and when I debug the button does not appear until you physically move the form. If I remove the button its fine.
If I remove the code at the bottom pictherm1.Image = bmp1 its fine but the scale does not build.

If I build the form without containers its also fine and builds correctly with the button.

Please can anyone help

Thank you in advance

Nevjc


VB
Imports System.Drawing.Drawing2D
Imports System.IO.Ports

Public Class Form1
    Dim bmp1 As Bitmap = New Bitmap(165, 550)
    Dim temperature1 As Double = 10
    Dim temperature2 As Double = 10
    Dim WithEvents sp As New SerialPort


    Private Sub pictherm1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pictherm1.Paint
        Dim g As Graphics = Graphics.FromImage(bmp1)
        g.Clear(Color.White)
        ' fill temperature1
        If temperature1 > 100 Then temperature1 = 100
        g.FillRectangle(Brushes.Green, 48, 525 - CInt(temperature1 * 5), 10, CInt(temperature1 * 5))
        ' fill temperature2
        If temperature2 > 100 Then temperature2 = 100
        g.FillRectangle(Brushes.Green, 108, 525 - CInt(temperature2 * 5), 10, CInt(temperature2 * 5))
        'draw scale
        g.DrawLine(Pens.Black, 60, 525, 60, 25)
        g.DrawLine(Pens.Black, 105, 525, 105, 25)
        ' minor ticks
        For i As Integer = 25 To 525 Step 5
            g.DrawLine(Pens.Black, 50, i, 60, i)
            g.DrawLine(Pens.Black, 105, i, 115, i)
        Next
        'major ticks
        Dim f As Font = New Font("Verdana", 10, FontStyle.Regular)
        Dim scale As Integer
        For i As Integer = 525 To 25 Step -25
            g.DrawLine(Pens.Black, 45, i, 60, i)
            g.DrawLine(Pens.Black, 105, i, 120, i)
            scale = (525 - i) / 5
            g.DrawString(Str(scale), f, Brushes.Black, 65, i - 8)
        Next
        pictherm1.Image = bmp1
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub
End Class
Posted
Updated 9-Apr-14 3:08am
v2

1 solution

VB
Dim g As Graphics = Graphics.FromImage(bmp1)

Why? The correct Graphics object is part of
VB
ByVal e As System.Windows.Forms.PaintEventArgs

I.e.
VB
Dim g As Graphics = e.Graphics

And you don't need the pictherm1.Image = bmp1 line.
 
Share this answer
 
Comments
Member 10701701 9-Apr-14 9:14am    
Thank you Bernhard that has sorted it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900