Search code examples
vb.netclientmdichild

Multiple MDI child clients that depends on the my.settings


I have a multiple client that I get here, what I want is to make each settings of the client to depend on my.settings.

I have this code:

'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

I want it to be something like this:

'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

It seems that I am missing something, am I?

Update: Base on the code given by jmcilhinney, it makes my UI to become like this

enter image description here

I've used it like this

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

Solution

  • This:

    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
    

    would have to be this:

    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