|
|
Title | Make a TextBox control that displays an image when its value is modified in Visual Basic .NET |
Description | This example shows how to make a TextBox control that displays an image when its value is modified in Visual Basic .NET. |
Keywords | TextBox, modified, data dirty, changed, text, image, bitmap |
Categories | Controls |
|
|
Many developers don't realize that controls such as TextBox can contain other controls. Create the TextBoxModified class and make it inherit from TextBox. At design time, add a PictureBox to the control. Anchor the PictureBox to the control's top right corner and give it an image. The following initialization code moves the PictureBox so it sits in the control's upper right corner.
The TextModified property gets or sets the PictureBox's Visible property. In other words, if the text is modified, it displays the picture. The program can hide the picture by setting TextModified to False.
When the TextBox's text is changed, the event handler sets TextModified to True so the picture is visible.
|
|
<ToolboxBitmap(GetType(TextBoxModified), _
"TextBoxModifiedTool.bmp")> _
Public Class TextBoxModified
Inherits TextBox
Friend WithEvents picModified As _
System.Windows.Forms.PictureBox
Public Sub New()
InitializeComponent()
InitPicture()
End Sub
Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = _
New System.Resources.ResourceManager(GetType(TextBoxModified))
Me.picModified = New System.Windows.Forms.PictureBox
'
'picModified
'
Me.picModified.Anchor = _
CType((System.Windows.Forms.AnchorStyles.Top Or _
System.Windows.Forms.AnchorStyles.Right), _
System.Windows.Forms.AnchorStyles)
Me.picModified.Image = _
CType(resources.GetObject("picModified.Image"), _
System.Drawing.Image)
Me.picModified.Location = New _
System.Drawing.Point(17, 17)
Me.picModified.Name = "picModified"
Me.picModified.Size = New System.Drawing.Size(16, _
16)
Me.picModified.SizeMode = _
System.Windows.Forms.PictureBoxSizeMode.AutoSize
Me.picModified.TabIndex = 0
Me.picModified.TabStop = False
End Sub
Private Sub InitPicture()
picModified.Left = Me.Width - _
picModified.Size.Width - 4
picModified.Top = Me.Height - _
picModified.Size.Height - 4
TextModified = True
Me.Controls.Add(picModified)
End Sub
Public Property TextModified() As Boolean
Get
If picModified Is Nothing Then Return False
Return picModified.Visible
End Get
Set(ByVal value As Boolean)
picModified.Visible = value
End Set
End Property
' Show the pencil.
Private Sub TextBoxModified_TextChanged(ByVal sender As _
Object, ByVal e As System.EventArgs) Handles _
MyBase.TextChanged
If Not TextModified Then TextModified = True
End Sub
End Class
|
|
|
|
|
|