Why can't I add an Image to a Button

I am working on a Pocket PC app. Using the Mobel Application Development Toolkit and writing it is VB. What I am trying to do is add an image to a button. I notice that there are not any properties for the button to do this (or function, or members). Also there is no help on trying to do this in any other way.

Anyone now why you can't do this?

[349 byte] By [guinea13] at [2008-2-28]
# 2

Or you can look at the VB code snippet under

Smart Devices > Windows Forms Application > Controls and Components > Create a Button with an Image

Below is the sample code

'Button with an image custom control.
Public Class PictureButton
Inherits Control

Private backgroundImg As Image
Private pressedImg As Image
Private pressed As Boolean = False

' Property for the background image to be drawn behind the button text.
Public Property BackgroundImageValue() As Image
Get
Return Me.backgroundImg
End Get
Set(ByVal Value As Image)
Me.backgroundImg = Value
End Set
End Property

' Property for the background image to be drawn behind the button text when
' the button is pressed.
Public Property PressedImageValue() As Image
Get
Return Me.pressedImg
End Get
Set(ByVal Value As Image)
Me.pressedImg = Value
End Set
End Property

' Ivalidate form to cause repaint.
Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
Me.pressed = True
Me.Invalidate()
MyBase.OnMouseDown(e)
End Sub

' When the mouse is released, reset the "pressed" flag
' and invalidate to redraw the button in the un-pressed state.
Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
Me.pressed = False
Me.Invalidate()
MyBase.OnMouseUp(e)
End Sub

' Override the OnPaint method so we can draw the background image and the text.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

If Me.pressed AndAlso (Me.pressedImg IsNot Nothing) Then
e.Graphics.DrawImage(Me.pressedImg, 0, 0)
Else
e.Graphics.DrawImage(Me.backgroundImg, 0, 0)
End If

' Draw the text if there is any.
If Me.Text.Length > 0 Then
Dim size As SizeF = e.Graphics.MeasureString(Me.Text, Me.Font)

' Center the text inside the client area of the PictureButton.
e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), (Me.ClientSize.Width - size.Width) / 2, (Me.ClientSize.Height - size.Height) / 2)
End If

' Draw a border around the outside of the
' control to look like Pocket PC buttons.
e.Graphics.DrawRectangle(New Pen(Color.Black), 0, 0, Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)

MyBase.OnPaint(e)
End Sub
End Class
David

DavidTSoMSFT at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...
# 3
Yes I did look at those. I wonder why they just didn't add the image attribute to the button object.

I did something similar to what you have just a little simplier in code

There not buttons but Picture boxes (The variables can be deceiving).



Private Sub ResetButton_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ResetButton.MouseUp

ResetButton.Image = CType(Res.GetObject("ImageList1.Images8"), System.Drawing.Image)

End Sub

Private Sub ResetButton_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ResetButton.MouseDown

ResetButton.Image = CType(Res.GetObject("ImageList1.Images9"), System.Drawing.Image)

End Sub



guinea13 at 2007-9-9 > top of Msdn Tech,Smart Device Development,Smart Devices VB and C# Projects...