How to use keyup event keystrikes to load paintevent shapes. Is it possible to use a delegate?

Štefan Mihael Rihar 181 Reputation points
2021-12-05T18:15:58.62+00:00

I found for me one possibility to use a keyup event to show a square on my home screen.
The method

    Sub Rechteck()
    Dim Entschlossen As Graphics = Me.CreateGraphics()
    With Entschlossen
        .DrawRectangle(Pens.Blue, 100, 100, 100, 100)
    End With
    End Sub

with the command 'CreateGraphics' is one option. I assume there are more. Also i consider
the documented posts regarding 'CreateGraphics' , that the command is used for components
and controls to change the graphics.

My extension.vb file holds with the above code

Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Public Sub New()
    End Sub

    Sub Rechteck()
    Dim Entschlossen As Graphics = Me.CreateGraphics()
    With Entschlossen
        .DrawRectangle(Pens.Blue, 100, 100, 100, 100)
    End With
    End Sub
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyUp
        Select Case  e.KeyCode
            Case Keys.Space
                Rechteck()
        End Select
    End Sub
        <STAThread()> _
    Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New Form1())
    End Sub
End Class

I have no time to remember every codeline. My misery. But i try. Therefore
i search for a path to have keyup event roots from a paintevent.
My biggest problem searching and remembering is that the variablename
for a painteventargs
has to be consdiered in a, for example, keyup event. It is not a vb.net language
problem i assume, again, it is yet a problem for me.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,614 questions
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-12-13T06:42:33.077+00:00

    Hi @Štefan Mihael Rihar ,
    Thanks for your feedback.
    You can refer to the following way to use setclip method in keyup event.

        Private state As String  
        Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp  
            Select Case e.KeyCode  
                Case Keys.O  
                    state = "O"  
                Case Keys.Space  
                    state = "Space"  
            End Select  
            Me.Invalidate()  
        End Sub  
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint  
            Select Case state  
                Case "O"  
                    Dim clipRect As New Rectangle(100, 100, 100, 100)  
                    e.Graphics.SetClip(clipRect, CombineMode.Replace)  
                    e.Graphics.FillRectangle(New SolidBrush(Color.Red), 0, 0, 500, 500)  
                Case "Space"  
                    Dim clipRect As New Rectangle(50, 50, 50, 50)  
                    e.Graphics.SetClip(clipRect, CombineMode.Replace)  
                    e.Graphics.FillRectangle(New SolidBrush(Color.Green), 0, 0, 500, 500)  
            End Select  
        End Sub  
    

    Press 'O':
    157045-1.png
    Press 'Space':
    157063-2.png

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-12-08T02:00:57.48+00:00

    Hi @Štefan Mihael Rihar ,

    I cannot see and develop how the Method "Osmoza" can be conected to a KeyUp Event.

    Perhaps there's another idea : If you want to paint after KeyUp, in your KeyUp event, set a boolean flag indicating that the key has been pressed and then call Control.Invalidate().
    Take a look at the following code:

        Private doDrawing As Boolean = False  
        Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp  
            Select Case e.KeyCode  
                Case Keys.Space  
                    doDrawing = True  
                    Me.Invalidate()  
            End Select  
        End Sub  
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint  
            If doDrawing Then  
                e.Graphics.DrawRectangle(Pens.Blue, 100, 100, 100, 100)  
            End If  
        End Sub  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.