2016-05-30 26 views
0

我有一個多客戶端,我得到here,我想要的是使客戶端的每個設置依賴於my.settings。取決於my.settings的多個MDI子客戶端

我有這樣的代碼:

'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變得像我已經使用了這樣的

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

這本

enter image description here

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 
+0

非常感謝你的先生,請看我最新的問題。應用您的代碼後,我的用戶界面變空了。先生,你有什麼想法嗎? –

+0

我錯過了對'InitializeComponent'的必要調用。我已經相應地更新了代碼。 – jmcilhinney

+0

非常感謝你的先生! :d –