Error When trying to inherit TabControl - vb.net

Hello guys i got the following error when trying to inherit tabcontrol: Base class 'System.Windows.Forms.TabControl' Specified for class 'Form1' cannot be different from the base class 'System.Windows.Forms.Form' of one of its other partial types!
Here is a picture: http://prntscr.com/3dqzd6
What im trying to make is a animated tab control. i Found a code but it was only for visual basic(I code in c# but this code was for vb only)
My code:
Public Class Form1
Inherits TabControl//Here i got the error
Dim OldIndex As Integer
Private _Speed As Integer = 9
Property Speed As Integer
Get
Return _Speed
End Get
Set(ByVal value As Integer)
If value > 20 Or value < -20 Then
MsgBox("Speed needs to be in between -20 and 20.")
Else
_Speed = value
End If
End Set
End Property
Sub New()
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw, True)
End Sub
Sub DoAnimationScrollLeft(ByVal Control1 As Control, ByVal Control2 As Control)
Dim G As Graphics = Control1.CreateGraphics()
Dim P1 As New Bitmap(Control1.Width, Control1.Height)
Dim P2 As New Bitmap(Control2.Width, Control2.Height)
Control1.DrawToBitmap(P1, New Rectangle(0, 0, Control1.Width, Control1.Height))
Control2.DrawToBitmap(P2, New Rectangle(0, 0, Control2.Width, Control2.Height))
For Each c As Control In Control1.Controls
c.Hide()
Next
Dim Slide As Integer = Control1.Width - (Control1.Width Mod _Speed)
Dim a As Integer
For a = 0 To Slide Step _Speed
G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
G.DrawImage(P2, New Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height))
Next
a = Control1.Width
G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
G.DrawImage(P2, New Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height))
SelectedTab = Control2
For Each c As Control In Control2.Controls
c.Show()
Next
For Each c As Control In Control1.Controls
c.Show()
Next
End Sub
Protected Overrides Sub OnSelecting(ByVal e As System.Windows.Forms.TabControlCancelEventArgs)
If OldIndex < e.TabPageIndex Then
DoAnimationScrollRight(TabPages(OldIndex), TabPages(e.TabPageIndex))
Else
DoAnimationScrollLeft(TabPages(OldIndex), TabPages(e.TabPageIndex))
End If
End Sub
Protected Overrides Sub OnDeselecting(ByVal e As System.Windows.Forms.TabControlCancelEventArgs)
OldIndex = e.TabPageIndex
End Sub
Sub DoAnimationScrollRight(ByVal Control1 As Control, ByVal Control2 As Control)
Dim G As Graphics = Control1.CreateGraphics()
Dim P1 As New Bitmap(Control1.Width, Control1.Height)
Dim P2 As New Bitmap(Control2.Width, Control2.Height)
Control1.DrawToBitmap(P1, New Rectangle(0, 0, Control1.Width, Control1.Height))
Control2.DrawToBitmap(P2, New Rectangle(0, 0, Control2.Width, Control2.Height))
For Each c As Control In Control1.Controls
c.Hide()
Next
Dim Slide As Integer = Control1.Width - (Control1.Width Mod _Speed)
Dim a As Integer
For a = 0 To -Slide Step -_Speed
G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
G.DrawImage(P2, New Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height))
Next
a = Control1.Width
G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
G.DrawImage(P2, New Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height))
SelectedTab = Control2
For Each c As Control In Control2.Controls
c.Show()
Next
For Each c As Control In Control1.Controls
c.Show()
Next
End Sub
Please help me!
-Christos

When you create a form the designer makes one named Form1 it has a Partial Class by the same name - this is where the generated code is maintained and should not be touched by you. This Form1 Inherits from System.Windows.Forms.Form and hence cannot be made to inherit from a TabControl. If you simply change the name of the class you have above that intends to be a Sub-Classed version of the TabControl then all would be well.
Public Class Form1
...
End Class
Partial Class Form1
'same class as above just allowed to be seperate
'they share the internal code
End Class
Your code should start like:
Public Class myTabControl : Inherits TabControl
'continue with sub classing

Related

Transparent image over two controls with different back colors

