Listbox FTP Log Problem

tsouthar

Member
Joined
Jan 29, 2007
Messages
10
Programming Experience
3-5
Hi,

Here is what I have... I have an FTP class with a bunch of functions in it, and everytime it recieves or sends a command to the FTP server, I have it go to a 'AddToLog' Function. This function is supposed to update the main form list box so I do a listbox.items.add(message). It doesn't add it to the list box.

I have the
Inherits System.Windows.Forms.Form in the class
i have the main form defined in that class as
Dim LogWindow As New Form1
so I call the list box by
LogWindow.listbox.items.add(message)
but it doesn't update the listbox or refresh the form. any thoughts?

Tim
 
"Dim LogWindow As New Form1" - you are creating a new instance of Form1 instead of referencing the existing instance.
There are many ways to reference the existing Form1 instance. Here are some:
--make a global variable for the Form1 instance (for example by creating a module and declaring the variable there)
--create a New procedure for the FTP class that accepts a reference to Form1
--create an event in the FTP class that is raised when the logging should occur and have Form1 handle that event. - probably the best solution.
 
Here is example code for creating the event in the FTP class:
VB.NET:
Public Class FTP

    Public Event SendMessage(ByVal Message As String)

    Private Sub Command()
        'receive or send a command
        RaiseEvent SendMessage("A Command has been sent or received")
    End Sub

End Class

Public Class Form1
    Inherits Form

    Private WithEvents _FTP As FTP

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _FTP = New FTP()
    End Sub

    Private Sub _FTP_LogInfo(ByVal message As String) Handles _FTP.SendMessage
        ListBox1.Items.Add(message)
    End Sub

End Class
As for using the module and global variables, read the articles in my signature named "Essential Reading: Multiple Forms"
 
I'm glad you got it working.
But, I think some may agree that it's bad practice to expose the whole form throughout entire the app, especially since the only purpose is to add something to a listbox contained within. In object oriented programming it is a good practice is to keep objects as encapsulated as possible with as narrow a scope as possible. The FTP class shouldn't have to know anything about form1, it should just do its work and raise events when appropriate.
 
Back
Top