8 Replies - 760 Views - Last Post: 31 January 2016 - 06:29 PM Rate Topic: -----

#1 imraandream1  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 3
  • Joined: 31-January 16

Pass Variables/String to another Form and display in a ListView

Posted 31 January 2016 - 02:44 PM

I have 2 forms, formOrder and formOrderSummary.

On formOrder there are 3 buttons and a listView1. btnVanilla, btnChocolate and btnNextPage and listview1

On formOrderSummary there is a listView2.

Each time Vanilla or Chocolate is clicked it will add it to the listview.

What I'm trying to do is get the listview1 on formOrder to show in listviewOrder on formOrderSummary. Currently I got it working if the listview is in the same form but I can't seem to do it if the listview is in a different form.

There is a screenshot below which visualises what I mean

formOrder

 Public Class formOrder

Dim frm2 As New formOrderSummary
Public vanillaCount As Integer
Public chocolateCount As Integer
Public mynumber As Double

Private Sub btnVanilla_Click(sender As Object, e As EventArgs) Handles btnVanilla.Click

    Me.vanillaCount = Me.vanillaCount + 1
    Dim str(3) As String
    Dim item As ListViewItem

    str(0) = "Vanilla"
    str(1) = Me.vanillaCount.ToString()
    mynumber = str(1) * 1
    str(2) = mynumber.ToString("C")

    Dim WholeString As String = str(0)

    item = New ListViewItem(str)
    For maindish As Integer = 0 To Me.ListView1.Items.Count - 1


        If (Me.ListView1.Items(maindish).ToString = "ListViewItem: {" + WholeString + "}") Then
            Me.ListView1.Items.RemoveAt(maindish)
            Me.ListView1.Items.Add(item)

            Return
        End If
    Next

    Me.ListView1.Items.Add(item)

End Sub

Private Sub BtnChocolate_Click(sender As Object, e As EventArgs) Handles b btnChocolate.Click

    Me.chocolateCount = Me.chocolateCount + 1
    Dim str(3) As String
    Dim item As ListViewItem

    str(0) = "Chocolate"
    str(1) = Me.chocolateCount.ToString()
    mynumber = str(1) * 1.5
    str(2) = mynumber.ToString("C")

    Dim WholeString As String = str(0)

    item = New ListViewItem(str)
    For maindish As Integer = 0 To Me.ListView1.Items.Count - 1


        If (Me.ListView1.Items(maindish).ToString = "ListViewItem: {" + WholeString + "}") Then
            Me.ListView1.Items.RemoveAt(maindish)
            Me.ListView1.Items.Add(item)

            Return
        End If
    Next

    Me.ListView1.Items.Add(item)

End Sub

Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click

    frm2.Show()
    Me.Hide()
End Sub

End Class 


FormOrderSummary

 Public Class formOrderSummary

    Private Sub formOrderSummary_Load(sender As Object, e As EventArgs) Handles MyBase.Load


    End Sub
End Class 

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Pass Variables/String to another Form and display in a ListView

#2 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1457
  • View blog
  • Posts: 3,699
  • Joined: 01-February 13

Re: Pass Variables/String to another Form and display in a ListView

Posted 31 January 2016 - 03:11 PM

Well, you have an Instance of your 2nd Form with the ListView on it in your main form as shown here.
Dim frm2 As New formOrderSummary


So, instead of adding the New ListViewItem to the ListView on your main form as shown here.
Me.ListView1.Items.Add(item)


You would add it to the ListView on the second Form.
frm2.ListView1.Items.Add(item)


Basically, instead of refering to the ListView on the main form (Me), you would refer to the ListView on the new instance of the 2nd form (frm2).

This post has been edited by IronRazer: 31 January 2016 - 03:15 PM

Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6404
  • View blog
  • Posts: 25,881
  • Joined: 12-December 12

Re: Pass Variables/String to another Form and display in a ListView

Posted 31 January 2016 - 03:28 PM

One form should not directly reference the controls of another form. Instead, they can share a data resource.

Passing Data Between Forms
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1457
  • View blog
  • Posts: 3,699
  • Joined: 01-February 13

Re: Pass Variables/String to another Form and display in a ListView

Posted 31 January 2016 - 03:49 PM

Quote

One form should not directly reference the controls of another form.

I have heard that over and over throughout the years but, nobody ever explains why that should not be done. What is the reason for that?

I usually use a Public Property or Sub in a second form to pass data for things like this but, figured it would be easier to just suggest doing the above.

As a rough example, i might have put a Public Sub in the second form something like this.
Public Class Form2

    Public Sub AddDataToListView(ByVal itm As ListViewItem)
        ListView1.Items.Add(itm)
    End Sub

End Class


