Custom Control

G

Guest

I am creating a custom control with a property that is a Class I created
that inherits from CollectionBase. When I add this control to a test
application I can edit the property but the Set block of code does not get
called and the data does not get saved. What I am forgetting to do?

Peter
 
B

Bob Powell [MVP]

Have you tagged your property with the DesignerSerializationVisibility
attribute? You should use DesignerSerializationVisibility.Content.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
G

Guest

I think i found where to put it. But I still don't get the message box to
appear when i exit the collection editor. this is the code

<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _

Property Headers() As ColumnDetailsCollection

Get

Return cdColumnInfo

End Get

Set(ByVal Value As ColumnDetailsCollection)

MsgBox("HERE")

cdColumnInfo = Value

End Set

End Property
 
B

Bob Powell [MVP]

Try setting the version of the control to some fixed number in the
AssemblyInfo.vb file. Remove the asterisks from the version number and
recompile.

There is a known issue with the version number of the control and the
resources stored in the application.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
G

Guest

That did not change anything. The Set block is still not being called when
the collection editor closes.
 
B

Bob Powell [MVP]

B

Bob Powell [MVP]

V

Vlado B.

That did not change anything. The Set block is still not being called when
the collection editor closes.


Hello Peter,

you do not need set block for collection at all, because collection editor
doesn't use set block. Collection editor AFAIK uses only get block to get
the collection.

You need set block in situation when you want to set your property, but you,
in this case, have a property whose type is collection and you are setting
the items of that collection.

With your set block you can set a completly another collection to your
property like this:

ColumnDetailsCollection cdc = new ColumnDetailsCollection();
cdc.Add(...);
cdc.Add(...);

yourCustomControl1.Headers = cdc;

and here you will get MessageBox.


Hope this helps,

Vlado
 
G

Guest

Here is the code for the three files. I want to call the CreateColumns
subroutine when the collection editor exits.



Public Class ColumnDetails

Private sHeaderText As String

Private iColumnWidth As Integer

Public Property Header() As String

Get

Return (sHeaderText)

End Get

Set(ByVal Value As String)

sHeaderText = Value

End Set

End Property

Public Property ColumnWidth() As Integer

Get

Return iColumnWidth

End Get

Set(ByVal Value As Integer)

iColumnWidth = Value

End Set

End Property

Public Sub New()

iColumnWidth = 100

sHeaderText = "Header"

End Sub

End Class





Public Class ColumnDetailsCollection

Inherits CollectionBase

Public ReadOnly Property Item(ByVal index As Integer) As ColumnDetails

Get

Return CType(List.Item(index), ColumnDetails)

End Get

End Property

Public Sub Add(ByVal cd As ColumnDetails)

List.Add(cd)

End Sub

Public Sub Remove(ByVal index As Integer)

If index > Count - 1 Or index < 0 Then

System.Windows.Forms.MessageBox.Show("Index not valid!")

Else

List.RemoveAt(index)

End If

End Sub

Public Sub New()

List.Clear()

End Sub

End Class





Imports System.ComponentModel

Imports System.ComponentModel.Design

Public Class ctlSearch

Inherits System.Windows.Forms.UserControl

Private cdColumnInfo As ColumnDetailsCollection

<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
_

Property Headers() As ColumnDetailsCollection

Get

Return cdColumnInfo

End Get

Set(ByVal Value As ColumnDetailsCollection)

Dim cdc As New ColumnDetailsCollection

For i As Integer = 0 To Value.Count - 1

cdc.Add(Value.Item(i))

Next i

MsgBox("HERE")

cdColumnInfo = cdc

CreateColumns

End Set

End Property



Public Sub CreateColumns()

Dim i As Integer

Dim SizeTotal As Integer

Dim TempCol As ctlSearchColumn

Me.Controls.Clear()

If cdColumnInfo Is Nothing Then Exit Sub

SizeTotal = 0

For i = 0 To cdColumnInfo.Count - 1

TempCol = New ctlSearchColumn

TempCol.HeaderText = cdColumnInfo.Item(i).Header

TempCol.Width = cdColumnInfo.Item(i).ColumnWidth

TempCol.Left = SizeTotal

SizeTotal = SizeTotal + TempCol.Width + 10

Me.Controls.Add(TempCol)

Next

End Sub

End Class
 
V

Vlado B.

Hello Peter!

As I said, the Set block of property Headers() will not be called at all
when you close the collection editor in property grid. It will be called
only from code if you do something like this:

Public Class someForm

Private ctls As New ctlSearch

Private Sub someSub()

Dim newCDC As New ColumnDetailsCollection

newCDC.Add(someItem)
newCDC.Add(someItem)
...

ctls.Headers = newCDC <===== here is where you go in
Set block of property Headers, and so CreateColumns will be called

End Sub



Also, I think that your Set block of property Headers() can look like this:

Set(ByVal Value As ColumnDetailsCollection)

cdColumnInfo = Value

CreateColumns

End Set


What you, probably, need here is to call moderated CreateColumns method, but
you must call it from collection's Add method after adding the item to List.

Hope this will help.

Vlado

P.S. I work in C# so sorry for possible mistakes in VB code. :)
 
V

Vlado B.

What can I do to call CreateColumns() at design time?


Hello Peter,

as I said in my last post, you can change a little bit your method
CreateColumns and rename it, lets say to CreateColumn, to create one column
at a time. That way you can call your method from collection's Add method
after you call List.Add(item). Maybe you will have to override a
collection's method named OnInsert and call CreateColumns from there.

The other way is to build your own editor (inherit UITypeEditor, override
EditValue and GetEditStyle) and call CreateColumns on closing it.

Vlado
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top