Enumerated Property in Custom User Control

NateDogg

Member
Joined
Jul 27, 2005
Messages
8
Programming Experience
5-10
[RESOLVED] Enumerated Property in Custom User Control

Hey all,

I'm new to the forums and .NET in general so hopefully this isn't TOO stupid of a question.

I'm trying to create a custom control to help with some user input validation. I basically want to provide an enumerated type for a text box so that developers can let me know what kind of validation they need. In VB6, it was as simple as this (simplified code):

Public Enum EInputType
InputText = 0
InputDollar = 1
End Enum

Private minputType as EInputType

Public Property Get InputType as EInputType
InputType = minputType
End Property

Public Property Let InputType(inValue as EInputType)
minputType = inValue
End Property

At design time, the developer would then be given the dropdown list of options (text or dollar) to choose from.

So I'm trying this in .NET, the only difference being the syntax of the property let/get, but the custom property will NOT show at design time though I can set it at run time. Anyone know what gives? I tried the "DesignerSerializationVisibility" property but that didn't provide any results, and I even changed the name of the property just in case "InputType" was a reserved word of some kind.

Any help would be appreciated.

Thanks,
Nathan
 
Last edited:
You need to give your property a Category attribute so it knows where in the Properties window to be displayed. Check the help for the CategoryAttribute class for more info. You can use one of the existing common categories or you can define your own if it is appropriate. Let me know if this doesn't work and I'll investigate further, but I'm pretty sure that's all I had to do.
 
Thanks for the quick response, but I couldn't get it to work. I tried all of the following:

<Category("Behavior")> _
Public Property...

<CategoryAttribute("Behavior")> _
Public Property...

<Category("Design")> _
Public Property...

<CategoryAttribute("Design")> _
Public Property...

<Category("Misc")> _
Public Property...

Nate

 
I just created a Control Library project with a single class with this code:
VB.NET:
Imports System.ComponentModel

Public Class TestBox
	Inherits TextBox

	Public Enum TestValues
		Hello
		Goodbye
		World
	End Enum

	Private m_TestValue As TestValues

	Public Property TestValue() As TestValues
		Get
			Return Me.m_TestValue
		End Get
		Set(ByVal Value As TestValues)
			Me.m_TestValue = Value
		End Set
	End Property

End Class
and when I put the control in the Toolbox and added an instance to a form the TestValue property was automatically displayed in the "Misc" section at the bottom of the Properties window. When I added a Category attribute like this:
VB.NET:
    <Category("Data")> Public Property TestValue() As TestValues
the property appeared in the Data section of the Properties window instead. I then changed the category to "Design" and recompiled the two projects and the property had moved accordingly. I don't know why it's not working for you, but I would suggest that you remove the control from the Toolbox, along with any other controls from the same assembly, then re-add them to ensure that any information for the assembly is up to date.
 
That is the result that I would have expected, but I can't seem to produce it. I thought the same thing about my compile not being up to date, so I even added a readonly property that just showed a contstant "TEST", and that one showed up in the "misc" list just fine.

Today I found that my project type was actually a dll (I had originally started the project to do some other things). I started a brand new control project, copied some code across, and compiled (using a different name) Then I added the reference to a brand new project. My property will still not show.

2 things to note that may be causing my problem:
1) I am using VS 2005 Beta 2.
2) I am adding the reference using "browse" (btw - how do I get my components to show up in the .NET list?)

Thanks again for all the help,

Nathan
 
I just tried to create a new control with different naming in case I was running into a conflict of some type. Now I get a very wierd error when I try to inherit from the textbox class. Here is the code:

VB.NET:
[size=2][color=#0000ff]Imports[/color][/size][size=2] System.ComponentModel

[/size][size=2][color=#0000ff]Public[/color][/size][size=2] [/size][size=2][color=#0000ff]Class[/color][/size][size=2] TempControl1

[/size][size=2][color=#0000ff]Inherits[/color][/size][size=2] TextBox

 

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Class

[/color][/size]

The error is:
Base class 'System.Windows.Forms.TextBox' specified for class 'TempControl1' cannot be different from the base class 'System.Windows.Forms.Usercontrol' of one of its other partial types.

What's funny is that as "error correction options", it lists:
Change class 'TempControl1' to inherit from System.Windows.Forms.Textbox or .UserControl

When I change it to System.Windows.Forms.Textbox it gives me the same error :p Very strange since my original control inherits from textbox just fine.
 
Well I finally got it to work! YAY!

I had to create a brand new control with no other properties - maybe my class was just broken somehow?!?! Not sure what fixed it, but whatever - it works!

I still can't inherit from textbox, but I can inherit from system.windows.forms.usercontrol so whatever works I guess.

Thanks again!

Nathan
 
Notice that in VB.NET 2005 there is a seperate code file for the designer elements. The file extension is .Designer.vb . If the class name is TempControl1 then you would have a file named TempControl1.vb and TempControl1.Designer.vb
The .Designer.vb file is where you need to change the Inherits statement.
 
Great! Thanks for the tip! I don't see that anywhere in my .NET project though - is editing the actual designer file the only way to get to that?
 
I don't have 2005 installed currently, so I may be incorrect here:
I think you have to select the option to view all project files (there's an icon at the top of the solution explorer). Then I beleive you may need to click the plus icon beside the vb file to expand the node in order to see the .Designer.vb file.
 
Add Control in Windows forms vb.net

Good afternoon

As I do using Visual Basic.net to create a new control inheriting the properties of the textbox where I will be creating a new property inside of Visual Studio 2005.

Example
Property CLIMBS could have the following values:
1 - small
2 - medium
3 - big

It is at once I want to thank its help.

Respectfully
Claudinei
nei@brazul.com.br

 
Claudinei said:
Good afternoon

As I do using Visual Basic.net to create a new control inheriting the properties of the textbox where I will be creating a new property inside of Visual Studio 2005.

Example
Property CLIMBS could have the following values:
1 - small
2 - medium
3 - big

It is at once I want to thank its help.

Respectfully
Claudinei
nei@brazul.com.br

Take a look at post #4. It shows you exactly what to do. Usually the actual values of the enumerated constants don't matter; just that they are distinct. If you do want each one to be equal to a specific value the you just assign that value in the definition:
VB.NET:
Public Enum Climb
    Small = 1
    Medium = 2
    Large = 3
End Enum
Like I said, unless there is a specific reason you want to use these numeric values, like they are already stored in a database, then don't worry about the actual values. Just accept the defaults as you should normally not ever have to specify the numeric value anyway.
 
Back
Top