5 Replies - 751 Views - Last Post: 08 November 2011 - 08:51 AM Rate Topic: -----

#1 Daelynn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-March 11

Copying item from listbox in second form to a listbox in main form

Posted 07 November 2011 - 01:13 PM

Hi, I have two forms, both with listboxes, and a module with a global variable called strItem.
Lstbox1 on my main form is blank, and lstbox2 on the second form has 3 items in it that has a price on it.
Here is my code on the second form.
        
strItem = lstPrint.SelectedItem.ToString()
Select Case(lstBox2.SelectedIndex)
     Case 0
        g_sngPrint = 5
     Case 1
        g_sngPrint = 10
     Case 2
        g_sngPrint = 15
End Select



Here is my module
Module MainModule
    Public strItem As String
    Public intVal As Integer
End Module



And here's my main form
    Private Sub MainForm_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Dim total as integer
        lstProduct.Items.Add(strItem)
    End Sub



When I return to the main form, the string from the lstbox on my second form should be added to the listbox on the first form. Also when I return, it should calculate values, but I'll worry about that later.

My problem is that if I use an activated event, I notice that if I have a message box pop up by clicking a button, it will add the item in the variable again. But if I use a load event, it doesn't function at all. I think I still need to be using a activated or load event so the mainform will update when I add items from the other listbox, but I'm still doing it wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Copying item from listbox in second form to a listbox in main form

#2 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Copying item from listbox in second form to a listbox in main form

Posted 07 November 2011 - 01:26 PM

One way to do it using your current event handlers would be to clear the variables after adding them to the listbox on the main form, and check them at the beginning of the Activated handler (and exit if they're empty):
Private Sub MainForm_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    If String.IsNullOrEmpty(strItem) Then Exit Sub

    Dim total as integer
    lstProduct.Items.Add(strItem)
    strItem = String.Empty
End Sub

The empty/exit check prevents you from having a bunch of empty entries in the list.

This post has been edited by Tryparse: 07 November 2011 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#3 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Copying item from listbox in second form to a listbox in main form

Posted 07 November 2011 - 02:42 PM

View PostTryparse, on 08 November 2011 - 06:26 AM, said:

One way to do it using your current event handlers would be to clear the variables after adding them to the listbox on the main form, and check them at the beginning of the Activated handler (and exit if they're empty):
Private Sub MainForm_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    If String.IsNullOrEmpty(strItem) Then Exit Sub

    Dim total as integer
    lstProduct.Items.Add(strItem)
    strItem = String.Empty
End Sub

The empty/exit check prevents you from having a bunch of empty entries in the list.

@TryParse: can't you do this:
If String.IsNullOrEmpty(strItem) Then
Exit Sub
Else
lstProducts.items.Add(strItem)
strItem = String.Empty
End If



J-Bo
Was This Post Helpful? 0
  • +
  • -

#4 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Copying item from listbox in second form to a listbox in main form

Posted 07 November 2011 - 03:12 PM

View PostjimmyBo, on 07 November 2011 - 03:42 PM, said:

@TryParse: can't you do this:
If String.IsNullOrEmpty(strItem) Then
Exit Sub
Else
lstProducts.items.Add(strItem)
strItem = String.Empty
End If



J-Bo

Yeah, that does the exact same thing. The way I wrote it is just my style .. it eliminates the need for the Else and End If lines, while maintaining the same readability (in my opinion anyway ...).
Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Copying item from listbox in second form to a listbox in main form

Posted 07 November 2011 - 04:48 PM

View PostTryparse, on 08 November 2011 - 08:12 AM, said:

View PostjimmyBo, on 07 November 2011 - 03:42 PM, said:

@TryParse: can't you do this:
If String.IsNullOrEmpty(strItem) Then
Exit Sub
Else
lstProducts.items.Add(strItem)
strItem = String.Empty
End If



J-Bo

Yeah, that does the exact same thing. The way I wrote it is just my style .. it eliminates the need for the Else and End If lines, while maintaining the same readability (in my opinion anyway ...).

Meh... I guess it just all comes down to that. When I use If statements with multiple conditions it helps me read it over when I use Else and ElseIf.
Was This Post Helpful? 0
  • +
  • -

#6 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Copying item from listbox in second form to a listbox in main form

Posted 08 November 2011 - 08:51 AM

Public Properties, variables, and modules are your gateway to simplicity when it comes to sharing data between forms.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1