I am trying to place a transparent image over two adjacent controls that have different background colors.
I want the image to remain transparent, meaning the Image needs to show the backcolor of each control.
The controls are two Panels set to different background colors and the Image (PictureBox or otherwise) is placed between the two panel controls.
Public Class frmMain
Private Img1 As Image = Image.FromFile("C:\xxxx.png")
Private Sub frmMain_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
e.Graphics.DrawImage(Img1, 5, 5)
End Sub
End Class
Let's try this.
Create a new class in the Project, call it TPanel and paste in the custom Panel class you can find below, overwriting the existing definition.
Compile the Project then find the new TPanel control in the ToolBox and drop one instance inside a Form.
On the Form, not inside one of the Colored Panels, otherwise it will become child of another control and it will be confined inside its bounds.
Add an event handler to the Paint event of the TPanel and insert this code inside the handler method:
Private Sub TPanel1_Paint(sender As Object, e As PaintEventArgs) Handles TPanel1.Paint
Dim canvas As Control = DirectCast(sender, Control)
Dim rect As Rectangle = ScaleImageFrame(imgBasketBall, canvas.ClientRectangle)
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
e.Graphics.CompositingMode = CompositingMode.SourceOver
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half
e.Graphics.DrawImage(imgBasketBall, rect)
End Sub
Private Function ScaleImageFrame(sourceImage As Bitmap, destinationFrame As Rectangle) As Rectangle
Dim rect As RectangleF = New RectangleF(0, 0, sourceImage.Width, sourceImage.Height)
'Define the ratio between the Image Rectangle and the Container ClientRectangle
Dim ratio As Single = CType(Math.Max(destinationFrame.Width, destinationFrame.Height) /
Math.Max(rect.Width, rect.Height), Single)
rect.Size = New SizeF(rect.Width * ratio, rect.Height * ratio)
'Use Integer division to avoid negative values
rect.Location = New Point((destinationFrame.Width - CInt(rect.Width)) \ 2,
(destinationFrame.Height - CInt(rect.Height)) \ 2)
Return Rectangle.Round(rect)
End Function
In the Form, create an instance of a Bitmap object that will contain the Image; also set the Location of the Panel (TPanel)
The Controls called panColored1 and panColored2 are supposed to be the names of the two existing Panels where the Image must be positioned. The sample code positions the Image in the middle of the 2 Panels, using TPanel1.Location( (...) )
Private imgBasketBall As Bitmap = Nothing
Public Sub New()
InitializeComponent()
imgBasketBall = DirectCast(Image.FromStream(New MemoryStream(File.ReadAllBytes("basketball.png"))), Bitmap)
TPanel1.Size = New Size(120, 120)
TPanel1.Location = New Point(panColored1.Left + (panColored1.Width - TPanel1.Width) \ 2,
panColored1.Top + (panColored1.Height + panColored2.Height - TPanel1.Height) \ 2)
TPanel1.BringToFront()
End Sub
Result:
Bitmap Size Bitmap Size
(1245x1242) (1178x2000)
The TPanel (Transparent Panel) class:
Imports System.ComponentModel
<DesignerCategory("Code")>
Public Class TPanel
Inherits Panel
Private Const WS_EX_TRANSPARENT As Integer = &H20
Public Sub New()
SetStyle(ControlStyles.AllPaintingInWmPaint Or
ControlStyles.UserPaint Or
ControlStyles.Opaque Or
ControlStyles.ResizeRedraw, True)
SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
e.Graphics.FillRectangle(Brushes.Transparent, Me.ClientRectangle)
MyBase.OnPaint(e)
End Sub
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim parameters As CreateParams = MyBase.CreateParams
parameters.ExStyle = parameters.ExStyle Or WS_EX_TRANSPARENT
Return parameters
End Get
End Property
End Class
There is also something you can also try, It may not be professional but it works. Split the images into two halves. Draw the first half on one of the panels and the second half on the other panel.
Be Sure to Import System.IO in Your project.
The code for the splitting goes like this:
Imports System.IO
...
Public Function SplitImage(ByVal imgpath As String) As Image()
Dim img As Image = Image.FromFile(imgpath)
Dim bmp As Bitmap = DirectCast(img, Bitmap)
Dim i As Integer = bmp.Height / 2
Dim image1 As Bitmap = New Bitmap(bmp.Width, i)
Dim image2 As Bitmap = New Bitmap(bmp.Width, i)
Dim yPos As Integer = 0
For x As Integer = 0 To image1.Width - 1
For y As Integer = 0 To image1.Height - 1
image1.SetPixel(x, y, bmp.GetPixel(x, y))
yPos = y
Next
Next
yPos += 1
Dim ycount As Integer = 0
For x As Integer = 0 To image2.Width - 1
For y As Integer = yPos To bmp.Height - 1
If ycount = i Then
ycount -= 1
End If
image2.SetPixel(x, ycount, bmp.GetPixel(x, y))
ycount += 1
Next
ycount = 0
Next
Dim ms As MemoryStream = New MemoryStream
Dim ms1 As MemoryStream = New MemoryStream
image1.Save(ms, Imaging.ImageFormat.Png)
image2.Save(ms1, Imaging.ImageFormat.Png)
Dim returnedImage(2) As Image
returnedImage(0) = image1
returnedImage(1) = image2
Return returnedImage
End Function
Create Two panels on your form (Panel1 and Panel2) and a Button(Button1).
Place The two panels the way you want it, set the BackgroundImageLayout property of the panels to StretchImage.
Then from your code you can call the function like this, i.e From the Button's click event:
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pic() As Image = SplitImage("C:\xxxx.png")
Panel1.BackgroundImage = pic(0)
Panel2.BackgroundImage = pic(1)
End Sub
For More Information about the Bitmap Class, Check out this link Bitmap Class

