Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Basics of the Windows Forms Library
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Basics of the Windows Forms Library
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-02-15

    Table of Contents:
  • Basics of the Windows Forms Library
  • 4.7 Drawing a Control
  • 4.8 Making a Form the Top-Most Form
  • 4.9 Indicating the Accept and Cancel Buttons on a Form

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Basics of the Windows Forms Library - 4.7 Drawing a Control
    (Page 2 of 4 )

    Problem

    You want to provide custom drawing code for a control.

    Solution

    Sample code folder: Chapter 04\ControlDrawing 

    For most controls, provide an event handler for thePaintevent, and add your drawing code there. This event’s second argument includes aGraphicsproperty representing the canvas on which you can issue your drawing commands. Some controls also provide separateDrawItem events that let you draw specific portions of the control, such as distinct items in aListBoxcontrol. You can also draw directly on the form’s surface. This recipe’s code includes samples for all these activities.

    Create a new Windows Forms application, and add two controls: aButtoncontrol namedXButtonand aComboBoxcontrol namedColorList. Change theColorListcontrol’sDrawModeproperty toOwnerDrawFixedand itsDropDownStyleproperty toDropDownList. Then add the following source code to the form’s code template:

      Private Sub Form1_Load(ByVal sender As Object, _
           
    ByVal e As System.EventArgs) Handles Me.Load
         ' ----- Add some basic colors to the color list.
         ColorList.Items.Add("Red")
         ColorList.Items.Add("Orange")
         ColorList.Items.Add("Yellow")
         ColorList.Items.Add("Green")
         ColorList.Items.Add("Blue")
         ColorList.Items.Add("Indigo")
         ColorList.Items.Add("Violet")
     
    End Sub

      Private Sub Form1_Paint(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.PaintEventArgs) _
            Handles Me.Paint
        
    ' ----- Draw an ellipse on the form.
        
    e.Graphics.DrawEllipse(Pens.Black, 10, 10, _
            Me.ClientRectangle.Width - 20, _
            Me.ClientRectangle.Height - 20)
      End Sub

      Private Sub XButton_Paint(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.PaintEventArgs) _
            Handles XButton.Paint 
        
    ' ----- Draw a big x in a rectangle on the button surface.
         Dim usePen As Pen

         ' ----- Provide a neutral background.
         e.Graphics.Clear(SystemColors.Control)

         ' ----- Draw the outline box.
         usePen = New Pen(SystemColors.ControlText, 3)
         e.Graphics.DrawRectangle(usePen, XButton.ClientRectangle)

         ' ----- Draw the x.
         e.Graphics.DrawLine(usePen, 0, 0, _
            XButton.Width, XButton.Height)
         e.Graphics.DrawLine(usePen, 0, _
            XButton.Height, XButton.Width, 0)
         usePen.Dispose()
      End Sub

      Private Sub ColorList_DrawItem(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.DrawItemEventArgs) _
            Handles ColorList.DrawItem
        
    ' ----- Draw the color instead of the text.
         Dim useBrush As Brush
     
        ' ----- Check for a nonselected item.
         If (e.Index = -1) Then Return

         ' ----- Set the neutral background.
         e.DrawBackground()

         ' ----- Fill in the color.
         useBrush = New SolidBrush(Color.FromName(CStr( _
            ColorList.Items(e.Index))))
         e.Graphics.FillRectangle(useBrush, _
            e.Bounds.Left + 2, e.Bounds.Top + 2, _
            e.Bounds.Width - 4, e.Bounds.Height - 4)
        
    useBrush.Dispose()

         ' ----- Surround the color with a black rectangle.
        
    e.Graphics.DrawRectangle(Pens.Black, _
            e.Bounds.Left + 2, e.Bounds.Top + 2, _
            e.Bounds.Width - 4, e.Bounds.Height - 4)

         ' ----- Show the item selected if needed.
         e.DrawFocusRectangle()
      End Sub

      Private Sub XButton_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles XButton.Click
         MsgBox("Button clicked.")
      End Sub

    Run the program. TheXButton control no longer looks like a button; it instead looks like a custom-drawn “X.” Although the button looks strange, it still works. The ellipse we drew directly on the form’s surface is there. Also, theComboBox control now displays actual colors instead of just the names of colors. This all appears in Figure 4-4.


    Figure 4-4.  Controls drawn with custom code

    Discussion

    Some of the controls that support item-level drawing, such as the ListBox andComboBoxcontrols, include ane.Stateproperty in the data passed to the event handler. This value indicates the current state of the item being drawn: selected, not selected, or a half dozen other relevant states. You do not need to take that property into account if your implementation doesn’t require it, but it is generally a good idea to provide feedback to the user in a way the user expects. Adjusting the display based on this property helps achieve that purpose.

    As shown in the sample code, theDrawItemevent handler includese.DrawBackground()ande.DrawFocusRectangle()methods that help you properly draw the item. Availability of these methods varies by control type.

    See Also

    See the recipes in Chapter 9 for examples that use the various GDI+ drawing commands.

    More Visual Basic.NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Visual Basic 2005 Cookbook," published by...
     

    Buy this book now. This article is excerpted from chapter four of the Visual Basic 2005 Cookbook, written by Tim Patrick and John Clark Craig (O'Reilly; ISBN: 0596101775). Check it out today at your favorite bookstore. Buy this book now.

    VISUAL BASIC.NET ARTICLES

    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...
    - Programmatically Controlling Windows Fax Ser...
    - Focusing on Forms and Menus in Visual Basic
    - Manipulating Forms with the Windows Forms Li...
    - Basics of the Windows Forms Library
    - Forms, Controls, and Other Useful Objects
    - Implementing OOP to Develop Database Oriente...
    - Using Themes and Skins for Personalization w...
    - A Deeper Look at Personalization using Visua...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway