Para Eduardo Morcillo (Editor de tipos)

19/02/2005 - 18:10 por Pedro Rivera | Informe spam
Eduardo Morcillo: Gracias por explicarme cómo hacer el
editor de tipos en Visual Basic .Net. Llevaba un buen
tiempo buscando la manera y de no ser por ti, así
estuviera todavía: Buscando.

Ya logré hacer que se muestre el editor y funciona casi a
la perfección, el casi es que no logro hacer que recuerde
su valor. Por ejemplo, la estructura tiene dos
propiedades que son Entero y Cadena cuyos valores
iniciales son 0 y "", si le asigno valores de 33 y "Mi
Nombre" respectivamente en la ventana de propiedades, y
después cierro la solución, al abrir nuevamente la
solución, los valores son 0 y "".

Por último, una pregunta: ¿Tomaste un curso con
Microsoft, trabajas(te) para ellos o dónde conseguiste
toda esa información que tienes sobre VB Net?
Te dejo el código de lo que estoy haciendo por si es que
puedes ayudarme otra vez.

Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Design



CONTROLX

Public Class ControlX
Inherits System.Windows.Forms.UserControl
Private p As New Prueba("Vacío", 88)

#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

'UserControl 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()
'
'ControlX
'
Me.BackColor = System.Drawing.Color.FromArgb(CType
(192, Byte), CType(0, Byte), CType(0, Byte))
Me.Font = New System.Drawing.Font("Trebuchet MS",
9.75!, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Name = "ControlX"
Me.Size = New System.Drawing.Size(200, 200)

End Sub

#End Region

Public Property MiPrueba() As Prueba
Get
Return p
End Get
Set(ByVal Valor As Prueba)
p = Valor
End Set
End Property

End Class













ESTRUCTURA


<TypeConverter(GetType(ExpandableObjectConverter)), Editor
(GetType(MiEditor), GetType(UITypeEditor))> _
Public Structure Prueba

Private MiTexto As String
Private MiEntero As Integer

#Region " Constructor y propiedades"

Public Sub New(ByVal t As String, ByVal i As Integer)
MiTexto = t
MiEntero = i
End Sub

Public Property Texto() As String
Get
Return MiTexto
End Get
Set(ByVal Valor As String)
MiTexto = Valor
End Set
End Property

Public Property Entero() As Integer
Get
Return MiEntero
End Get
Set(ByVal Valor As Integer)
MiEntero = Valor
End Set
End Property

#End Region

End Structure













FORMULARIO


Public Class Form1
Inherits System.Windows.Forms.Form
Private MiPrueba As Prueba

#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

'Form 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.
Friend WithEvents btnAceptar As
System.Windows.Forms.Button
Friend WithEvents btnCancelar As
System.Windows.Forms.Button
Friend WithEvents txtTexto As
System.Windows.Forms.TextBox
Friend WithEvents txtEntero As
System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.txtTexto = New System.Windows.Forms.TextBox
Me.btnAceptar = New System.Windows.Forms.Button
Me.btnCancelar = New System.Windows.Forms.Button
Me.txtEntero = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'txtTexto
'
Me.txtTexto.Location = New System.Drawing.Point
(64, 40)
Me.txtTexto.Name = "txtTexto"
Me.txtTexto.Size = New System.Drawing.Size(144,
20)
Me.txtTexto.TabIndex = 0
Me.txtTexto.Text = "Texto"
'
'btnAceptar
'
Me.btnAceptar.BackColor =
System.Drawing.Color.Lime
Me.btnAceptar.DialogResult =
System.Windows.Forms.DialogResult.OK
Me.btnAceptar.Location = New System.Drawing.Point
(24, 192)
Me.btnAceptar.Name = "btnAceptar"
Me.btnAceptar.TabIndex = 1
Me.btnAceptar.Text = "Aceptar"
'
'btnCancelar
'
Me.btnCancelar.BackColor =
System.Drawing.Color.Red
Me.btnCancelar.DialogResult =
System.Windows.Forms.DialogResult.Cancel
Me.btnCancelar.ForeColor =
System.Drawing.Color.White
Me.btnCancelar.Location = New System.Drawing.Point
(144, 192)
Me.btnCancelar.Name = "btnCancelar"
Me.btnCancelar.TabIndex = 2
Me.btnCancelar.Text = "Cancelar"
'
'txtEntero
'
Me.txtEntero.Location = New System.Drawing.Point
(64, 96)
Me.txtEntero.Name = "txtEntero"
Me.txtEntero.TabIndex = 3
Me.txtEntero.Text = "0"
'
'Form1
'
Me.AcceptButton = Me.btnAceptar
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.CancelButton = Me.btnCancelar
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.txtEntero)
Me.Controls.Add(Me.btnCancelar)
Me.Controls.Add(Me.btnAceptar)
Me.Controls.Add(Me.txtTexto)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Public Property Probar() As Prueba
Get
MiPrueba = New Prueba(Me.txtTexto.Text,
Me.txtEntero.Text)
Return MiPrueba
End Get
Set(ByVal Valor As Prueba)
MiPrueba = Valor
End Set
End Property

Private Sub Cerrar(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnAceptar.Click,
btnCancelar.Click
MiPrueba.Texto = Me.txtTexto.Text
MiPrueba.Entero = Convert.ToInt32
(Me.txtEntero.Text)
Me.Close()
End Sub

End Class













Imports System.Windows.Forms.Design
Public Class MiEditor
Inherits System.Drawing.Design.UITypeEditor
Dim f As New Form1

Public Overloads Overrides Function GetEditStyle
(ByVal context As
System.ComponentModel.ITypeDescriptorContext) As
System.Drawing.Design.UITypeEditorEditStyle
Return
System.Drawing.Design.UITypeEditorEditStyle.Modal
End Function

Public Overloads Overrides Function EditValue(ByVal
context As System.ComponentModel.ITypeDescriptorContext,
ByVal provider As System.IServiceProvider, ByVal valor As
Object) As Object
Dim MiEditor As IWindowsFormsEditorService
MiEditor = CType(provider.GetService(GetType
(IWindowsFormsEditorService)), IWindowsFormsEditorService)
f.txtTexto.Text = CType(valor, Prueba).Texto
f.txtEntero.Text = CType(valor, Prueba).Entero
MiEditor.ShowDialog(f)
If f.DialogResult = Windows.Forms.DialogResult.OK
Then
Return f.Probar
Else
Return valor
End If
End Function

End Class
 

Leer las respuestas

#1 Eduardo A. Morcillo [MS MVP VB]
21/02/2005 - 01:09 | Informe spam
Aqui va un ejemplo completo:

Option Explicit On
Option Strict On

Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Drawing.Design
Imports System.Reflection
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

Public Class MiControl
Inherits UserControl

Private _miPropiedad As MiEstructura

Public Property MiPropiedad() As MiEstructura
Get
Return _miPropiedad
End Get
Set(ByVal value As MiEstructura)
_miPropiedad = value
End Set
End Property

End Class

< _
TypeConverter(GetType(MiEstructuraConverter)), _
Editor(GetType(MiEstructuraEditor), GetType(UITypeEditor)) _
_


Public Structure MiEstructura

Private _texto As String
Private _entero As Integer

Sub New(ByVal texto As String, ByVal entero As Integer)
_texto = texto
_entero = entero
End Sub

Public Property Texto() As String
Get
Return _texto
End Get
Set(ByVal Value As String)
_texto = Value
End Set
End Property

Public Property Entero() As Integer
Get
Return _entero
End Get
Set(ByVal Value As Integer)
_entero = Value
End Set
End Property

Public Overrides Function ToString() As String
Return """" & _texto & """, " & _entero
End Function

End Structure

Public Class MiEstructuraConverter
Inherits ExpandableObjectConverter

Public Overloads Overrides Function CanConvertTo( _
ByVal context As ITypeDescriptorContext, _
ByVal destinationType As Type) As Boolean

If destinationType Is GetType(InstanceDescriptor) Then
Return True
End If

Return MyBase.CanConvertTo(context, destinationType)

End Function

Public Overloads Overrides Function ConvertTo( _
ByVal context As System.ComponentModel.ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo, _
ByVal value As Object, _
ByVal destinationType As System.Type) As Object

If TypeOf value Is MiEstructura Then

If destinationType Is GetType(InstanceDescriptor) Then

Dim obj As MiEstructura = DirectCast(value, MiEstructura)
Dim parametros() As Type = New Type() {GetType(String),
GetType(Integer)}
Dim constructor As ConstructorInfo GetType(MiEstructura).GetConstructor(parametros)

If Not constructor Is Nothing Then
Return New InstanceDescriptor(constructor, New Object()
{obj.Texto, obj.Entero})
End If

End If

End If

Return MyBase.ConvertTo(context, culture, value, destinationType)

End Function

End Class

Public Class MiEstructuraEditor
Inherits UITypeEditor

Private Class Form
Inherits System.Windows.Forms.Form

#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

'Form 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.
Friend WithEvents textoLabel As System.Windows.Forms.Label
Friend WithEvents enteroLabel As System.Windows.Forms.Label
Friend WithEvents aceptar As System.Windows.Forms.Button
Friend WithEvents cancelar As System.Windows.Forms.Button
Friend WithEvents texto As System.Windows.Forms.TextBox
Friend WithEvents entero As System.Windows.Forms.TextBox

Private Sub InitializeComponent()
Me.textoLabel = New System.Windows.Forms.Label
Me.enteroLabel = New System.Windows.Forms.Label
Me.aceptar = New System.Windows.Forms.Button
Me.cancelar = New System.Windows.Forms.Button
Me.texto = New System.Windows.Forms.TextBox
Me.entero = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'textoLabel
'
Me.textoLabel.AutoSize = True
Me.textoLabel.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.textoLabel.Location = New System.Drawing.Point(16, 8)
Me.textoLabel.Name = "textoLabel"
Me.textoLabel.Size = New System.Drawing.Size(36, 16)
Me.textoLabel.TabIndex = 1
Me.textoLabel.Text = "Texto:"
'
'enteroLabel
'
Me.enteroLabel.AutoSize = True
Me.enteroLabel.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.enteroLabel.Location = New System.Drawing.Point(12, 32)
Me.enteroLabel.Name = "enteroLabel"
Me.enteroLabel.Size = New System.Drawing.Size(41, 16)
Me.enteroLabel.TabIndex = 2
Me.enteroLabel.Text = "Entero:"
'
'aceptar
'
Me.aceptar.DialogResult = System.Windows.Forms.DialogResult.OK
Me.aceptar.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.aceptar.Location = New System.Drawing.Point(124, 56)
Me.aceptar.Name = "aceptar"
Me.aceptar.TabIndex = 3
Me.aceptar.Text = "Aceptar"
'
'cancelar
'
Me.cancelar.DialogResult System.Windows.Forms.DialogResult.Cancel
Me.cancelar.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.cancelar.Location = New System.Drawing.Point(204, 56)
Me.cancelar.Name = "cancelar"
Me.cancelar.TabIndex = 4
Me.cancelar.Text = "Cancelar"
Me.cancelar.CausesValidation = False
'
'texto
'
Me.texto.Location = New System.Drawing.Point(56, 4)
Me.texto.Name = "texto"
Me.texto.Size = New System.Drawing.Size(224, 20)
Me.texto.TabIndex = 5
Me.texto.Text = ""
'
'entero
'
Me.entero.Location = New System.Drawing.Point(56, 28)
Me.entero.Name = "entero"
Me.entero.TabIndex = 6
Me.entero.Text = ""
'
'Form
'
Me.AcceptButton = Me.aceptar
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.CancelButton = Me.cancelar
Me.ClientSize = New System.Drawing.Size(286, 87)
Me.Controls.Add(Me.entero)
Me.Controls.Add(Me.texto)
Me.Controls.Add(Me.cancelar)
Me.Controls.Add(Me.aceptar)
Me.Controls.Add(Me.enteroLabel)
Me.Controls.Add(Me.textoLabel)
Me.FormBorderStyle System.Windows.Forms.FormBorderStyle.FixedDialog
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form"
Me.StartPosition System.Windows.Forms.FormStartPosition.CenterParent
Me.Text = "Ingrese los valores"
Me.ResumeLayout(False)
End Sub

#End Region

Private Sub OnEnteroTextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles entero.TextChanged
Try
Integer.Parse(entero.Text)
Catch ex As Exception
entero.Text = "0"
End Try
End Sub

End Class

Public Overloads Overrides Function GetEditStyle( _
ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle

Return UITypeEditorEditStyle.Modal

End Function

Public Overloads Overrides Function EditValue( _
ByVal context As ITypeDescriptorContext, _
ByVal provider As System.IServiceProvider, _
ByVal value As Object) As Object

Dim obj As MiEstructura = DirectCast(value, MiEstructura)
Dim editorSvc As IWindowsFormsEditorService

editorSvc CType(provider.GetService(GetType(IWindowsFormsEditorService)),
IWindowsFormsEditorService)

Dim f As New MiEstructuraEditor.Form
f.texto.Text = obj.Texto
f.entero.Text = obj.Entero.ToString

If editorSvc.ShowDialog(f) = DialogResult.OK Then
Return New MiEstructura(f.texto.Text,
Integer.Parse(f.entero.Text))
Else
Return value
End If

End Function

End Class

Eduardo A. Morcillo [MS MVP VB]
http://www.mvps.org/emorcillo

El ignorante si calla, será tenido por erudito
y pasará por sabio si no habre los labios.
Salomón

Preguntas similares