【问题标题】:User Control (Windows Form) make elements 'Shared'用户控件(Windows 窗体)使元素“共享”
【发布时间】:2021-09-03 00:18:50
【问题描述】:

问题:我正在使用 VB 在 Outlook (VS 2019) 中构建自定义任务窗格。即将到来,但我无法找到如何保持添加的控件(文本框、列表框等)以从其他用户控件、模块等共享和访问。

当前修复:我可以在 usercontrol.designer.vb 部分手动添加命令“Shared”(它说不要编辑)。例如改变:

Friend WithEvents lbMatterList as Windows.Forms.ListBox

到:

Friend Shared WithEvents lbMatterList as Windows.Forms.ListBox

这适用于构建,但如果我在设计器中编辑 UserControl 的任何部分,代码会重置,我每次都需要手动重新编辑。

我知道 VB 不是最好的语言,但它是我所知道的(这不是我的工作,我只是构建 TaskPane 来帮助我的工作)。

有没有办法在使用设计器时将控件设置为“共享”而无需手动编辑或重置?

示例代码:

在这个阶段,尝试的动作很简单。我只是尝试使用一个模块来使用数组中的值更新列表框的内容

TaskManagement.vb(模块)代码:

Public Sub PopulateMatterList()
    'MatterNotes = TaskDetailView.
    With TaskManagerCtrl.lbMatterList
        .ClearSelected()
        .Items.Add(MatterListPrimary(0, 1) & " - " & MatterListPrimary(0, 0))
        '.Items.Add()
    End With
End Sub

错误:BC30469 对非共享成员的引用需要对象引用

TaskManagerCrtl.Designer.vb 代码:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class TaskManagerCtrl
Inherits System.Windows.Forms.UserControl

'UserControl overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
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()
    Me.lbMatterList = New System.Windows.Forms.ListBox()
    Me.Button1 = New System.Windows.Forms.Button()
    Me.SuspendLayout()
    '
    'lbMatterList
    '
    Me.lbMatterList.ColumnWidth = 900
    Me.lbMatterList.FormattingEnabled = True
    Me.lbMatterList.ItemHeight = 25
    Me.lbMatterList.Location = New System.Drawing.Point(38, 329)
    Me.lbMatterList.MultiColumn = True
    Me.lbMatterList.Name = "lbMatterList"
    Me.lbMatterList.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple
    Me.lbMatterList.Size = New System.Drawing.Size(898, 979)
    Me.lbMatterList.TabIndex = 0
    Me.lbMatterList.ValueMember = "Shared"
    '
    'Button1
    '
    Me.Button1.Location = New System.Drawing.Point(358, 98)
    Me.Button1.Name = "Button1"
    Me.Button1.Size = New System.Drawing.Size(170, 86)
    Me.Button1.TabIndex = 1
    Me.Button1.Text = "Button1"
    Me.Button1.UseVisualStyleBackColor = True
    '
    'TaskManagerCtrl
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(12.0!, 25.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.Controls.Add(Me.Button1)
    Me.Controls.Add(Me.lbMatterList)
    Me.Name = "TaskManagerCtrl"
    Me.Size = New System.Drawing.Size(1010, 1374)
    Me.ResumeLayout(False)

End Sub

Friend WithEvents Button1 As Windows.Forms.Button
Friend Shared WithEvents lbMatterList As Windows.Forms.ListBox

End Class

在最后一行添加 Shared 需要手动以避免错误。我找不到将控件设置为共享的位置。

【问题讨论】:

  • 这里有什么不对劲的地方。您必须在设计器代码中更改访问修饰符这一事实向我表明,您的解决方案中存在更深层次的问题,更新修饰符实际上只是掩盖了真正的来源。不确定到底是什么,但可能会通过一些上下文、示例代码等来更新您的问题
  • 我添加到代码中,我认为我缺少某个设置。

标签: vb.net controls vsto friend shared


【解决方案1】:

使用“共享”将代码添加到 ThisAddIn.vb 允许通过 ThisAddin 进行访问。无需编辑设计器代码即可从模块中获取。

Public WithEvents Active_Explorer 作为 Microsoft.Office.Interop.Outlook.Explorer

'establish Outlook.TaskPanes
Public Shared TaskManagerCtrl As TaskManagerCtrl
Public Shared WithEvents ManagerTaskPane As Microsoft.Office.Tools.CustomTaskPane

'establish Detailed View Outlook.TasPane
Public Shared TaskDetailView As TaskDetailView
Public Shared WithEvents DetailsTaskPane As Microsoft.Office.Tools.CustomTaskPane

【讨论】:

    猜你喜欢
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多