And then called it from the main form using the new instance of the 2nd form something like this.
Public Class Form1
    Private f2 As New Form2

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        f2.Show()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim lvi As New ListViewItem("Hello")
        f2.AddDataToListView(lvi)
    End Sub
End Class



Of coarse, OP appears to want to do other stuff with the Items, so that would need to be set up too. I suppose sharing a data source might be easier though. 8)
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6404
  • View blog
  • Posts: 25,881
  • Joined: 12-December 12

Re: Pass Variables/String to another Form and display in a ListView

Posted 31 January 2016 - 04:30 PM

It is described in the important principle of separation of concerns:

Quote

The value of separation of concerns is simplifying development and maintenance of computer programs. When concerns are well-separated, individual sections can be reused, as well as developed and updated independently. Of special value is the ability to later improve or modify one section of code without having to know the details of other sections, and without having to make corresponding changes to those sections.

The disadvantages are well described in the wikipedia page for coupling:

Quote

Tightly coupled systems tend to exhibit the following developmental characteristics, which are often seen as disadvantages:

  • A change in one module usually forces a ripple effect of changes in other modules.
  • Assembly of modules might require more effort and/or time due to the increased inter-module dependency.
  • A particular module might be harder to reuse and/or test because dependent modules must be included.

In these days of TDD the third point is highlighted more prominently.

(At a slightly more philosophical level is the discussion of separation of presentation and content.)

It might seem a chore just for the separation of these two forms but, with a longer term view, the whole application should be built around an object-relational mapping, and classes; the presentation layer (the forms) should not directly interact with the data layer (the model), which is the case here with the data being the listview items.



f2.AddDataToListView(lvi)

This should be event-based, with the first form raising an event that the second form listens for, causing it to execute its own method.



I appreciate that there are two forms, and they are both in the presentation layer, and it might be considered that they work as a unit. But, if this were the case, then they wouldn't be two separate forms, just one ;)
Was This Post Helpful? 1
  • +
  • -

#6 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1457
  • View blog
  • Posts: 3,699
  • Joined: 01-February 13

Re: Pass Variables/String to another Form and display in a ListView

Posted 31 January 2016 - 05:46 PM

I guess i figured that adding a new instance of a ListViewItem to a ListView on Form2 would not be much different than adding it to a ListView on Form1 other than Form1 and Form2 are just 2 different instances of a Form class.

I knew that keeping controls separate from the data as in using a variable/collection to store the data and only using the controls to display/gather that data.

Just to pick your brain, what do you think would be the best way to share data between 2 forms like this if you did not want to use a DataBase or (yuk) a Module. Maybe a Public Property of a List/Collection type which if you really needed could be saved/restored via binary or Xml file...?

Hope i am not being too big of a pain in your butt, i am just curious. 8)
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6404
  • View blog
  • Posts: 25,881
  • Joined: 12-December 12

Re: Pass Variables/String to another Form and display in a ListView

Posted 31 January 2016 - 05:58 PM

Quote

Maybe a Public Property of a List/Collection type which if you really needed could be saved/restored via binary or Xml file...?

Not knowing the full scope (or scoop, get it? :)) of the application, I would probably consider (as a first step) an ice-cream class and a collection of such, and passing (sharing) the instance of the collection in the second form's constructor. Both ListViews can be bound to the same collection.

Yes, a public property is an alternative. I prefer this approach when a form has a distinct, and limited, purpose. With a broader purpose (and several public properties) the possibility of the two forms being considered tightly bound resurfaces (I suspect).
Was This Post Helpful? 1
  • +
  • -

#8 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1457
  • View blog
  • Posts: 3,699
  • Joined: 01-February 13

Re: Pass Variables/String to another Form and display in a ListView

Posted 31 January 2016 - 06:11 PM

Ok, i do use the constructor to pass data once and a while if the data does not need to be changed while the second form is opened. I guess never going to school for programming and only doing it as a hobby i never really got to deep into learning some of the details i should have. I am slowly gathering it from people like you, experimenting, and reading things here an there though. Thanks for your time andrewsw. 8)
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6404
  • View blog
  • Posts: 25,881
  • Joined: 12-December 12

Re: Pass Variables/String to another Form and display in a ListView

Posted 31 January 2016 - 06:29 PM

I'm self-taught, but over a long period and with a lot of time and effort invested. I'm sure the good folks here will correct me though ;) if I make a mis-statement.

Quote

Ok, i do use the constructor to pass data once and a while if the data does not need to be changed while the second form is opened.

That is most common, and sensible; that is, only one view of the data being editable. It is possible for more than one (simultaneous) view to be editable, but not a good idea, from both a user and maintenance perspective. In fact, this is the default as both forms would share the same collection and behaviours, so steps need to be taken to make other views read-only.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1