Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

This is mostly testing the waters for me to learn how to improve and finally create my own control components.

 

I had this working fine (it's an example in my Mastering VB.Net book) a few months ago, but I just came back to it and I can't get it working correctly.

 

In the TestApp I created to run it, I can see the properties in the code view and set them without any errors.

 

I can also open the properties window and everything looks fine. But when I double click on the Mandatory property to click False to True, it doesn't work. When I click on one of the color, it lets me choose a color, it remembers it if I open up the color chooser again, but the little color box stays white unless I change the backcolor manually.

 

When running the TestApp, even setting the colors via code, it just assumes that the EnterFocusColor and LostFocusColor are both white, even if I set something else.

 

If anyone can see a mistake in my code, I'd be appreciative :)

 

Imports System.ComponentModel

Public Class TestText
   Inherits System.Windows.Forms.TextBox

#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

   'UserControl1 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()
       '
       'UserControl1
       '
       Me.Name = "UserControl1"

   End Sub

#End Region

   Dim _mandatory As Boolean
   Dim _enterFocusColor, _leaveFocusColor As Color
   Dim _mandatoryColor As Color

#Region "Properties"
   <Description("Indicates whether the control can be left blank"), _
       Category("Appearance")> _
   Property Mandatory() As Boolean
       Get
           Mandatory = _mandatory
       End Get
       Set(ByVal Value As Boolean)
           _mandatory = Mandatory
       End Set
   End Property

   <Description("Indicates the backcolor when control has focus"), _
       Category("Appearance")> _
   Property EnterFocusColor() As Color
       Get
           EnterFocusColor = _enterFocusColor
       End Get
       Set(ByVal Value As Color)
           _enterFocusColor = EnterFocusColor
       End Set
   End Property

   <Description("Indicates the backcolor when control looses focus"), _
   Category("Appearance")> _
Property LeaveFocusColor() As Color
       Get
           LeaveFocusColor = _leaveFocusColor
       End Get
       Set(ByVal Value As Color)
           _leaveFocusColor = LeaveFocusColor
       End Set
   End Property

   <Description("Indicates the backcolor when left empty an is indicated as mandatory"), _
       Category("Appearance")> _
Property MandatoryColor() As Color
       Get
           MandatoryColor = _mandatoryColor
       End Get
       Set(ByVal Value As Color)
           _mandatoryColor = MandatoryColor
       End Set
   End Property
#End Region

   Private Sub TestText_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Enter
       Me.BackColor = _enterFocusColor
   End Sub

   Private Sub TestText_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseLeave
       If Trim(Me.Text).Length = 0 And _mandatory Then
           Me.BackColor = _mandatoryColor
       Else
           Me.BackColor = _leaveFocusColor
       End If
   End Sub
End Class

Posted
You need to change the property set to use the value being assigned...

Property Mandatory() As Boolean
       Get
           Mandatory = _mandatory
       End Get
       Set(ByVal Value As Boolean)
           _mandatory = Value
       End Set
   End Property

 

I feel like burning the book now. I glanced at how to write a property, wrote it like that with "Value", looked in the book and saw it wanted you to do the "_mandatory = Mandatory". Figured it was something unique to Properties or something.

 

Then again the book also had an error were _manditoryColor was defined as an array (dim _mandatoryColor() as Color), which I had to change. Just sounds like poorly tested code or bad editing... not very promising when you're trying to learn from a book and errors are popping up.

 

Thank you :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...