UITypeEditor设置属性的时候,使用了同一个对象

李察德-泰森 2020-04-26 09:19:11

Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Globalization
Imports System.IO
Imports System.Linq
Imports System.Reflection
Imports System.Runtime.CompilerServices
Imports System.Security
Imports System.Text
Imports System.Threading.Tasks
Imports Microsoft.VisualBasic

Public Class CheckedListBoxEditor
Private _strValue As String = "(Collection)"
Private _strValue2 As String = "(Collection)"

<Description("This property contains the checked listbox collection.")>
<EditorAttribute(GetType(CheckedListBoxUITypeEditor), GetType(System.Drawing.Design.UITypeEditor))>
Public Property CheckedListBoxCollectionProperty As String
Get
Return _strValue
End Get
Set(ByVal value As String)
_strValue = "(Collection)"
End Set
End Property

<Description("This property contains the checked listbox collection.")>
<EditorAttribute(GetType(CheckedListBoxUITypeEditor), GetType(System.Drawing.Design.UITypeEditor))>
Public Property CheckedListBoxCollectionProperty2 As String
Get
Return _strValue2
End Get
Set(ByVal value As String)
_strValue2 = "(Collection)"
End Set
End Property
End Class

Public Class CheckedListBoxUITypeEditor
Inherits System.Drawing.Design.UITypeEditor

Private Sub New()
cbx = New CheckedListBox()
End Sub

Private _cbx As CheckedListBox

Public Property cbx As CheckedListBox
<MethodImpl(MethodImplOptions.Synchronized)>
Get
Return _cbx
End Get
<MethodImpl(MethodImplOptions.Synchronized)>
Set(ByVal value As CheckedListBox)

If _cbx IsNot Nothing Then
_cbx.Leave -= AddressOf bx_Leave
End If

_cbx = value

If _cbx IsNot Nothing Then
_cbx.Leave += AddressOf bx_Leave
End If
End Set
End Property

Private es As IWindowsFormsEditorService

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

Public Overloads Overrides ReadOnly Property IsDropDownResizable As Boolean
Get
Return True
End Get
End Property

Public Overloads Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object
es = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)

If es IsNot Nothing Then
LoadListBoxItems()
cbx.Sorted = True
es.DropDownControl(cbx)
End If

Return Nothing
End Function

Private Sub bx_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
My.Settings.UrlsList.Clear()

If True Then
Dim withBlock = cbx

For i As Integer = 0 To withBlock.Items.Count - 1
Dim txt As String = withBlock.Items(i).ToString
Dim chk As String = withBlock.GetItemChecked(i).ToString
Dim combined As String = Strings.LCase(txt) & "," + Strings.LCase(chk)
If withBlock.Items(i).ToString <> "" Then My.Settings.UrlsList.Add(combined)
Next
End If

My.Settings.Save()
End Sub

Private Sub LoadListBoxItems()
Dim a As ArrayList = New ArrayList()

For Each s As String In My.Settings.UrlsList
a.Add(Strings.Split(s, ","))
Next

Dim h As Hashtable = New Hashtable()

For i As Integer = 0 To a.Count - 1
h.Add(CType(a.Item(i).GetValue(0).ToString, Array), CType(a.Item(i).GetValue(1).ToString, Array))
Next

a = Nothing
cbx.Items.Clear()

For Each de As DictionaryEntry In h
cbx.Items.Add(de.Key, System.Convert.ToBoolean(de.Value))
Next

h = Nothing
End Sub
End Class

如上代码,属性 CheckedListBoxCollectionProperty 和 CheckedListBoxCollectionProperty2 单独是可以使用的,但是,CheckedListBoxCollectionProperty 里变更时,在 CheckedListBoxCollectionProperty2 属性里也变更成和 CheckedListBoxCollectionProperty 一样了。看起来 CheckedListBoxUITypeEditor 只生成了一个对象,被两个属性共用,希望两个属性单独使用自己的 CheckedListBoxUITypeEditor 对象,需要怎么修改呢?
...全文
357 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
JackerKun 2021-12-15
  • 打赏
  • 举报
回复

问题解决了吗? 求助 我也遇到了

李察德-泰森 2020-05-15
  • 打赏
  • 举报
回复
引用 6 楼 正怒月神 的回复:
[quote=引用 5 楼 李察德-泰森 的回复:] [quote=引用 4 楼 正怒月神 的回复:] 不能说一个自定义控件只能出现一次 你的自定义控件,每次都是new出来,就没问题。 如果是一个全局变量,然后在2个页面里公用,那就有问题。
现在问题就是自定义控件,每次调用的时候都是new出来的,但是控件中的CheckedListBoxCollectionProperty这个属性,第一个控件修改了,再看第二个控件的属性面板,下拉框中选择项也被修改了[/quote] 其实比较简单的方法,就是你加个断点,调试一下CheckedListBoxCollectionProperty和CheckedListBoxCollectionProperty2。 以及下拉框的绑定事件。 看看为什么会触发。[/quote] 这个看过,就是

