Michael_9 0 Newbie Poster

Me being a master noob and making a triangles color change with a console.

link to program its shwelfClick Here

Public Class main
    Inherits Windows.Forms.Form
    Protected gfx As Drawing.Graphics
    Protected thePoints() As Drawing.Point
    Public Sub New()
        InitializeComponent()
        Console.WriteLine("Form seems to be functioning so far.")
    End Sub
    Private Sub InitializeComponent()
        Me.SuspendLayout()
        '
        'main
        '
        Me.ClientSize = New System.Drawing.Size(640, 400)
        Me.MaximizeBox = False
        Me.MaximumSize = New System.Drawing.Size(1024, 768)
        Me.MinimizeBox = False
        Me.MinimumSize = New System.Drawing.Size(300, 300)
        Me.Name = "main"
        Me.ResumeLayout(False)
        If initializeME() = True Then
            Console.WriteLine("More function done,but hmm smells like a noob.")
        End If
    End Sub
    Function initializeME() As Boolean
        Dim point1, point2, point3 As Drawing.Point
        point1 = New Drawing.Point(100, 0)
        point2 = New Drawing.Point(200, 0)
        point3 = New Drawing.Point(200, 100)
        thePoints = {point1, point2, point3}
        Return True
    End Function
    Private Sub paintme(sender As Object, e As Windows.Forms.PaintEventArgs) Handles Me.Paint

        gfx = e.Graphics

        gfx.Clear(Drawing.Color.CornflowerBlue)

        gfx.DrawPolygon(Drawing.Pens.Azure, thePoints)

        gfx.FillPolygon(polygoncolor(True), thePoints)

    End Sub

    Private Function polygoncolor(Optional change As Boolean = False) As Drawing.Brush

        If change = True Then
            Dim wutcolor As String
            ''Discarded variable
            ''Dim brush As Drawing.Brush
            ''now do a barrel roll
            Console.WriteLine("Choose, red, green or blue.")
            wutcolor = Console.ReadLine()

            If wutcolor = "red" Then
                Return New Drawing.SolidBrush(Drawing.Color.Red)
            ElseIf wutcolor = "blue" Then
                Return New Drawing.SolidBrush(Drawing.Color.Blue)
            ElseIf wutcolor = "green" Then
                Return New Drawing.SolidBrush(Drawing.Color.Green)
            End If

        End If

        Return Drawing.Brushes.Azure
    End Function
End Class



Imports LuaInterface
Module Module1
    Private lua As New Lua
    Private cmd As String
    Private opstring As String
    Const running As String = ""
    Public Sub runLine(value As Object)
        Console.WriteLine(value)
    End Sub

    Public Sub runReadLine(value As Object)
        value = Console.ReadLine()
    End Sub
    Private Function initialize() As Boolean
        Console.Title = "Digital Console"
        lua.RegisterFunction("writeL", Environment.CurrentManagedThreadId, GetType(Module1).GetMethod("runLine"))
        lua.RegisterFunction("readL", Environment.CurrentManagedThreadId, GetType(Module1).GetMethod("runReadLine"))
        '' lua("cmd") = cmd
        lua("opstring") = opstring

        Try
            lua.DoFile("scriptwrite.lua")
        Catch luaex As LuaException
            Throw New LuaException("File is corrupt,missing and or edited!")
        End Try
        Return True
    End Function
    Private Sub luaEX()
        Console.WriteLine("zomg something is wrong!")
    End Sub
    Sub Main()
        initialize()
        Dim form = New main

        Windows.Forms.Application.Run(form)




        While (running = "running")
            Windows.Forms.Application.DoEvents()
        End While
    End Sub

End Module

Dani AI

Generated

Nice experiment, . Mixing a console and a WinForms paint loop is a useful way to learn, but the combination has a few common pitfalls that make the app hang or leak resources. Below are concise, practical fixes and safe patterns you can apply without changing the visual idea.

First, avoid blocking the UI thread. Do not perform blocking console I/O during Paint or other UI handlers. Read console input on a background thread, then marshal the result to the form thread and call Invalidate to trigger a repaint. Do not keep a Graphics object in a field; use the PaintEventArgs.Graphics only inside the Paint handler. When you create brushes at runtime, dispose them (or use Using) — shared brushes (Brushes.Azure) must not be disposed. Use Color.FromName or ColorTranslator for flexible, case-insensitive color parsing and validate the result before using it.

A minimal safe pattern (VB.NET sketch):

' background console reader
Task.Run(Sub()
    While True
        Dim input = Console.ReadLine()
        If input Is Nothing Then Exit While
        Dim c = Color.FromName(input)
        If c.IsEmpty Then c = Color.Azure
        form.BeginInvoke(Sub()
            form.CurrentColor = c
            form.Invalidate()
        End Sub)
    End While
End Sub)

' in Form.Paint (keep drawing simple)
Using br As New SolidBrush(CurrentColor)
    e.Graphics.Clear(Color.CornflowerBlue)
    e.Graphics.FillPolygon(br, points)
End Using

For the Lua registration: register Module/shared methods with Nothing as the target (not a thread id), and expose functions that return values when you need data back into Lua. If you need a visible console when running a Forms app, run as a Console application or attach one explicitly; otherwise consider an in-form textbox/log for input and output. Finally, prefer Debug.WriteLine for quick debugging during development instead of mixed console I/O.

These changes will prevent UI hangs, make color parsing robust, and avoid leaks from undisposed GDI objects.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.