Setting default property fails because it is read only?

 ̄綄美尐妖づ 提交于 2021-02-11 13:49:24

问题


This code was converted from VB6 to VB.Net:

Public prvMainForm = VB6Form    
If prvMainForm IsNot Nothing Then
    CObj(prvMainForm).StatusBar.Panels(1) = "Initializing Folders..."
End If

(My code is quite long so I've just added this if block which is where the actual error occurs.)

The error is seen on the single line inside the If statement:

Property 'Item' is 'ReadOnly'


回答1:


StatusBar.Panels(1) returns a MSComctlLib.Panel.

StatusBar.Panels(1) = "Initializing Folders..." is valid in VB6 because of default properties.

Default properties in VB.NET must have parameters. A parameterless property cannot be default and therefore cannot be omitted. Thus, .Panels(1) = "..." is understood by VB.NET as an attempt to replace the entire Panel in the Panels property, which is not allowed.

You can look up the name of the default property in the VB6 object browser, which turns out to be Property _ObjectDefault As String, so you should be able to do:

CObj(prvMainForm).StatusBar.Panels(1).[_ObjectDefault] = "Initializing Folders..."

As you have observed, assigning Text should do the same:

CObj(prvMainForm).StatusBar.Panels(1).Text = "Initializing Folders..."


来源:https://stackoverflow.com/questions/59720936/setting-default-property-fails-because-it-is-read-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!