Shape Demo

Tuesday, August 4th, 2009

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Configuration
Imports System.Resources
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Diagnostics

Public Class MainClass
    Shared Sub Main()
        Dim myform As Form = New ShapesForm()
        Application.Run(myform)
    End Sub
End Class
Public Class ShapesForm
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        ‘This call is required by the Windows Form Designer.
        InitializeComponent()

        ‘Add any initialization after the InitializeComponent() call

    End Sub

    ‘Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ‘Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    ‘NOTE: The following procedure is required by the Windows Form Designer
    ‘It can be modified using the Windows Form Designer.  
    ‘Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
        Me.Text = "ShapesForm"
    End Sub

#End Region

    Private Sub ShapesForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint

        Dim g As Graphics = e.Graphics
        Dim x As Integer = 0
        Dim y As Integer = 0
        Dim width As Integer = Me.ClientRectangle.Width / 3
        Dim height As Integer = Me.ClientRectangle.Height / 3
        Dim blackBrush As Brush = Brushes.Black
        Dim fillBrush As Brush = Brushes.Gray
        Dim whitePen As Pen = Pens.White
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center

        Dim pen As Pen = New Pen(Color.Blue, 4)
        g.DrawArc(pen, x + 10, y + 10, width - 20, height - 20, 180, 180)
        g.DrawString("Arc", Me.Font, blackBrush, New RectangleF(x, y, width, height), format)
        x = IIf(x > Me.ClientRectangle.Width - 2 * width, 0, x + width)
        y = IIf(x = 0, y + height, y)

        g.DrawBezier(pen, New Point(x + 10, y + height - 20), New Point(x + width - 20, y + height - 20), New Point(x + 10, y + 10), New Point(x + width - 20, y + 10))
        g.DrawString("Bezier", Me.Font, blackBrush, New RectangleF(x, y, width, height), format)
        x = IIf(x > Me.ClientRectangle.Width - 2 * width, 0, x + width)
        y = IIf(x = 0, y + height, y)

        g.FillClosedCurve(fillBrush, New PointF() {New PointF(x + 10, y + height - 20), New PointF(x + width - 20, y + height - 20), New PointF(x + 10, y + 10), New PointF(x + width - 20, y + 10)})
        g.DrawClosedCurve(pen, New PointF() {New PointF(x + 10, y + height - 20), New PointF(x + width - 20, y + height - 20), New PointF(x + 10, y + 10), New PointF(x + width - 20, y + 10)})
        g.DrawString("Closed Curve", Me.Font, blackBrush, New RectangleF(x, y, width, height), format)
        x = IIf(x > Me.ClientRectangle.Width - 2 * width, 0, x + width)
        y = IIf(x = 0, y + height, y)

        g.DrawCurve(pen, New PointF() {New PointF(x + 10, y + height - 20), New PointF(x + width - 20, y + height - 20), New PointF(x + 10, y + 10), New PointF(x + width - 20, y + 10)})
        g.DrawString("Curve", Me.Font, blackBrush, New RectangleF(x, y, width, height), format)
        x = IIf(x > Me.ClientRectangle.Width - 2 * width, 0, x + width)
        y = IIf(x = 0, y + height, y)

        g.FillEllipse(fillBrush, x + 10, y + 10, width - 20, height - 20)
        g.DrawEllipse(pen, x + 10, y + 10, width - 20, height - 20)
        g.DrawString("Ellipse", Me.Font, blackBrush, New RectangleF(x, y, width, height), format)
        x = IIf(x > Me.ClientRectangle.Width - 2 * width, 0, x + width)
        y = IIf(x = 0, y + height, y)

        g.DrawLines(pen, New PointF() {New PointF(x + 10, y + height - 20), New PointF(x + width - 20, y + height - 20), New PointF(x + 10, y + 10), New PointF(x + width - 20, y + 10)})
        g.DrawString("Lines", Me.Font, blackBrush, New RectangleF(x, y, width, height), format)
        x = IIf(x > Me.ClientRectangle.Width - 2 * width, 0, x + width)
        y = IIf(x = 0, y + height, y)

        g.FillPie(fillBrush, x + 10, y + 10, width - 20, height - 20, 180, 180)
        g.DrawPie(pen, x + 10, y + 10, width - 20, height - 20, 180, 180)
        g.DrawString("Pie", Me.Font, blackBrush, New RectangleF(x, y, width, height), format)
        x = IIf(x > Me.ClientRectangle.Width - 2 * width, 0, x + width)
        y = IIf(x = 0, y + height, y)

        g.FillPolygon(fillBrush, New PointF() {New PointF(x + 10, y + height - 20), New PointF(x + width - 20, y + height - 20), New PointF(x + 10, y + 10), New PointF(x + width - 20, y + 10)})
        g.DrawPolygon(pen, New PointF() {New PointF(x + 10, y + height - 20), New PointF(x + width - 20, y + height - 20), New PointF(x + 10, y + 10), New PointF(x + width - 20, y + 10)})
        g.DrawString("Polygon", Me.Font, blackBrush, New RectangleF(x, y, width, height), format)
        x = IIf(x > Me.ClientRectangle.Width - 2 * width, 0, x + width)
        y = IIf(x = 0, y + height, y)

        g.FillRectangle(fillBrush, x + 10, y + 10, width - 20, height - 20)
        g.DrawRectangle(pen, x + 10, y + 10, width - 20, height - 20)
        g.DrawString("Rectangle", Me.Font, blackBrush, New RectangleF(x, y, width, height), format)
        x = IIf(x > Me.ClientRectangle.Width - 2 * width, 0, x + width)
        y = IIf(x = 0, y + height, y)
          End Sub
