' Project RealEstate, source code file Form1.vb. ' Display information about houses graphically in a picture box. ' If user wants more information, clicking on a house brings up ' a secondary window with more information. Imports System.Data.OleDb 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 Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents mnuPlot As System.Windows.Forms.MenuItem Friend WithEvents picDisplay As System.Windows.Forms.PictureBox Friend WithEvents trkMin As System.Windows.Forms.TrackBar Friend WithEvents trkMax As System.Windows.Forms.TrackBar Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents txtMin As System.Windows.Forms.TextBox Friend WithEvents txtMax As System.Windows.Forms.TextBox Private Sub InitializeComponent() Me.trkMin = New System.Windows.Forms.TrackBar Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.picDisplay = New System.Windows.Forms.PictureBox Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.mnuPlot = New System.Windows.Forms.MenuItem Me.trkMax = New System.Windows.Forms.TrackBar Me.Label3 = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.txtMin = New System.Windows.Forms.TextBox Me.txtMax = New System.Windows.Forms.TextBox CType(Me.trkMin, System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.trkMax, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'trkMin ' Me.trkMin.Location = New System.Drawing.Point(16, 48) Me.trkMin.Maximum = 750 Me.trkMin.Minimum = 50 Me.trkMin.Name = "trkMin" Me.trkMin.Orientation = System.Windows.Forms.Orientation.Vertical Me.trkMin.Size = New System.Drawing.Size(45, 360) Me.trkMin.TabIndex = 0 Me.trkMin.TickFrequency = 50 Me.trkMin.Value = 50 ' 'Label1 ' Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(120, 48) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(80, 24) Me.Label1.TabIndex = 2 Me.Label1.Text = "$750,000" ' 'Label2 ' Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label2.Location = New System.Drawing.Point(120, 384) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(80, 24) Me.Label2.TabIndex = 3 Me.Label2.Text = "$50,000" ' 'picDisplay ' Me.picDisplay.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.picDisplay.Location = New System.Drawing.Point(136, 80) Me.picDisplay.Name = "picDisplay" Me.picDisplay.Size = New System.Drawing.Size(352, 296) Me.picDisplay.TabIndex = 4 Me.picDisplay.TabStop = False ' 'MainMenu1 ' Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuPlot}) ' 'mnuPlot ' Me.mnuPlot.Index = 0 Me.mnuPlot.Text = "Plot Houses" ' 'trkMax ' Me.trkMax.Location = New System.Drawing.Point(72, 48) Me.trkMax.Maximum = 750 Me.trkMax.Minimum = 50 Me.trkMax.Name = "trkMax" Me.trkMax.Orientation = System.Windows.Forms.Orientation.Vertical Me.trkMax.Size = New System.Drawing.Size(45, 360) Me.trkMax.TabIndex = 5 Me.trkMax.TickFrequency = 50 Me.trkMax.Value = 750 ' 'Label3 ' Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label3.Location = New System.Drawing.Point(8, 16) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(40, 24) Me.Label3.TabIndex = 6 Me.Label3.Text = "Min" ' 'Label4 ' Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label4.Location = New System.Drawing.Point(64, 16) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(40, 24) Me.Label4.TabIndex = 7 Me.Label4.Text = "Max" ' 'txtMin ' Me.txtMin.Location = New System.Drawing.Point(8, 408) Me.txtMin.Name = "txtMin" Me.txtMin.Size = New System.Drawing.Size(32, 20) Me.txtMin.TabIndex = 8 Me.txtMin.Text = "" Me.txtMin.TextAlign = System.Windows.Forms.HorizontalAlignment.Right ' 'txtMax ' Me.txtMax.Location = New System.Drawing.Point(64, 408) Me.txtMax.Name = "txtMax" Me.txtMax.Size = New System.Drawing.Size(32, 20) Me.txtMax.TabIndex = 9 Me.txtMax.Text = "" Me.txtMax.TextAlign = System.Windows.Forms.HorizontalAlignment.Right ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(544, 473) Me.Controls.Add(Me.txtMax) Me.Controls.Add(Me.txtMin) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.trkMax) Me.Controls.Add(Me.picDisplay) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.trkMin) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "RealEstate Example" CType(Me.trkMin, System.ComponentModel.ISupportInitialize).EndInit() CType(Me.trkMax, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub #End Region Private c As OleDbConnection Private sc As OleDbCommand Private dr As OleDbDataReader Private rivers As New ArrayList Private houses As New ArrayList Private houseForm As New frmHouse Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load c = New OleDbConnection c.ConnectionString = "" c.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=c:\RealEstate.mdb" c.Open() sc = c.CreateCommand() sc.CommandType = CommandType.Text sc.CommandText = "SELECT * FROM Rivers" dr = sc.ExecuteReader() While dr.Read() rivers.Add(New RiverPoint(dr.GetFloat(0), _ dr.GetFloat(1), dr.GetBoolean(2))) End While dr.Close() txtMin.Text = trkMin.Value txtMax.Text = trkMax.Value End Sub Private Sub picDisplay_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) Handles picDisplay.Paint Dim g As Graphics = e.Graphics Dim p As New Pen(Color.Blue, 3) Dim br As New SolidBrush(Color.Red) Dim r As RiverPoint Dim h As HousePoint Dim xprev, yprev As Single For Each r In rivers If r.StartPoint Then g.DrawLine(p, 10 * r.X, 10 * r.Y, 10 * r.X, 10 * r.Y) Else g.DrawLine(p, 10 * xprev, 10 * yprev, 10 * r.X, 10 * r.Y) End If xprev = r.X yprev = r.Y Next For Each h In houses g.FillEllipse(br, 10 * h.X - 3, 10 * h.Y - 3, 6, 6) Next End Sub Private Sub mnuPlot_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles mnuPlot.Click Dim minPrice, maxPrice As Double minPrice = 1000 * trkMin.Value maxPrice = 1000 * trkMax.Value sc.CommandType = CommandType.Text sc.CommandText = "SELECT * FROM Houses WHERE " & _ minPrice & " < Price And Price < " & maxPrice dr = sc.ExecuteReader() houses.Clear() While dr.Read() houses.Add(New HousePoint(dr.GetFloat(1), dr.GetFloat(2), dr.GetInt32(0))) End While dr.Close() picDisplay.Invalidate() End Sub Private Sub trkMin_Scroll(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles trkMin.Scroll txtMin.Text = trkMin.Value End Sub Private Sub trkMax_Scroll(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles trkMax.Scroll txtMax.Text = trkMax.Value End Sub Private Sub picDisplay_MouseUp(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Handles picDisplay.MouseUp Dim h As HousePoint For Each h In houses If Math.Abs(e.X - 10 * h.X) < 3 And Math.Abs(e.Y - 10 * h.Y) < 3 Then DisplayHouse(h.ID) End If Next End Sub Private Sub DisplayHouse(ByVal theID As Integer) Dim price As Double sc.CommandText = "SELECT * FROM Houses WHERE ID = " & theID dr = sc.ExecuteReader If dr.Read Then houseForm.txtAddress.Text = dr.GetString(3) houseForm.txtCity.Text = dr.GetString(4) houseForm.txtBedrooms.Text = dr.GetFloat(5) houseForm.txtBathrooms.Text = dr.GetFloat(6) price = dr.GetDecimal(8) houseForm.txtPrice.Text = price.ToString("$#,##0.") End If houseForm.ShowDialog() dr.Close() End Sub End Class