Results 1 to 6 of 6

Thread: VB Studio 2005

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    10

    VB Studio 2005

    I am trying to create a function to return a color based on a value and then draw a line that color. I need a specific color gradient for each value I send between 0 and 1500. No errors but the color never shows and the line never shows. If I force the line color to black, that works.

    Code:
    Public Class Form1
        Public mypen As Pen
        Public rcol As Integer = 100
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Function getrgb(ByVal cc As Integer) As Integer
            Dim rt As Integer
            If cc <= 255 Then
                rt = RGB(255, cc, 0)
            ElseIf cc < 511 Then
                rt = RGB(255 - (cc - 255), 255, 0)
            ElseIf cc < 766 Then
                rt = RGB(0, 255, cc - 511)
            ElseIf cc < 1022 Then
                rt = RGB(0, 255 - (cc - 767), 255)
            ElseIf cc < 1277 Then
                rt = RGB(0, 0, 255 - (cc - 1022))
            Else
                rt = RGB(0, 0, 0)
            End If
            getrgb = rt
        End Function
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            Dim rr As Integer = getrgb(rcol)
            mypen = New Pen(Color.FromArgb(rr))
            e.Graphics.DrawLine(mypen, 0, 0, Me.Width, Me.Height) 'doesn't work
            'e.Graphics.DrawLine(Pens.Black, 0, 0, Me.Width, Me.Height) 'works
            mypen.Dispose()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Me.Refresh()
            rcol += 1
            If rcol > 1500 Then
                rcol = 0
            End If
        End Sub
    End Class

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: VB Studio 2005

    seems over complicated...

    I'd change the function to return a color rather than an integer, then use that directly with the pen constructor:
    vb.net Code:
    1. Function GetColor(ByVal cc As Integer) As System.
    2.         Dim rt As Integer
    3.         If cc <= 255 Then
    4.             rt = RGB(255, cc, 0)
    5.         ElseIf cc < 511 Then
    6.             rt = RGB(255 - (cc - 255), 255, 0)
    7.         ElseIf cc < 766 Then
    8.             rt = RGB(0, 255, cc - 511)
    9.         ElseIf cc < 1022 Then
    10.             rt = RGB(0, 255 - (cc - 767), 255)
    11.         ElseIf cc < 1277 Then
    12.             rt = RGB(0, 0, 255 - (cc - 1022))
    13.         Else
    14.             rt = RGB(0, 0, 0)
    15.         End If
    16.         getrgb = rt
    17.     End Function
    Then
    vb.net Code:
    1. Using myPen As New Pen(GetColor(rr))
    2.             e.Graphics.DrawLine(myPen, 0, 0, Me.Width, Me.Height) 'doesn't work
    3.             'e.Graphics.DrawLine(Pens.Black, 0, 0, Me.Width, Me.Height) 'works
    4.         End Using

    BTW - there's no need for MyPen to be declared where it is... it's only going to be used in the Paint event, and you're disposing it each time. That's why I used the Using statement... it'll create the pen, then auto dispose it at the end.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: VB Studio 2005

    using LinearGradientBrush to handle gradients.
    Code:
    Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            
            Dim color As New Color
            Dim m_Rnd As New Random
            color = color.FromArgb(255, _
                m_Rnd.Next(0, 255), _
                m_Rnd.Next(0, 255), _
                m_Rnd.Next(0, 255))
    
    
            Using linGrBrush As New LinearGradientBrush(New Point(0, 10), New Point(200, 10), color.FromArgb(Getrgb(rcol)), color)
                Using mypen As New Pen(linGrBrush, 2)
                    e.Graphics.DrawLine(mypen, 0, 0, Me.Width, Me.Height)
                End Using
            End Using
        End Sub
    KGC
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    10

    Re: VB Studio 2005

    Thanks for the examples...

    techgnome example still does not draw a line in any color.

    KGComputers example makes a nice line with a gradient along the distance but I need the routine to return 1 color of the rainbow and draw the line that color.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,713

    Re: VB Studio 2005

    In KGComputer's example, it's up to you as the programmer to make the color variable a similar color to the Getrgb(rcol) variable if you want the gradient line to be just one color variation.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Posts
    10

    Re: VB Studio 2005

    DOH!

    The alpha component was set to 0.

    Now it works.

    Code:
    Public Class Form1
    
        Public rcol As Integer = 700
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Function getrgb(ByVal cc As Integer) As System.Drawing.Color
            Dim returnValue As Color
            Dim rt As Integer
            If cc <= 255 Then
                rt = RGB(255, cc, 0)
                returnValue = Color.FromArgb(255, 255, cc, 0)
            ElseIf cc < 511 Then
                rt = RGB(255 - (cc - 255), 255, 0)
                returnValue = Color.FromArgb(255, 255 - (cc - 255), 255, 0)
            ElseIf cc < 767 Then
                rt = RGB(0, 255, cc - 511)
                returnValue = Color.FromArgb(255, 0, 255, cc - 511)
            ElseIf cc < 1022 Then
                rt = RGB(0, 255 - (cc - 767), 255)
                returnValue = Color.FromArgb(255, 0, 255 - (cc - 767), 255)
            ElseIf cc < 1277 Then
                rt = RGB(0, 0, 255 - (cc - 1022))
                returnValue = Color.FromArgb(255, 0, 0, 255 - (cc - 1022))
            Else
                rt = RGB(0, 0, 0)
                returnValue = Color.FromArgb(255, 0, 0, 0)
            End If
    
            getrgb = returnValue
    
        End Function
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim gc As Color = getrgb(rcol)
            Using mypen As New Pen(gc, 2)
                e.Graphics.DrawLine(mypen, 0, 0, Me.Width, Me.Height)
            End Using
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Me.Refresh()
            rcol += 1
            If rcol > 1200 Then
                rcol = 0
            End If
        End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width