-1

So to explain what my problem is, I made a PictureBox, and I need to fill in many FILLED squares inside of it. However, to do so I would need to create a brush and all of the solutions I've found online are returned as errors by Visual Studio 2019. I don't know what to do anymore.

Here's an example for brush declaration:

SolidBrush shadowBrush = new SolidBrush(customColor) (returns error)


Brush randomBrush = new brush(customColor) (returns error)
Darp mosh
  • 1
  • 3
  • How about showing the code implementation of your best attempt, the exact error(s) your encountering and where your encountering them – Hursey Jan 28 '21 at 20:11
  • OK I've added and edited the question to include examples.] – Darp mosh Jan 28 '21 at 20:43
  • 1
    The `Brush` is an abstract class, you can't create instances of it. As for the first line, what is the `customColor`? How do you declare it? What is the exception/error message? – dr.null Jan 28 '21 at 21:33
  • Sorry but a single line of code with not context is pointless. What exactly is the error? – Hursey Jan 28 '21 at 22:06
  • 1
    Why is this question tagged VB.NET but contains code that looks more like C# than VB? Is that your actual problem? Are you copying C# code form the web and pasting it into a VB app? – jmcilhinney Jan 28 '21 at 23:45
  • nvm I've resolved this problem here and I know what to do now. Now all I need is to know how to set a public array. – Darp mosh Jan 29 '21 at 02:07
  • @jmcilhinney What I typed in was C# true because I didn't know how to assign Brushes and Pens in Visual Basic. Through Even more digging I know how to assign them now. – Darp mosh Jan 29 '21 at 02:08
  • It's not about "assigning Brushes and Pens". It's about declaring variables, creating objects and assigning them to those variables. If you've been through a beginners VB tutorial then you know how to do those things. What the specific types are is irrelevant because the syntax is the same regardless. If you really had written C# code in a VB app, does that mean that writing the appropriate VB code has solved your issue? If so then you ought to delete this question because it's not an actual VB problem and has nothing to do with `Brush` objects. – jmcilhinney Jan 29 '21 at 02:58

1 Answers1

0

The way GDI+ drawing works, you should store all the data that represents your drawing in one or more fields and then read that data in the Paint event handler of the appropriate control to do the drawing. In your case, you need information to represent a square and the colour it will be drawn in and you need multiple of them. In that case, you should define a type that has a Rectangle property and a Color property and store a generic List of that type. You can then loop through that list, create a SolidBrush with the Color and call FillRectangle.

Public Class Form1

    Private ReadOnly boxes As New List(Of Box)

    Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        For Each box In boxes
            Using b As New SolidBrush(box.Color)
                e.Graphics.FillRectangle(b, box.Bounds)
            End Using
        Next
    End Sub

End Class

Public Class Box

    Public Property Bounds As Rectangle

    Public Property Color As Color

End Class

Now, to add a square, you simply create a new Box object, add it to the List and then call Invalidate on the PictureBox. For simplicity, you can call Invalidate with no arguments and the whole PictureBox will be repainted. It is better if you can specify the area that has or may have changed though, because that keeps the repainting, which is the slow part, to a minimum. As you already have a Rectangle that describes the area that has changed, you can pass that, e.g.

Dim boxBounds As New Rectangle(10, 10, 100, 100)

boxes.Add(New Box With {.Bounds = boxBounds, .Color = Color.Black})

PictureBox1.Invalidate(boxBounds)
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Thank you SOOO Much! This is very interesting indeed but I'm looking for something else. But this will come in handy as a reference, and potentially be used! Thank you! – Darp mosh Jan 29 '21 at 02:22