End Class

           
       

Register Delegates and call them

Sunday, July 26th, 2009

Imports System

Public Class MainClass
    Public Delegate Sub CallBackFunc()
    Private Shared m_cbFunc As CallBackFunc

    Public Shared Sub Main()
        RegisterDelegate(AddressOf CallBackHandler1)
        RegisterDelegate(AddressOf CallBackHandler2)
        CallDelegates()
    End Sub

    Shared Public Sub CallBackHandler1()
        Console.WriteLine("Callback 1 returned ")
    End Sub

    Shared Public Sub CallBackHandler2()
        Console.WriteLine("Callback 2 returned ")
    End Sub

    Shared Public Sub RegisterDelegate(ByRef cbFunc As CallBackFunc)
        m_cbFunc = CType(System.Delegate.Combine(m_cbFunc, cbFunc), CallBackFunc)
    End Sub
  
    Shared Public Sub CallDelegates()
        m_cbFunc()
    End Sub

End Class

           
       

Structure with Constructor

Sunday, July 26th, 2009

Imports System

Public Class MainClass
    
    Shared Sub Main()
        Dim loc1 As New Location(200, 300)

        Console.WriteLine("Loc1 location: {0}", loc1)

        Dim loc2 As New Location(  )
        Console.WriteLine("Loc2 location: {0}", loc2)

        myFunc(loc1)

        Console.WriteLine("Loc1 location: {0}", loc1)
    End Sub

    Shared Public Sub myFunc(ByVal loc As Location)
        loc.XVal = 50
        loc.YVal = 100
        Console.WriteLine("Loc1 location: {0}", loc)
    End Sub

End Class

      ‘ declare a Structure named Location
     Public Structure Location
         ‘ the structure has private data
         Private myXVal As Integer
         Private myYVal As Integer

         ‘ constructor
         Public Sub New( _
            ByVal xCoordinate As Integer, ByVal yCoordinate As Integer)
             myXVal = xCoordinate
             myYVal = yCoordinate
         End Sub ‘New

         ‘ property

         Public Property XVal(  ) As Integer
             Get
                 Return myXVal
             End Get
             Set(ByVal Value As Integer)
                 myXVal = Value
             End Set
         End Property

         Public Property YVal(  ) As Integer
             Get
                 Return myYVal
             End Get
             Set(ByVal Value As Integer)
                 myYVal = Value
             End Set
         End Property

         ‘ Display the structure as a String
         Public Overrides Function ToString(  ) As String
             Return String.Format("{0}, {1}", xVal, yVal)
         End Function ‘ToString
     End Structure ‘Location

           
       

Read XML schema with DataSet and output its structure

Saturday, July 25th, 2009

Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Collections
Imports System.Data

Public Class MainClass
    
    Shared Sub Main()
        Dim MyDS As New DataSet()
        MyDS.ReadXmlSchema("book.xdr")

        Console.WriteLine("Book Structure:")
        Dim Table As DataTable
        For Each Table In MyDS.Tables
            Console.WriteLine("Table {0}", Table.TableName)
            Dim Column As DataColumn
            For Each Column In Table.Columns
                Console.WriteLine("  {0}", Column.ColumnName)
            Next
        Next
    End Sub
End Class

‘book.xdr
‘<?xml version = "1.0"?>

‘<Schema xmlns = "urn:schemas-microsoft-com:xml-data">
‘   <ElementType name = "title" content = "textOnly" 
‘      model = "closed" />

‘   <ElementType name = "book" content = "eltOnly" model = "closed">
‘      <element type = "title" minOccurs = "1" maxOccurs = "1" />
‘   </ElementType>

‘   <ElementType name = "books" content = "eltOnly" model = "closed">
‘      <element type = "book" minOccurs = "0" maxOccurs = "*" />
‘   </ElementType>
‘</Schema>

           
       

Bind String Array into TextBox

Monday, July 20th, 2009

Imports System
Imports System.Data
Imports System.Windows.Forms

public class MainClass
   Shared Sub Main()
       Dim form1 As Form = New Form1
       Application.Run(form1)
   End Sub
End Class

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        ‘This call is required by the Windows Form Designer.
        InitializeComponent()

        ‘Add any initialization after the InitializeComponent() call

    End Sub

    ‘Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ‘Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    ‘NOTE: The following procedure is required by the Windows Form Designer
    ‘It can be modified using the Windows Form Designer.  
    ‘Do not modify it using the code editor.
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.TextBox1 = New System.Windows.Forms.TextBox
      Me.SuspendLayout()
      ‘
      ‘TextBox1
      ‘
      Me.TextBox1.Location = New System.Drawing.Point(8, 8)
      Me.TextBox1.Name = "TextBox1"
      Me.TextBox1.Size = New System.Drawing.Size(160, 20)
      Me.TextBox1.TabIndex = 0
      Me.TextBox1.Text = "TextBox1"
      ‘
      ‘Form1
      ‘
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(184, 37)
      Me.Controls.Add(Me.TextBox1)
      Me.Name = "Form1"
      Me.Text = "Form1"
      Me.ResumeLayout(False)

   End Sub

#End Region

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim carDetails() As String = _
         {"Ferrari", "Red", "F355", "1965", "?25,000"}
      TextBox1.DataBindings.Add("text", carDetails, "")
   End Sub
End Class