LRanger


I have created a class based on the forms.button class. Adding the control to the Toolbox, I can add the control to my forms. The problem is that I have a property called 'Mode' that changes the control's other properties as needed. For example, while in the design window, if I set the Mode property to <something>, I want to automatically set the said control's Enabled property to false. It works because I can run the app and the control is in fact disabled but while in design mode the Enabled property still shows True. I can change just about any other property in the same way and the change is reflected in the control's property window just the Enabled property fails to refresh. I am relatively new to VS 2005 so forgive me if this doesn't make sense but is it me, by design, a bug and if so, any workarounds

Thanks,

Robert



Re: Possible to update a VB 2005 control's 'enabled' property via code while in the IDE environment?

Spidermans_DarkSide - MSP, VSIP


Hi,

While in design mode hit the F4 key on your keyboard or go to the View menu and select the Properties Window.

Each control that you want Enabled to be False with, just highlight it ( or right-click on it and select properties ), for each one you can have True or False set.

I don't think you can see a control as disabled visually on the Form itself via code or otherwise in the IDE in design mode.

It is only shown as Enabled or not in the properties window only.

It only shows as disabled once you run your program - application, so i guess it is "by design" as you put it.

Hope that helps.

Regards,

S_DS






Re: Possible to update a VB 2005 control's 'enabled' property via code while in the IDE environment?

LRanger

Hi S_DS,

Thanks for the response but seeing the properties window or the control itself in a disabled state while in design mode is not the problem. Let's say (for this example) I have button control that has 3 properties, Enabled, Mode and Tabstop. The code for the Mode property would be something like this:

Code Snippet

Public Enum modeType

thismode = 1

thatmode = 0

End Enum

Public buttonMode As Integer = 0

Public Property Mode() As modeType

Get

Return buttonMode

End Get

Set(ByVal Value As modeType)

buttonMode = Value

If buttonMode = modeType.thismode Then

Enabled = False

TabStop = False

Else

Enabled = True

TabStop = True

End If

End Set

End Property

While in design mode I set the Mode property to 1 (thismode) via the properties window. The Tabstop property instantly shows the change (while looking at the properties window) to False but the Enabled property does not. If I run the app at this point, the Enabled property is, in fact, set to False (or at least acts like it) but when I return to design mode it still shows True. With that said, I am wondering why the TabStop property instantly shows the changes (in the properties window) while in desgn mode but the Enabled property does not. It makes me think it is a bug as changing the Enabled property by selecting it while in design mode is possible so why not allow the above code to show the change I hope this makes it a bit clearer, as I said before, I am somewhat new to VS 2005. And thanks again for trying to help.

Robert






Re: Possible to update a VB 2005 control's 'enabled' property via code while in the IDE environment?

Stephen Weatherford MS

The "Enabled" property is treated specially, it's called a shadowed property. The control's designer keeps track of the value itself, and does not pass it on to the control being designed. You wouldn't want the control to actually become disabled at designtime, right

Unfortunately, this means that if you want to control the Enabled property's value at designtime, you'll have to create a control designer yourself and make your Mode property a shadowed property.

You can find more information at http://msdn2.microsoft.com/en-us/library/ms973820.aspx#custdsgnrdotnet_using, but here's a code snippet that should get you started. Be sure to look for the "TODO" comment before running.

Code Snippet

Imports System.ComponentModel

Imports System.Windows.Forms.Design

Public Class MyButtonDesigner

Inherits ControlDesigner 'Need to add reference to System.Design for this

Protected Overrides Sub PreFilterProperties(ByVal properties As System.Collections.IDictionary)

MyBase.PreFilterProperties(properties)

'Replace our Mode property

Dim descriptor As PropertyDescriptor = CType(properties("Mode"), PropertyDescriptor)

If descriptor IsNot Nothing Then

properties("Mode") = TypeDescriptor.CreateProperty(GetType(MyButtonDesigner), descriptor, New Attribute() {})

End If

End Sub

Private Property Mode() As MyButton.modeType

Get

Return ShadowProperties("Mode")

End Get

Set(ByVal value As MyButton.modeType)

'Note this value is not passed to the actual

' control

ShadowProperties("Mode") = value

'Change Enabled (a shadows property) and TabStop (not a shadowed

' property) according to new Mode value.

If value = MyButton.modeType.thismode Then

ShadowProperties("Enabled") = False

Control.TabStop = False

Else

ShadowProperties("Enabled") = True

Control.TabStop = True

End If

End Set

End Property

End Class

'TODO: change the assembly name here

<Designer("WindowsApplication11.MyButtonDesigner")> _

Public Class MyButton

Inherits Button

Public Enum modeType

thismode = 1

thatmode = 0

End Enum

Private buttonMode As Integer = 0

Public Property Mode() As modeType

Get

Return buttonMode

End Get

Set(ByVal Value As modeType)

buttonMode = Value

If buttonMode = modeType.thismode Then

Me.Enabled = False

TabStop = False

Else

Me.Enabled = True

TabStop = True

End If

End Set

End Property

<EditorBrowsable(EditorBrowsableState.Never)> _

Public Shadows Property Enabled() As Boolean

Get

Return MyBase.Enabled

End Get

Set(ByVal value As Boolean)

MyBase.Enabled = value

End Set

End Property

End Class