User created rectangles
User created rectangles
User created rectangles
User created rectangles
User created rectangles
User created rectangles User created rectangles User created rectangles User created rectangles User created rectangles User created rectangles User created rectangles User created rectangles
User created rectangles User created rectangles
User created rectangles
Go Back  Xtreme Visual Basic Talk > > > User created rectangles


Reply
 
Thread Tools Display Modes
  #1  
Old 04-11-2014, 01:44 PM
Under Study Under Study is offline
Regular
 
Join Date: Jul 2008
Posts: 83
Default User created rectangles


I don't think Ive ever been this lost in how to accomplish this, seemly simple, task.

I need to be able to create a "limitless" number of rectangles on the form. For each time a user clicks on the form, a rectangle should pop up on the center of their cursor. They should be able to click as many times as they want and at any time, use the arrow keys to move all the current rectangles around the screen.

Ive got everything i need to make this happen except the draw method. I don't know how to write code that flexible in this way... if i could limit it to like 10 or 50 rectangles, that would be simple enough, but to have the number be dependent on the user... I am lost.

Any ideas?
Thanks!
Reply With Quote
  #2  
Old 04-13-2014, 07:43 AM
Gruff's Avatar
GruffUser created rectangles Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Although it is not rocket science you do have to look up some things in help to get started.

1) DrawRectangles(<Pen>,<Rectangle Array>)
2) TranslateTransform(deltaX, deltaY)
3) List(of Type) In this case rectangle
4) KeyValue

The Paint event is where you draw items you want to persist.

Code:
Public Class Form1 Const nSize as integer = 40 'Pixels Const nMove as integer = 4 'Pixels Dim RectList As New List(Of Rectangle) Dim RectSize As New Size(nSize, nSize) Dim GX As Integer = 0 Dim GY As Integer = 0 Private Sub Form1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp If e.KeyValue >= Keys.Left And e.KeyValue <= Keys.Down Then Select Case e.KeyValue Case Keys.Left GX += -nMove Case Keys.Right GX += nMove Case Keys.Up GY += -nMove Case Keys.Down GY += nMove End Select Me.Invalidate() ' Redraw screen End If End Sub Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown ' Center the rectangle over the mouse point Dim Pt As New Point(e.Location.X - RectSize.Width \ 2, e.Location.Y - RectSize.Height \ 2) Dim Rect As New Rectangle(Pt, RectSize) RectList.Add(Rect) Me.Invalidate() ' Redraw screen End Sub Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint With e.Graphics If RectList.Count > 0 Then ' Only draw something if there are rectangles in the list .TranslateTransform(GX, GY) .DrawRectangles(Pens.Black, RectList.ToArray) End If End With End Sub End Class
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 04-13-2014 at 08:00 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
User created rectangles
User created rectangles
User created rectangles User created rectangles
User created rectangles
User created rectangles
User created rectangles User created rectangles User created rectangles User created rectangles User created rectangles User created rectangles User created rectangles
User created rectangles
User created rectangles
 
User created rectangles
User created rectangles
 
-->