help

jawadh90

Member
Joined
Jun 19, 2003
Messages
18
peeps, i am trying to put an image on the form without using the background property nor using a picturebox. i got this problem solved by using the form onpaint overrides. the problem is, i need to change that image when i click on the form as well as when i click a button. any suggestions. this is what i have so far. (just add in any of you images instead). also, i am confused if i should use the base class click or the overridable onclick event

Imports System.Drawing.Drawing2D
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 cmdDigitalOff As System.Windows.Forms.Button
Friend WithEvents cmdDigitalOn As System.Windows.Forms.Button
Friend WithEvents Label9 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.cmdDigitalOff = New System.Windows.Forms.Button()
Me.cmdDigitalOn = New System.Windows.Forms.Button()
Me.Label9 = New System.Windows.Forms.Label()
Me.SuspendLayout()

cmdDigitalOff

Me.cmdDigitalOff.BackColor = System.Drawing.SystemColors.Control
Me.cmdDigitalOff.Location = New System.Drawing.Point(128, 312)
Me.cmdDigitalOff.Name = "cmdDigitalOff"
Me.cmdDigitalOff.Size = New System.Drawing.Size(56, 20)
Me.cmdDigitalOff.TabIndex = 60
Me.cmdDigitalOff.Text = "Off"

cmdDigitalOn

Me.cmdDigitalOn.BackColor = System.Drawing.SystemColors.Control
Me.cmdDigitalOn.Location = New System.Drawing.Point(48, 312)
Me.cmdDigitalOn.Name = "cmdDigitalOn"
Me.cmdDigitalOn.Size = New System.Drawing.Size(56, 20)
Me.cmdDigitalOn.TabIndex = 58
Me.cmdDigitalOn.Text = "On"

Label9

Me.Label9.Location = New System.Drawing.Point(8, 8)
Me.Label9.Name = "Label9"
Me.Label9.TabIndex = 59
Me.Label9.Text = "Picture On/Off"

Form1

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(232, 341)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdDigitalOff, Me.cmdDigitalOn, Me.Label9})
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Dim onSwitch, offSwitch As Bitmap
Dim gpOn, gpOff As Graphics
Dim bool As Boolean = True

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
If bool Then
e.Graphics.DrawImage(New Bitmap(onSwitch), 10, 10, 200, 200)
bool = False
Else
e.Graphics.DrawImage(New Bitmap(offSwitch), 10, 10, 200, 200)
bool = True
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
onSwitch = New Bitmap(Image.FromFile(Application.StartupPath & ("\Switch1.gif")))
offSwitch = New Bitmap(Image.FromFile(Application.StartupPath & ("\Switch2.gif")))

gpOn = Graphics.FromImage(onSwitch)
gpOff = Graphics.FromImage(offSwitch)
End Sub

Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
If bool Then
gpOn.DrawImage(onSwitch, 0, 0, 200, 200)
bool = False
Else
gpOff.DrawImage(offSwitch, 0, 0, 200, 200)
bool = True
End If
End Sub

Private Sub cmdDigitalOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDigitalOn.Click
gpOn.DrawImage(onSwitch, 0, 0, 200, 200)
End Sub

Private Sub cmdDigitalOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDigitalOff.Click
gpOff.DrawImage(offSwitch, 0, 0, 200, 200)
End Sub

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
If bool Then

Else

End If
End Sub
End Class
 
To the last part, add the code:

[VB]
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
Dim G as Graphics = Me.CreateGraphics
G.Clear(SystemColors.Window)
If bool Then
G.DrawImage(OnSwitch, New Point(0, 0))
Else
G.DrawImage(OffSwitch, New Point(0, 0))
End If
bool = Not bool toggle the flag
G.Dispose()
End Sub
[/VB]
 
Back
Top