Grid I can paint on

So I am trying to create an application to ease creation of pixel arts (school project), what I've done so far is draw a grid in a panel, next step would be to allow the user to click on a cell and have it painted, but I can't manage to make it work, here's the code I have:
Private Sub drawGrid(g As Graphics, rows As Integer, columns As Integer)
Dim originPoint As Point = New Point(10, 2)
Dim size As Size = New Size(64, 64)
Dim left As Integer = originPoint.X
Dim up As Integer = originPoint.Y
Dim right As Integer = originPoint.X + (columns * size.Width)
Dim down As Integer = originPoint.Y + (rows * size.Height)
For y As Integer = up To down + 1 Step size.Height
Dim pt1 As New Point(left, y)
Dim pt2 As New Point(right, y)
g.DrawLine(Pens.Black, pt1, pt2)
Next
For x As Integer = left To right + 1 Step size.Width
Dim pt1 As New Point(x, up)
Dim pt2 As New Point(x, down)
g.DrawLine(Pens.Black, pt1, pt2)
Next
End Sub
This draws a grid with the amount of columns and rows the user wants, but I've been struggling to allow painting
What I've been thinking is: dispose this code, and create a 'pixel' class, create the amount of 'pixel' objects based on user rows and columns, and draw each one individually, then just change each 'pixel's' color
This is a Grid class that allows setting the color of its cells.
The Grid cell are referenced using a List(Of List(Of Class)).
The Cell class Object contains is a simple Rectagle property that measures the size of the cell, and a Color property, which allows to set the color of the single cell:
Friend Class GridCell
Public Property Cell() As Rectangle
Public Property CellColor() As Color
End Class
You can define:
The size of the Grid → ColoredGrid.GridSize = new Size(...)
The number of Columns and Rows → ColoredGrid.GridColumnsRows = new Size(...)
The position of the Grid inside the Canvas → ColoredGrid.GridPosition = New Point(...)
The color of the Grid → ColoredGrid.GridColor = Color.Gray
The BackGround color of the cells → ColoredGrid.CellColor = Color.FromArgb(32, 32, 32)
The color of a selected cell → ColoredGrid.SelectedCellColor = Color.OrangeRed
The Grid class holds a reference to the control which will be used as the Canvas for the grid painting. This reference is set in the class contructor.
The Grid registers the Canvas control Paint() and MouseClick() events to respond to the related actions automatically.
When a Mouse Click is detected on the Canvas surface, the MouseEventArgs e.Location property reports the coordinates where the Click occurred.
To identify the Grid Cell where this action is performed, the GetUpdateCell() method inspects the List(Of List(Of GridCell)) using a simple LINQ SelectMany() and identified the Cell rectangle that contains the Mouse Click coordinates (expressed as a Point() value).
This identification is performed simply checking whether the Cell Rectangle.Contains(Point()).
When the cell is identified, the Canvas Invalidate() method is called, specifing the area to repaint.
This area corresponds to the Cell Rectangle, so only this section is repainted when a Cell is colored, to save resources and time.
To test it, create a Panel and a Button in a Form:
Imports System.Drawing
'This Grid object in defined at Form Class scope
Public ColoredGrid As ColorGrid
'Button used to trigger the Grid painting
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ColoredGrid IsNot Nothing Then
ColoredGrid.Dispose()
End If
ColoredGrid = New ColorGrid(Panel1)
ColoredGrid.GridSize = New Size(300, 300)
ColoredGrid.GridColumnsRows = New Size(10, 10)
ColoredGrid.GridPosition = New Point(10, 10)
ColoredGrid.GridColor = Color.White
ColoredGrid.CellColor = Color.FromArgb(32, 32, 32)
ColoredGrid.SelectedCellColor = Color.OrangeRed
ColoredGrid.BuildGrid()
End Sub
This is a visual sample that shows how it works:
This is the main Grid class.
The ColorGrid Class supports IDisposable, because it registers the described events. These must be unregistered when the Class is not used anymore. Weird things can happen if you don't.
Public Class ColorGrid
Implements IDisposable
Private Grid As List(Of List(Of GridCell))
Private CurrentGridSize As New Size(100, 100)
Private GridColRows As New Size(10, 10)
Private CellSize As New Size(10, 10)
Private MouseCell As Point = Point.Empty
Private Canvas As Control = Nothing
Private UpdateCell As Boolean = False
Private NewGrid As Boolean = False
Public Sub New(DrawingControl As Control)
If DrawingControl IsNot Nothing Then
Me.Canvas = DrawingControl
AddHandler Me.Canvas.Paint, New PaintEventHandler(AddressOf Me.ControlPaint)
AddHandler Me.Canvas.MouseClick, New MouseEventHandler(AddressOf Me.MouseHandler)
Me.GridPosition = New Point(10, 10)
Me.CellColor = Color.FromArgb(32, 32, 32)
End If
End Sub
Public Property GridPosition() As Point
Public Property CellColor() As Color
Public Property SelectedCellColor() As Color
Public Property GridColor() As Color
Public Property GridSize() As Size
Get
Return Me.CurrentGridSize
End Get
Set(value As Size)
Me.CurrentGridSize = value
SetCellSize()
End Set
End Property
Public Property GridColumnsRows() As Size
Get
Return Me.GridColRows
End Get
Set(value As Size)
Me.GridColRows = value
SetCellSize()
End Set
End Property
Private Property RefreshCell() As GridCell
Friend Class GridCell
Public Property Cell() As Rectangle
Public Property CellColor() As Color
End Class
Private Sub SetCellSize()
Me.CellSize = New Size((Me.CurrentGridSize.Width \ Me.GridColRows.Width),
(Me.CurrentGridSize.Height \ Me.GridColRows.Height))
If Me.CellSize.Width < 4 Then Me.CellSize.Width = 4
If Me.CellSize.Height < 4 Then Me.CellSize.Height = 4
End Sub
Public Sub BuildGrid()
If Me.Canvas Is Nothing Then Return
Me.Grid = New List(Of List(Of GridCell))()
For row As Integer = 0 To GridColumnsRows.Height - 1
Dim RowCells As New List(Of GridCell)()
For col As Integer = 0 To GridColumnsRows.Width - 1
RowCells.Add(New GridCell() With {
.Cell = New Rectangle(New Point(Me.GridPosition.X + (col * Me.CellSize.Width),
Me.GridPosition.Y + (row * Me.CellSize.Height)),
Me.CellSize),
.CellColor = Me.CellColor})
Next
Me.Grid.Add(RowCells)
Next
Me.NewGrid = True
Me.Canvas.Invalidate()
End Sub
Private Sub ControlPaint(o As Object, e As PaintEventArgs)
If Me.NewGrid Then
e.Graphics.Clear(Me.Canvas.BackColor)
Me.NewGrid = False
End If
Me.Grid.
SelectMany(Function(rowcells) rowcells).
Select(Function(colcell)
If Me.UpdateCell Then
Using brush As New SolidBrush(Me.RefreshCell.CellColor)
e.Graphics.FillRectangle(brush, Me.RefreshCell.Cell.X + 1, Me.RefreshCell.Cell.Y + 1,
Me.RefreshCell.Cell.Width - 1, Me.RefreshCell.Cell.Height - 1)
End Using
Me.UpdateCell = False
Return Nothing
Else
Using pen As New Pen(Me.GridColor)
e.Graphics.DrawRectangle(pen, colcell.Cell)
End Using
Using brush As New SolidBrush(colcell.CellColor)
e.Graphics.FillRectangle(brush, colcell.Cell.X + 1, colcell.Cell.Y + 1,
colcell.Cell.Width - 1, colcell.Cell.Height - 1)
End Using
End If
Return colcell
End Function).TakeWhile(Function(colcell) colcell IsNot Nothing).ToList()
End Sub
Private Sub MouseHandler(o As Object, e As MouseEventArgs)
Me.RefreshCell = GetUpdateCell(e.Location)
Me.RefreshCell.CellColor = Me.SelectedCellColor
Dim CellColorArea As Rectangle = Me.RefreshCell.Cell
CellColorArea.Inflate(-1, -1)
Me.UpdateCell = True
Me.Canvas.Invalidate(CellColorArea)
End Sub
Private Function GetUpdateCell(CellPosition As Point) As GridCell
Return Me.Grid.
SelectMany(Function(rowcells) rowcells).
Select(Function(gridcell) gridcell).
Where(Function(gridcell) gridcell.Cell.Contains(CellPosition)).
First()
End Function
Public Sub Dispose() Implements IDisposable.Dispose
If Me.Canvas IsNot Nothing Then
RemoveHandler Me.Canvas.Paint, AddressOf Me.ControlPaint
RemoveHandler Me.Canvas.MouseClick, AddressOf Me.MouseHandler
Me.Grid = Nothing
End If
End Sub
End Class

