Solved

VB.NET add listbox item that is in another form.

Posted on 2004-08-19
24
664 Views
Last Modified: 2012-05-05
I want to be able to add a listbox item from another form


Form1                      Form2
{Listbox1}                {Button}  - --- >    {Add item to Form1 Listbox1}
0
Comment
Question by:Vartana
  • 9
  • 7
  • 4
  • +2
24 Comments
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 11843836
Public Class Form2: Inherits Windows.Forms
'blah blah blah

Private Sub AddListBoxToOtherForm
   Dim newForm as New Form1
   newForm.Controls.Add(new ListBox)
   newForm.Show
End Sub

End Class
0
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 11843883
Actually, for exact code:

Create a Button - let's call it Button1 on Form2

The code behind is:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim newForm As New Form1
        Dim newLB As New ListBox
        newLB.Items.Add("You just added me from Form2!")
        newLB.Items.Add("Good Job!")
        newLB.Location = (New Point(100, 100))
        newLB.Size = New Size(50, 50)
        newForm.Controls.Add(newLB)
        newForm.Show()
    End Sub

Enjoy.
0
 
LVL 1

Author Comment

by:Vartana
ID: 11844034
The form is already open and it already has a listbox

I just want to be able to add new items to it.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 11844068
You need to have a reference to the listbox in your form2 class like this:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(8, 8)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(312, 238)
        Me.ListBox1.TabIndex = 0
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(200, 256)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(120, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Open Form2"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(328, 286)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.ListBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private WithEvents f2 As Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If f2 Is Nothing Then
            f2 = New Form2
            f2.lb = Me.ListBox1
            f2.Show()
        Else
            f2.Focus()
        End If
    End Sub

    Private Sub f2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles f2.Closed
        f2 = Nothing
    End Sub
End Class

Public Class Form2
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(8, 8)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(280, 20)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "TextBox1"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(200, 40)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(88, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Add to ListBox"
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 70)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.Name = "Form2"
        Me.Text = "Form2"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public lb As ListBox

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not (lb Is Nothing) Then
            lb.Items.Add(TextBox1.Text)
            TextBox1.Text = ""
        End If
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            e.Handled = True
            If TextBox1.Text.Length > 0 Then
                Button1_Click(Nothing, Nothing)
            End If
        End If
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text.Length > 0 Then
            Button1.Enabled = True
        Else
            Button1.Enabled = False
        End If
    End Sub

End Class

Regards,

Idle_Mind
0
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 11844104
Ah, ok.

Well then, let's see:

        Dim newform As New Form1
        Dim ctl As Control
        For Each ctl In newform.Controls
            If ctl.Name = "ListBox1" Then
                CType(ctl, ListBox).Items.Add("I just added a new item!")
            End If
        Next
        newform.Show()
0
 
LVL 1

Author Comment

by:Vartana
ID: 11844108
I have a form1 and the Module i created will be the one adding .
0
 
LVL 1

Author Comment

by:Vartana
ID: 11844137
the form is already visible.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11844146
Does your app start with Form1 or the Module?

Either way, you just need to declare a Public variable of type ListBox and set it to the ListBox in your Form1.

Idle_Mind
0
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 11844152
Well, that's fine too.

Just ignore that part.  

0
 
LVL 1

Author Comment

by:Vartana
ID: 11844155
       Do While Blah blah
                xFrm.ListBox1.Items.Add("File Not Found : " & sFileChild)
                xFrm.Show()
        Loop

This just opens alot
0
 
LVL 1

Author Comment

by:Vartana
ID: 11844170
Idle_Mind how do i do this
Either way, you just need to declare a Public variable of type ListBox and set it to the ListBox in your Form1.

Example please
0
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 11844188
I don't understand.

At the beginning you specifically stated you needed to add the item to Form1 from Form2.

Now you say from a module.

If it's a module, then did the module create the form?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:Vartana
ID: 11844216
Sorry I thought it would be easy that way but i need it to be a module

Heres how its going to be


{ Form1 }
[Listbox1]

On load its going to run a function in the module1

{Module1}
[Function]
form1 listbox add new item
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11844223
Right.  I have already asked this question.


Vartana,

It makes a big difference to the code depending on which comes first, the Form or the Module.

If the Module is creating the form, then jrandallsexton has already given you an example that should work.  You can access the ListBox directly using your reference to the form.

If the form comes first then you have to options.
1) Pass a reference to your listbox into the sub/function in your module.
2) Create a public variable on your module to hold a reference to the listbox.

Idle_Mind
0
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 11844227
So, Form1 will call a function within the module that will in turn add items to a listbox on Form1, yes?
0
 
LVL 1

Author Comment

by:Vartana
ID: 11844248
yes
0
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 11844256

From form 1:

Dim theListBox as ListBox = ListBox1

theModule.SomeFunction(me.theListBox)

From the module:

Public Function SomeFunction(ByRef theListBox As ListBox)
   theListBox.Items.Add("I've been added from the Module!")
End function
0
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 11844271
This is implementing what Idle_Mind stated about passing in a reference to the listbox on Form1.
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11844561
Using Module is a good choice...in your module... make an instance of Form1.

Public F1 As New Form1()

Then use,    F1.ListBox1.Items.Add("blah blah")   (Make sure that the Modifier property of the ListBox1 is set to Friend)
0
 
LVL 4

Expert Comment

by:Daniellus83
ID: 11845032
I made you an example:

     http://www.danieltrommel.com/download/expl02.zip

unzip this solution and you'll know how to do this....


Try it and maybe you like it!!


Daniël
0
 
LVL 4

Expert Comment

by:Daniellus83
ID: 11845100
Forgot: the example provides 2 forms. Click a button on one form,.. it wil open the other form. In that other form there is a button,... if you click that one all the textboxes from the other form are being cleared. Look how this is done and you will be able to do so with your Listbox...

It's more easy than you would think.
0
 
LVL 4

Expert Comment

by:jrandallsexton
ID: 11845700
Vartana:

did you get it working yet?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11846833
Daniellus83,

The purpose of Experts Exchange is build to database of solutions to problems.  Providing an answer in the form of a zip does not allow future users of EE to benefit from your code since your link will not always be there.

In the future, please post code and/or explain how to build the project you think will solve the authors problem.

Regards,

Idle_Mind
0
 
LVL 4

Expert Comment

by:Daniellus83
ID: 11850062
Idle_Mind,...

yes you are right.. I am really sorry.

But what I usually do (...) is when this solution is accepted I post the code. Or more often: I post the solution in form of a link (for easy use) with the code. Sometimes I help somebody with a question and it generates a massive block (I mean >4 pages) of sourcecode... Then I choose to post the code later...
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now