5 Replies - 375 Views - Last Post: 14 February 2013 - 10:08 AM Rate Topic: -----

#1 Warfarin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-September 12

transfering from one form to another

Posted 14 February 2013 - 09:28 AM

I am using VB express 2010. My question is this. I am creating a game similar to the old D&D games. I have a store on form1 in list box fashion. I have another form that just lists all of their equipment and allows them to change equipment or drop equipment. My problem is this. When they select an item from the store(listbox) how do I transfer that to the listbox on the equipment form. I also need to add equipment that is found while adventuring.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: transfering from one form to another

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,290
  • Joined: 12-June 08

Re: transfering from one form to another

Posted 14 February 2013 - 09:45 AM

Depending on how many corners you want to cut you can easily just reference GUI objects through the second form you created.

Public Class Form6

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        '-- create your second form object
        Dim _foo As New Form7
        '-- show said form
        _foo.ShowDialog()

        '-- reference the textbox on the second form ("txtboxInput") and get the text out of it.
        Dim sVal As String = _foo.txtboxInput.Text
        '-- display said textbox.
        MsgBox(sVal)

    End Sub
End Class

'-----------
Public Class Form7
    '-- has a textbox on the form called "txtboxInput"
End Class



or.. if you are trying to make a better practice you can pass your objects into form2's when it is created and pass back those values when done through basic 'property' calls.
Was This Post Helpful? 0
  • +
  • -

#3 Warfarin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-September 12

Re: transfering from one form to another

Posted 14 February 2013 - 09:48 AM

are you just picking _foo as a variable or is there something special about it. I have seen it before but have no clue what or why it is used
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,290
  • Joined: 12-June 08

Re: transfering from one form to another

Posted 14 February 2013 - 09:50 AM

It's a variable name. I could have picked 'cow' or 'brown' or 'form_number_seven'.
Was This Post Helpful? 0
  • +
  • -

#5 Warfarin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-September 12

Re: transfering from one form to another

Posted 14 February 2013 - 09:51 AM

ok thanks I have just seen others use it.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,290
  • Joined: 12-June 08

Re: transfering from one form to another

Posted 14 February 2013 - 10:08 AM

Here's a more proper example.. something that transfers an object (filled with data) from the main form to the second form and then the main form can access that data that may have changed on the second form.

The gist is we have this trinket class.. nothing fancy, but it has two variables. We create it on form6, pass it to form7 when form7 is created, let a user edit it on form7, and then allow form6 to get the value through a property. Form7's object still exists in that scope (even though it is not visible) so any internal variables can be made to be open.

'-- this is a basic class with two variables and a method to print out their state.
Public Class Item
    Public Property Name As String
    Public Property Val As String

    Public Sub New()
        Name = String.Empty
        Val = "0"
    End Sub

    Public Sub New(ByVal nm As String, ByVal value As String)
        Name = nm
        Val = value
    End Sub

    Public Function Print() As String
        Return String.Format("Name: {0}, Value: {1}", Name, Val)
    End Function
End Class


Public Class Form6

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        '-- create your item object.
        Dim _bar As New Item("Pwnies", "20")
        MsgBox(_bar.Print) '-- verify we have a value in there

        '-- create your second form object
        Dim _foo As New Form7(_bar) '-- send it item object
        '-- show said form
        _foo.ShowDialog() '-- show that scond form.

        '-- when the second form is closed grab the value of 'item' property
        Dim temp As Item = _foo.MyItem
        '-- show we have a changed value!
        MsgBox(temp.Print)

    End Sub
End Class


Public Class Form7
    '-- the variable in scope of this form.
    Private _item As Item

    '-- how we get the private variable out of the form.
    Public ReadOnly Property MyItem As Item
        Get
            Return _item
        End Get
    End Property

    '-- modified constructor to take in an item object.
    Public Sub New(ByVal myItem As Item)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        '-- set the internal object to the object that was sent in.
        _item = myItem

        '-- set a label to the name that came in
        lblName.Text = _item.Name
        '-- set the editable textbox to the item's value.
        txtValue.Text = _item.Val
    End Sub

    '-- when the form is closing set the internal variable's val property to what ever was last left in the textbox
    Private Sub Form7_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        _item.Val = txtValue.Text
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1