Custom Control Display Issue

I have created this class in visual Basic it works fine but there is a slight design issue. As you can see there is this small bumb, how can I fix this? And also, how can I fix the spacing between the content Box and the selection menu.
Public Class VerticallTabControll
Inherits TabControl
Sub New()
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
DoubleBuffered = True
SizeMode = TabSizeMode.Fixed
ItemSize = New Size(30, 170)
End Sub
Protected Overrides Sub CreateHandle()
MyBase.CreateHandle()
Alignment = TabAlignment.Left
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim selected As New SolidBrush(Color.FromArgb(0, 122, 204))
Dim notSelected As New SolidBrush(Color.FromArgb(63, 63, 70))
Dim B As New Bitmap(Width, Height)
Dim G As Graphics = Graphics.FromImage(B)
G.Clear(Color.FromArgb(63, 63, 70))
For i = 0 To TabCount - 1
Dim TabRectangle As Rectangle = GetTabRect(i)
If i = SelectedIndex Then
'// if tab is selected
G.FillRectangle(selected, TabRectangle)
Else
'// tab is not selected
G.FillRectangle(notSelected, TabRectangle)
End If
'Line Test
'Dim start As New Point(10, 31 * (i + 1))
'Dim ende As New Point(160, 31 * (i + 1))
'G.DrawLine(Pens.White, start, ende)
G.DrawString(TabPages(i).Text, Font, Brushes.White, TabRectangle, New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
Next
e.Graphics.DrawImage(B.Clone, 0, 0)
G.Dispose() : B.Dispose() : selected.Dispose() : notSelected.Dispose()
MyBase.OnPaint(e)
e.Dispose()
End Sub
End Class
You can try overriding the DisplayRectangle property to adjust the interior space accordingly:
Public Overrides ReadOnly Property DisplayRectangle As Rectangle
Get
Return New Rectangle(MyBase.DisplayRectangle.Left,
MyBase.DisplayRectangle.Top - 2,
MyBase.DisplayRectangle.Width + 2,
MyBase.DisplayRectangle.Height + 4)
End Get
End Property

Checking to see if Cursor is in rectangle bounds

I have a program that randomly sets the cursor that an x,y coordinate and then clicks. I have this function/sub that creates a rectangle based on certain parameters.
Private Sub drawTitleBarRectangle()
Dim titleBarRectangle As Rectangle = RectangleToScreen(Me.ClientRectangle)
Dim titleBarRectangleHeight As Integer = titleBarRectangle.Top - Me.Top
Dim titleBarRectangleWidth As Integer = Screen.PrimaryScreen.Bounds.Width
Dim titleBarRectangleTop As Integer = Screen.PrimaryScreen.Bounds.Top
Dim titleBarBounds As New Drawing.Rectangle(0, 0, titleBarRectangleWidth, titleBarRectangleHeight)
End Sub
I want to check if when the cursor is at it's x,y position if it is within the bounds of the rectangle created from that function or not. Right now I have this:
drawTitleBarRectangle()
SetCursorPos(x, y)
If titleBarRectangle.Contains(x, y) Then
leftClick(800, 800)
End If
The Private titleBarRectangle is from a global variable that I declare as Private titleBarRectangle As New Drawing.Rectangle I'm not too sure why it's there to be honest...
Any help would be appreciated.
All of the variables in the initial method you've listed are local variables. This means they are simply discarded when that method exits. You need to update the class level variable you've declared by making an assignment instead of a declaration. With that in mind, it should look more like:
Public Class Form1
Private titleBarRectangle As Rectangle
Private Sub drawTitleBarRectangle()
Dim rc As Rectangle = Me.RectangleToScreen(Me.ClientRectangle)
Dim titleBarRectangleHeight As Integer = rc.Top - Me.Top
titleBarRectangle = New Rectangle(Me.Location.X, Me.Location.Y, Me.Width, titleBarRectangleHeight)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
drawTitleBarRectangle()
Debug.Print(titleBarRectangle.ToString)
ControlPaint.DrawReversibleFrame(titleBarRectangle, Color.Black, FrameStyle.Dashed)
Dim x As Integer = titleBarRectangle.Location.X + titleBarRectangle.Width / 2
Dim y As Integer = titleBarRectangle.Location.Y + titleBarRectangle.Height / 2
Cursor.Position = New Point(x, y)
If titleBarRectangle.Contains(Cursor.Position) Then
Debug.Print("It's in there!")
End If
End Sub
End Class
Notice how the last line in the method will use the class level variable instead of a local since we don't have Dim in front of it.

Draw triangle VB.NET

Dear forum members I have to make a triangle class.
My problem is that I the public overrides sub teken does not work
IN MY CLASS FORM (FUNCTION RANDOMSTRAAL)
Private Function RandomStraal() As Int32
Return 20 + _Random.Next(Me.ClientSize.Width\2)
End Function
IN MY CLASS FORM (SUB MAAKTRIANGLE)
Private Sub MaakTriangle(x As Integer, y As Integer)
Dim tria As New Triangle(RandomKleur, RandomKleur, New Point(x, y), New Point(x, y + RandomStraal()), New Point(x + RandomStraal(), y))
tria.PenDikte = _Random.Next(1, 6)
_Tekening.Add(tria)
'_Tekening.Teken(Me.CreateGraphics)
Invalidate()
End Sub
MY CLASS TRIANGLE
Friend Class Triangle
Inherits Figuur
Public Property Point1() As Point
Public Property Point2() As Point
Public Property Point3() As Point
Private _Pointers() As Point = {Point1, Point2, Point3}
Public Sub New(penKleur As Color, vulKleur As Color, point1 As Point, point2 As Point, point3 As Point)
MyBase.New(penKleur, vulKleur)
Me.Point1 = point1
Me.Point2 = point2
Me.Point3 = point3
End Sub
Public Overrides Sub Teken( doek As Graphics)
Using borstel As New SolidBrush(VulKleur),
pen As New Pen(PenKleur, PenDikte)
Dim tria As New Rectangle(_Pointers) **'<--the problem**
doek.FillPolygon(borstel, tria)
doek.DrawPolygon(pen, tria)
End Using
End Sub
End Class
what should happen to do this work
thanks in advance
Two problems:
The Rectangle object doesn't take an array of pointers, and besides, you are trying to make a triangle, not a rectangle. Eliminate this:
' Dim tria As New Rectangle(_Pointers)
The second problem is you are referencing the _Pointers array, but they are not updated with the new coordinates. The points are all (0, 0):
Try it like this:
Public Overrides Sub Teken(doek As Graphics)
Using borstel As New SolidBrush(VulKleur), _
pen As New Pen(Me.PenKleur, Me.PenDikte)
Dim myPoints() As Point = New Point() {Point1, Point2, Point3}
doek.FillPolygon(borstel, myPoints)
doek.DrawPolygon(pen, myPoints)
End Using
End Sub
Side note: make sure you to use the Paint event of your control:
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) _
Handles Panel1.Paint
e.Graphics.Clear(Color.White)
Dim tria As New Triangle(Color.Blue, Color.Red, New Point(64, 64), _
New Point(96, 96), _
New Point(32, 96))
tria.Teken(e.Graphics)
End Sub
or override the OnPaint method if drawing directly on a form.