我有一個奇怪的問題,我無法解決。
我有以下代碼:
Public Class Form1
Public WithEvents MyClass1 As New MyClass
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub MyClass_UpdateListbox() Handles MyClass1.UpdateListbox
For Each sItem as String In MyClass1.Listbox
MsgBox(sItem) 'an MsgBox shows correct items each time.
Next sItem
Me.Listbox = Me.MyClass1.Listbox 'doesn't work and breaks listbox.
Me.Listbox.Items.Clear() 'listbox is empty anyway, but has no effect.
Me.Listbox.Items.Add("event triggered") 'does nothing.
End Sub
End Class
Public Class MyClass
Public Listbox as new Listbox
Public Event UpdateListbox()
Public Sub New()
'Constructor. sub.
Me.AddItem("Populating listbox")
End Sub
Public Sub AddItem(sItem as String)
Me.Listbox.Items.Add(sItem)
RaiseEvent UpdateListbox()
End Sub
End Class
如果我在上面的代碼中注釋以下行,串列框會event triggered按預期繼續添加。當然,我不必洗掉透明的。它會起作用,但它只會添加相同的專案。如果我使用命令按鈕并呼叫正確添加的 MyClass.AddItem("Something") ,只要將以下內容注釋掉即可。但如果沒有,那么一旦串列框處于損壞狀態,就無法再添加任何內容。
Me.Listbox = Me.MyClass1.Listbox 'doesn't work and breaks listbox.
Me.Listbox.Items.Clear() 'listbox is empty anyway, but has no effect.
如何使用虛擬串列框并將其分配給我的真實串列框?
此外,除了將一個串列框分配給另一個串列框外,我當然可以為每個回圈使用它并一個一個地添加每個專案,但這首先是為了除錯目的。
編輯:
我使用此應用程式的目標是構建一個包含待辦事項串列中沒有的功能的待辦事項串列。這是我為作業而構建的專案,因為我需要這樣的工具。我已經有一個我使用的 todolist,但我過去構建錯誤。一切都濃縮在 form1 中,沒有模塊沒有額外的類。結果,我得到了一些奇怪的錯誤,我用變通方法修補了這些錯誤。我現在正在從頭開始重建應用程式,將任務分離到自己的類中,這樣我就可以應用業務邏輯并擁有一個真正的 OOP 應用程式。待辦事項串列將成為自己的類,管理串列等將由該類處理。它與表單上的控制元件互動,例如按鈕和串列框。如果我只使用課程中的 form1.listbox,程式開始時會出現問題。我開始了另一個問題,下面的代碼是一個現已洗掉的答案。
所以我的目標是讓 todolist 完全由 todolist 類處理。它確實需要一種與 form1 上的控制元件互動的方法,這就是我目前正在嘗試解決的難題。
uj5u.com熱心網友回復:
在原始代碼中,主要問題是如果將 Form 重新分配給自定義類中定義的另一個 ListBox 控制元件的實體,則會顯示包含控制元件實體的欄位:
Me.Listbox = Me.MyClass1.Listbox
從現在開始,Me.Listbox指向另一個未顯示在螢屏上的 ListBox,因此任何更新 Form 的子 ListBox 的嘗試都會失敗,除非在重新分配 Me.Listbox.Items.Clear() 時 - 在相同的程序中 - 在它被重新分配之后,因為ObjectCollection的所有者的句柄(保存 ListBox 中顯示的專案的物件)尚未更新。盡管如此,在當前方法退出后它將失敗。
如評論中所述,這是使用處理程式類處理表單及其子控制元件的簡化方法。類處理程式和表單之間的契約由介面(此處命名)密封IFormHandler。
實作此介面的表單公開了介面定義的方法,這些方法允許觸發操作和特定行為,具體取決于控制元件的型別和實作。
我建議看一下 WinForms Patterns 的MVPor ReactiveUI (MVVM-derived)。
如何進行:
打開ApplicationEvents類物件。
如果您還沒有它,請選擇Project -> Properties -> Application并單擊該View Application Events按鈕。它將生成ApplicationEvents.vb. 在解決方案資源管理器中找到它并打開它。
它應該看起來像這樣(加上一堆解釋它的用途的評論):
Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
Partial Friend Class MyApplication
End Class
End Namespace
粘貼到MyApplication這些代碼行中:
Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
Partial Friend Class MyApplication
Public SomeFormHandler As MyFormHandler(Of SomeForm)
Protected Overrides Function OnStartup(e As StartupEventArgs) As Boolean
SomeFormHandler = New MyFormHandler(Of SomeForm)
Return MyBase.OnStartup(e)
End Function
End Class
End Namespace
添加定義表單必須實作的操作(或行為)的介面。
此處,該GetUsersList()方法指定實作此介面的表單必須回傳子串列框控制元件的實體。
(要添加介面,Project -> Add -> New Item...請選擇并選擇Interface模板。命名檔案IFormHandler)
根據需要擴展此介面,以添加更多定義操作和行為的方法或屬性。
Public Interface IFormHandler
Function GetUsersList() As ListBox
End Interface
實作介面的FormIFormHandler實作并公開GetUsersList()方法,該方法回傳一個ListBox控制元件的實體(命名為 usersListhere)
這個Form沒有別的關系,控制權交給了MyFormHandler用這個Type初始化的物件。
Public Class SomeForm
Implements IFormHandler
Public Sub New()
InitializeComponent()
End Sub
Public Function GetUsersList() As ListBox Implements IFormHandler.GetUsersList
Return Me.usersList
End Function
End Class
現在,要顯示SomeForm,您可以使用下面的MyFormHandler類物件顯示。
' Set the Owner if called from another Form
My.Application.SomeFormHandler.Show(Me)
' Or without an Owner
My.Application.SomeFormHandler.Show()
要關閉SomeForm,您可以使用其處理程式:
My.Application.SomeFormHandler.Close()
或像往常一樣關閉它:
[SomeForm Instance].Close()
如果MyFormHandler確定 的實體已被釋放,它會在您稍后再次SomeForm呼叫其方法時創建一個新實體。Show()
要更新 的 ListBox 控制元件SomeForm,請使用MyFormHandler該類公開的公共方法:
' Add a new element
My.Application.SomeFormHandler.UpdateUsersList(UpdateType.AddElement, "Some Item")
' Remove an element
My.Application.SomeFormHandler.UpdateUsersList(UpdateType.RemoveElement, "Some Item")
' Replace an element
My.Application.SomeFormHandler.UpdateUsersList(UpdateType.ReplaceElement, "New Item", "Some Item")
' Clears the ListBox
My.Application.SomeFormHandler.ClearUsersList()
所有這些操作都會生成一個事件,您可以在需要時訂閱該事件。
另請參閱顯示如何在 ListBox 引發其標準事件之一時引發自定義事件的示例;SelectedIndexChanged在這里處理。
參見MyFormHandler.
通用表單處理程式:
表單需要為類實作IFormHandler介面MyFormHandler才能接受它為有效。
您當然可以擴展介面,添加更多操作,或構建MyFormHandler使用不同介面或多個介面的類物件。
Public Class MyFormHandler(Of TForm As {Form, IFormHandler, New})
Implements IDisposable
Private formObject As TForm
Private IsInstanceSelfClosing As Boolean = False
Public Event UsersListUpdate(item As Object, changeType As UpdateType)
Public Event UsersListIndexChanged(index As Integer)
Public Sub New()
InitializeInstance()
Dim lstBox = formObject.GetUsersList()
AddHandler lstBox.SelectedIndexChanged, AddressOf OnUsersListIndexChanged
End Sub
Private Sub InitializeInstance()
formObject = New TForm()
AddHandler formObject.FormClosing, AddressOf OnFormClosing
End Sub
Private Sub OnFormClosing(sender As Object, e As FormClosingEventArgs)
IsInstanceSelfClosing = True
Dispose()
End Sub
Public Sub UpdateUsersList(updateMode As UpdateType, newItem As Object, Optional oldItem As Object = Nothing)
If newItem Is Nothing Then Throw New ArgumentException("New Item is null")
Dim lstBox = formObject.GetUsersList()
Select Case updateMode
Case UpdateType.AddElement
lstBox.Items.Add(newItem)
Case UpdateType.RemoveElement
lstBox.Items.Remove(newItem)
Case UpdateType.ReplaceElement
If oldItem Is Nothing Then Throw New ArgumentException("Replacement Item is null")
Dim index = lstBox.Items.IndexOf(oldItem)
lstBox.Items.Remove(oldItem)
lstBox.Items.Insert(index, newItem)
Case Else : Return
End Select
RaiseEvent UsersListUpdate(newItem, updateMode)
End Sub
Public Sub ClearUsersList()
formObject.GetUsersList().Items.Clear()
End Sub
Private Sub OnUsersListIndexChanged(sender As Object, e As EventArgs)
RaiseEvent UsersListIndexChanged(DirectCast(sender, ListBox).SelectedIndex)
End Sub
Public Sub Show(Optional owner As IWin32Window = Nothing)
If formObject Is Nothing OrElse formObject.IsDisposed Then InitializeInstance()
If formObject.Visible Then
formObject.WindowState = FormWindowState.Normal
formObject.BringToFront()
Else
formObject.Show(owner)
End If
End Sub
Public Sub Close()
If formObject IsNot Nothing AndAlso (Not formObject.IsDisposed) Then
RemoveHandler formObject.FormClosing, AddressOf OnFormClosing
IsInstanceSelfClosing = False
Dispose()
End If
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Sub Dispose(disposing As Boolean)
If disposing Then
If formObject Is Nothing OrElse formObject.IsDisposed Then Return
Dim lstBox = formObject.GetUsersList()
RemoveHandler lstBox.SelectedIndexChanged, AddressOf OnUsersListIndexChanged
RemoveHandler formObject.FormClosing, AddressOf OnFormClosing
If Not IsInstanceSelfClosing Then formObject.Close()
IsInstanceSelfClosing = False
End If
End Sub
End Class
列舉器用于MyFormHandler:
Public Enum UpdateType
AddElement
RemoveElement
ReplaceElement
End Enum
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489384.html
