Jump to content
Xtreme .Net Talk

How do I populate a listbox on one from with the contents of a listbox on another??


Recommended Posts

Posted (edited)

I have an array containing all the item names. I just need to understand how to get the variable over to the other form.

 

thanks

Edited by Adam
I feel the pivitol moment of failure approaching.
Posted

Just add a public property to your new form-class and if you create an instance then drop the listbox in it.

example:

 

'This is the new form wich gets the lisbox
Public Class Form2
   Inherits System.Windows.Forms.Form
   Public lb as Listbox
End Class

 

'This is the call to the new form
Dim newForm as new Form2
newForm.lb = listbox1
newForm.show()

sometimes I blog...

http://michal.grafix.at

Posted

The first code you posted, I understand. But the second, I don't.

 

I have the forms created already. I don't understand the dimming of a New Form2. and the newForm.show().. I'm doing this on a button click. I'm clicking a button and then populating a listbox on another form with an array variable i've got on the form where I clicked the button.

I'm just so confussed.

 

Thanks for helping.

I feel the pivitol moment of failure approaching.
Posted

Think of this as opening your address book in Outlook. You have not data in the To: box. So you click a button and it opens your address window. You select the address you want from the right lstbox and click "add" and it populates the left lst box with your selections. You can add and remove other items at will. When your done you click a "Done" button and your left lstbox data is copied to the lstbox on the email form. (this isn't an email form. But it's a similar function,.. adding the addresses)

Private Sub lnklblAddCust_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnklblAddCust.LinkClicked
       Dim OpenForm As frmAddCust
       OpenForm = New frmAddCust()
       OpenForm.ShowDialog(Me)
   End Sub

 

Once the AddMaterial button is clicked I open this form and do stuff:

 

Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
       Dim lstbox(lstboxMaterial.Items.Count - 1) As String
       Dim val As Integer

       For val = 0 To lstboxMaterial.Items.Count - 1
           lstbox(val) = lstboxMaterial.Items.Item(val)
       Next

       Me.Close()
   End Sub

I feel the pivitol moment of failure approaching.
Posted

hmm .. now i am confused too .. but i think its still the same i have written to you.

 

add the following line to your frmAddCust form

 

Public parentForm as Form

 

now add this line when opening the form

OpenForm.parentForm = me

 

and now you can easily use all properties and methods of the parentform. example

 

Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
       Dim lstbox(lstboxMaterial.Items.Count - 1) As String
       Dim val As Integer

       For val = 0 To lstboxMaterial.Items.Count - 1
           lstbox(val) = lstboxMaterial.Items.Item(val)
       Next
       
       'now we can use all properties and methods of parentform
       Me.parentform.width = 500
       Me.Close()
   End Sub

 

hope you know what i mean

sometimes I blog...

http://michal.grafix.at

Posted

LOL my bad... posted the wrong code. But it's still close to the same...

 

 Private Sub btnAddMaterial_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddMaterial.Click
       Dim OpenForm As frmMaterialSelect
       OpenForm = New frmMaterialSelect()
       OpenForm.ShowDialog(Me)
   End Sub

 

If there is a more right way to do this i'm all ears. I've extremely new to VB and i'm trying to learn as I go.

I feel the pivitol moment of failure approaching.
Posted

Adam,

 

I was reading through the previous messages in this thread and I have to say that, besides confusing each other, you also lost me.

 

I would like to help you, it seems you don't have a major issue here. Could you do me a favor and start fresh.

 

Please state what you need, what you got, and where the problem is.

 

Thanks... :)

IS
Posted

Sure.

Baby steps. I need baby steps. :)

On one form I have an "Add Material" button and a listbox under it(blank). I click the Add Material button and it brings up a new form. This form has 2 listboxes, one on the left and the other on the right. The one on the left is populated with all the Material in a MDB. The one on the right is empty. I select from the left lstbox items I want to add to a WorkOrder Materail list (lstbox on the left). Once all item used on the job are selected and added to the right lstbox I click a done button. I want the contents of the right lstbox to populate the lstbox on the previous form.

 

I stored all the items of the right lstbox into an array. I can display them in a 3rd lstbox (did this as a test). So I have the items saved into this array and ready to populate the lstbox on the first form.

I feel the pivitol moment of failure approaching.
Posted

Ok, so let me try on this Friday :)

 

you initialize this form like this:

 

OpenForm = New frmMaterialSelect()

OpenForm.ShowDialog(Me)

 

Try this instead:

 

OpenForm = New frmMaterialSelect(Me)

OpenForm.ShowDialog()

 

in the frmMaterialSelect, add the following to the initialize section, right below the Public Sub New () method

 

Private CallerForm as NAMEOFYOURCALLINGFORM

 

Public Sub New (frm as NAMEOFYOURCALLINGFORM)

MyClass.New()

CallerForm = frm

End Sub

 

In your Done button, loop through your array and fill the list in the caller form by using this reference, like:

 

CallerForm.ListBox1.[and here whatever you want to do to it]

 

 

Does this work? It would be cool if get this over with during this week :)

IS
Posted
ok. Going to give it go. May take me abit to get back. I'm here at work and well.. i got to do some type of work besides playing with vb. brb asap.
I feel the pivitol moment of failure approaching.
Posted

OpenForm = New frmMaterialSelect(Me)

OpenForm.ShowDialog()

 

(Me) is underlinded saying.... To many arguments to 'Public Sub New()'

I feel the pivitol moment of failure approaching.
Posted

Adam,

 

Read carefully through my message. Did you see the section telling you to create this in your called form:

 

Private CallerForm as NAMEOFYOURCALLINGFORM

 

Public Sub New (frm as NAMEOFYOURCALLINGFORM)

MyClass.New()

CallerForm = frm

End Sub

 

That will give you an overloaded sub new with the parameter.

 

And gabru, of course there is already a Sub New, there is always one when you create a form. This Sub New is generated automatically and takes no parameters....

IS
Posted

Got it. yahooooooooo!!!!!!!!!

Now i'm going to study this for about 3hrs so i'll know what the hell is going on.

 

Thanks ya'll

I feel the pivitol moment of failure approaching.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...