Public Class CheckedListBoxUITypeEditor
    Inherits System.Drawing.Design.UITypeEditor
 
    Private Sub New()
        cbx = New CheckedListBox()
    End Sub
End Class
只有第一个控件才走这里的new,以后new出来的控件不走这个new了,但是呢,这个调用是用的 <EditorAttribute(GetType(CheckedListBoxUITypeEditor), GetType(System.Drawing.Design.UITypeEditor))> ,不知道这里要怎么修改才能让CheckedListBoxUITypeEditor每次都走new了
正怒月神 2020-05-15
  • 打赏
  • 举报
回复
引用 5 楼 李察德-泰森 的回复:
[quote=引用 4 楼 正怒月神 的回复:] 不能说一个自定义控件只能出现一次 你的自定义控件,每次都是new出来,就没问题。 如果是一个全局变量,然后在2个页面里公用,那就有问题。
现在问题就是自定义控件,每次调用的时候都是new出来的,但是控件中的CheckedListBoxCollectionProperty这个属性,第一个控件修改了,再看第二个控件的属性面板,下拉框中选择项也被修改了[/quote] 其实比较简单的方法,就是你加个断点,调试一下CheckedListBoxCollectionProperty和CheckedListBoxCollectionProperty2。 以及下拉框的绑定事件。 看看为什么会触发。
李察德-泰森 2020-05-15
  • 打赏
  • 举报
回复
引用 4 楼 正怒月神 的回复:
不能说一个自定义控件只能出现一次 你的自定义控件,每次都是new出来,就没问题。 如果是一个全局变量,然后在2个页面里公用,那就有问题。
现在问题就是自定义控件,每次调用的时候都是new出来的,但是控件中的CheckedListBoxCollectionProperty这个属性,第一个控件修改了,再看第二个控件的属性面板,下拉框中选择项也被修改了
正怒月神 2020-05-15
  • 打赏
  • 举报
回复
不能说一个自定义控件只能出现一次 你的自定义控件,每次都是new出来,就没问题。 如果是一个全局变量,然后在2个页面里公用,那就有问题。
李察德-泰森 2020-05-15
  • 打赏
  • 举报
回复
引用 2 楼 正怒月神 的回复:
引用类型问题吧。 最简单的做法是,把要赋值的内容,转json,在转回来。 赋值给CheckedListBoxCollectionProperty
代码是为了方便调试写的。实际做的时候,一个对象中只存在一个CheckedListBoxCollectionProperty属性。但是,自定义控件,不能说一个自定义控件只能出现一次,当出现多次的时候,问题就发生了。
正怒月神 2020-05-15
  • 打赏
  • 举报
回复
引用类型问题吧。 最简单的做法是,把要赋值的内容,转json,在转回来。 赋值给CheckedListBoxCollectionProperty
李察德-泰森 2020-05-15
  • 打赏
  • 举报
回复
深吸一口气,我顶
这篇文章中我们重点需要实现的是(3)、(4)两项功能,下面我们来介绍具体实现的方法。 第一步,实现ImageComboBoxItem类。 要实现显示图标,当然要给每个项添加与图标相关的信息了,ImageComboBoxItem类应该包括以下内容:文本(Text)、缩进的级别(Level)、图标的索引(ImageIndex、ImageKey),用户数据(Tag)。ImageComboBoxItem类实现了ISerializable接口,实现自定义序列化。ImageComboBoxItem类的类视图如下: 图3 ImageComboxItem类视图 ImageComboBoxItem类的代码如下: [Serializable] [DefaultProperty("Text")] [TypeConverter( typeof(ExpandableObjectConverter))] public class ImageComboBoxItem : IDisposable, ISerializable ...{ Fields#region Fields private ImageComboBox _imageComboBox; private string _text = "ImageComboBoxItem"; private ImageComboBoxItemImageIndexer _imageIndexer; private object _tag; private int _level; #endregion Constructors#region Constructors public ImageComboBoxItem() ...{ } public ImageComboBoxItem(string text) : this(text, -1, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex) : this(text, imageIndex, 0) ...{ } public ImageComboBoxItem( string text, string imageKey) : this(text, imageKey, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex, int level) : this() ...{ _text = text; ImageIndexer.Index = imageIndex; _level = level; } public ImageComboBoxItem( string text, string imageKey, int level) : this() ...{ _text = text; ImageIndexer.Key = imageKey; _level = level; } protected ImageComboBoxItem( SerializationInfo info, StreamingContext context) : this() ...{ Deserialize(info, context); } #endregion Properties#region Properties [Localizable(true)]

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