Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDraw and order simple objects (rectangle, circle, etc.) and let the user click them
DescriptionThis example shows how to draw and order simple objects (rectangle, circle, etc.) and let the user click them in Visual Basic 6.
Keywordsdrawing, object, rectangle, circle, order, to top, to bottom
CategoriesGraphics, Software Engineering
 
Thanks to Hussien M. Sharaf from Egypt. He made enhancements to the original program.

This program includes some enhancements to the program HowTo: Draw simple objects (rectangle, circle, etc.) and let the user click them. It adds a toolbar and the ability to reorder objects, moving them to the top or bottom of the redraw stack.

See the original article for information about the basics.

The following code shows how the program lets you move objects to the top or bottom of the pile. Subroutine RemoveSelectedObject removes an object from the m_DrawingObjects collection. It loops through the collection until it finds the ojbect and then removes it.

 
' Remove the selected object from the
' m_DrawingObjects collection.
Private Sub RemoveSelectedObject()
  On Error GoTo ErrorHandler
Dim i As Integer

    ' Find the selected object's current position.
    For i = 1 To m_DrawingObjects.Count
        If m_DrawingObjects(i) Is m_SelectedObject Then _
            Exit For
    Next i

    ' Remove the object.
    m_DrawingObjects.Remove i
    Exit Sub
ErrorHandler:
  ShowErrMessage intErr:=conErrOthers, _
      strErrMessage:=Err.Description
End Sub
 
The following menu event handlers move the selected object. Each uses RemoveSelectedObject to remove the selected object from the drawing collection. They then add the item back into the collection, either at the top or the bottom of the collection. They then redraw the objects.
 
' Move the selected object to the bottom of the list.
Private Sub mnuAttributesPositionToBottom_Click()
  On Error GoTo ErrorHandler
    ' Remove the object from m_DrawingObjects.
    RemoveSelectedObject

    ' Readd the object at the bottom of the list.
    If m_DrawingObjects.Count > 0 Then
        m_DrawingObjects.Add m_SelectedObject, , 1
    Else
        m_DrawingObjects.Add m_SelectedObject
    End If

    Redraw
    Exit Sub
ErrorHandler:
  ShowErrMessage intErr:=conErrOthers, _
      strErrMessage:=Err.Description
End Sub

' Move the selected object to the top of the list.
Private Sub mnuAttributesPositionToTop_Click()
  On Error GoTo ErrorHandler
    ' Remove the object from m_DrawingObjects.
    RemoveSelectedObject

    ' Readd the object at the top of the list.
    m_DrawingObjects.Add m_SelectedObject

    Redraw
    Exit Sub
ErrorHandler:
  ShowErrMessage intErr:=conErrOthers, _
      strErrMessage:=Err.Description
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated