多个依赖于my.settings的MDI子客户端

时间:2016-05-30 05:19:08

标签: vb.net client mdichild

我有一个多客户端,我得到here,我想要的是让客户端的每个设置都依赖于my.settings。

我有这段代码:

'Sub to create client
Private Sub AddNewClient()
    Call New frmClient() With {.MdiParent = Me}.Show()
End Sub


'OnLoad Event that  creates the new client
Private Sub MainWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Display a single client window by default.
    Me.AddNewClient()
    Me.AddNewClient()
End Sub



Public Class frmClient
    Private ReadOnly host As String = Environment.MachineName
    Private ReadOnly port As Integer = 3131
    Private WithEvents client As New MessageClient(host, port)
    'And lots and lots of code
End Class

我希望它是这样的:

'Sub to create client
Private Sub AddNewClient(parameterForIP, parameterForPort)
    Call New frmClient(parameterForIP, parameterForPort) With {.MdiParent = Me}.Show()
End Sub


'OnLoad Event that  creates the new client
Private Sub MainWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Display a single client window by default.
    Me.AddNewClient(my.settings.ipClient1, my.settings.ipPort1)
    Me.AddNewClient(my.settings.ipClient2, my.settings.ipPort2)
End Sub



Public Class frmClient(parameterForIP, parameterForPort)
    Private ReadOnly host As String = parameterForIP
    Private ReadOnly port As Integer = parameterForPort
    Private WithEvents client As New MessageClient(host, port)
    'And lots and lots of code
End Class

'Lots of code follows here

似乎我错过了什么,是吗?

更新:基于jmcilhinney给出的代码,它使我的UI变得像这样

enter image description here

我已经像这样使用了它

Public Sub AddNewClient(clientIP As String, clientPort As Integer)
    Call New frmClient(clientIP, clientPort) With {.MdiParent = Me}.Show()
End Sub


Private Sub MainWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Display a single client window by default.
    With My.Settings
        Me.AddNewClient(.ipClient1, .portClient1)
        Me.AddNewClient(.ipClient2, .portClient2)
    End With
End Sub

1 个答案:

答案 0 :(得分:1)

此:

Public Class frmClient(parameterForIP, parameterForPort)
    Private ReadOnly host As String = parameterForIP
    Private ReadOnly port As Integer = parameterForPort
    Private WithEvents client As New MessageClient(host, port)
    'And lots and lots of code
End Class

必须是这样的:

Public Class frmClient
    Private ReadOnly host As String
    Private ReadOnly port As Integer
    Private WithEvents client As MessageClient

    Public Sub New(parameterForIP As String, parameterForPort As Integer)
        InitializeComponent()

        host = parameterForIP
        port = parameterForPort
        client = New MessageClient(host, port)
    End Sub

    'And lots and lots of code
End Class