VB - 通过变量/字符串在ListView另一种形式的显示器 [英] VB - Pass Variables/String to another Form and display in a ListView

查看:260
本文介绍了VB - 通过变量/字符串在ListView另一种形式的显示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2种形式,formOrder和formOrderSummary。

I have 2 forms, formOrder and formOrderSummary.

在formOrder有3个按钮和一个ListView1的。
btnVanilla,btnChocolate和btnNextPage和ListView1的

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

在formOrderSummary有一个listView2。

On formOrderSummary there is a listView2.

每个时间香草或巧克力是点击它会添加到列表视图。

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

我想要做的就是ListView1的上formOrder在listviewOrder展示
上formOrderSummary。目前,我得到它的工作,如果列表视图是在相同的形式,但我似乎无法做到这一点,如果列表视图是一个不同的形式。

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.

有低于截图形象化了我的意思

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

在这里输入的形象描述

推荐答案

表格只是类,它是这么说的所有的人的顶部:

Forms are just classes, it says so at the top of all of them:

Public Class formOrder 

所以,你可以添加所有的方法,或者你想属性。你真正需要做的是建立一个方法(次)的顺序传递。更大的问题是你如何在一个方法传递2个东西(2锥)?如果冰Creame专柜提供的华夫筒,洒​​,调味壳或其他额外?

so, you can add all the methods or properties you'd like. All you really need to do is create a method (sub) to pass the order to. The bigger issue is how do you pass 2 things (2 cones) in one method? What if the Ice Creame Shoppe offered waffle cones, sprinkles, flavored shells or other extras?

那岂不是巨大的,如果有一种方法来保持数据一起组织的?有:

Wouldn't it be great if there was a way to keep the data together and organized? There is:

Public Class IceCream

    Public Enum IceCreamFlavors
        Vanilla
        Chocolate
        Strawberry
    End Enum

    Public Property Flavor As IceCreamFlavors
    Public Property Sprinkles As Boolean
    Public Property Scoops As Int32

    Public Sub New()
        Scoops = 1
    End Sub

    Public ReadOnly Property Cost As Decimal
        Get
            Return GetCost()
        End Get
    End Property

    ' just because I dont like a lot of code in getters
    Private Function GetCost() As Decimal
        Dim price As Decimal = 0

        Select Case Flavor
            Case IceCreamFlavors.Chocolate
                price = 0.75D
            Case IceCreamFlavors.Vanilla
                price = 0.65D
            Case IceCreamFlavors.Strawberry
                price = 55D
                ' coming soon: Rocky Road!
        End Select

        Dim temp = (Scoops * price)

        If Sprinkles Then
            temp += 0.25D
        End If
        Return temp
    End Function
End Class

好吧,这将举行一个锥形的信息。注意它包含一切相关锥项目,甚至定价。这是很容易使其更加复杂,如WaffleCones,巧克力外壳等,而无需在形式加入大量松散的变量。

Ok, that will hold the info for one cone. Notice how it contains everything related to a cone item, even pricing. It is easy to make it more complex such as WaffleCones, Chocolate shell etc without adding lots of loose variables in your form.

我们还需要一种方法来容纳几个人。我知道你在想什么:阵列的。不要去那里。 列表(冰淇淋)更容易比阵列来使用。

We also will need a way to hold several of them. I know what you are thinking: array. Dont go there. A List(of IceCream) is even easier to use than an array.

Private cones As New List(Of IceCream)   ' will only hold icecream items
...
Dim item = New IceCream
item.Flavor = IceCream.IceCreamFlavors.Chocolate
item.Sprinkles = False
item.Scoops = 2
cones.Add(item)

该块创建一个新的冰淇淋项目,然后设置的各种属性,那么最后它添加到列表中。请注意,我怎么没有告诉列表有多大是?

The block creates a new Icecream item, then sets the various attributes, then finally adds it to the list. Notice how I did not have to tell the List how big to be?

item = New IceCream
item.Flavor = IceCream.IceCreamFlavors.Vanilla
item.Sprinkles = True
cones.Add(item)

您访问信息大致相同的方式:昏暗thisCost = item.Cost

You access the info much the same way: Dim thisCost = item.Cost

冰淇淋类默认为1勺,所以你只需要设置,当它不是一球。你可以添加其他默认值。

The IceCream class defaults to 1 scoop, so you only have to set that when it is not one scoop. You could add other defaults.

现在,订单汇总表上创建一个方法来接受数据:

Now, on the order summary form create a method to accept the data:

Public Sub SummaryData(order As List(Of IceCream))

   ' post items to listview
   'e.g.: flavor = order(0).Flavor.ToString()
End Sub

如果您想通过项目添加到它的项目,声明它采取一个项目:

If you want to add to it item by item, declare it to take one item:

Public Sub SummaryData(orderItem as IceCream)

(会出现一个问题,当你想删除一个)。来传递数据:

(A problem will arise when you want to remove one). To pass the data:

frmSummaryINSTANCE.SummaryData(cones)

提示:一个更简单的方法是使用用一个DataGridView。

Tip: An even easier method is to use use a DataGridView.

dgv.DataSource = order

DataGridView的将创建列,并自动显示属性数据。

The DataGridView will create the columns and display the property data automagically.

您当然可以使用该不通过改变参数一类或列表,但它得到罗嗦和致密的参数的增加的数量和类型。

You could of course use this without a Class or List by changing the arguments, but it gets wordy and dense as the number and type of arguments increases.

表单实例结果
一旦你的数据组织得当,变成pretty容易做任何你想要的。注意:这里假设你正在使用的表单实例。即,而不是

Form Instances
Once you have the data organized properly it becomes pretty easy to do whatever you want. Note: This assumes you are using form instances. That is, rather than:

formOrderSummary.SummaryData(cones)

您做这样的事情:

Dim frmSumm As New formOrderSummary()
frmSumm.SummaryData(cones)

如上所述,形式是类和使用它们的适当的方法是(就像在C#)创建一个实例。如果你不这样做了,这是别人学习的时候了一些东西。

As noted, forms are classes and the proper way to use them is to create an instance of one (just like in C#). If you are not doing that now, it is something else to learn right away.

您可能也想看看:五分钟指南类和列表

这篇关于VB - 通过变量/字符串在ListView另一种形式的显示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