Howto: Return data from one form to another

Locked Howto: Return data from one form to another

  • 2005년 6월 19일 일요일 오전 3:29
     
     
    Hi!

    I want to return data to calling point of  the form, but i can't return data when the called form closes. Please tell me how can i return data from one from to another.


    Shani!

모든 응답

  • 2005년 6월 19일 일요일 오전 11:56
    중재자
     
     
    Hi,

    just use properties to do so. and before closeing save the data you want in those properties.
    in the closing event of form 2 it coul look like this.

    form1.Name = "Shani";
  • 2005년 7월 3일 일요일 오후 12:23
     
     
    also you can make a function in some form to pass parameters to other form

    class form1
    dim f2 as new form2
    f2.getDataFromOtherForm(20)
    f2.show
    end class

    class form2
    dim val as int16
    public sub getDataFromOtherForm(i as int16)
       val=i
    end sub
    end class

    I hope this helps Big Smile
  • 2005년 7월 22일 금요일 오후 4:50
     
     답변됨

    A simple scenario
    Form1 has a button which when you click on displays form2 which has a simple textbox and two buttons.  One button on form2 dialog has a dialog result of OK and the other of cancel.
    If OK button is pressed then it sets the property (TextboxText) on form2 and closes returning a dialog result of OK.  I button2 is click it closes the form and returns a dialog result of Cancel.

    When the Form2 is closed and the focus returns to the caller procedure in form1 (Button1_Click).   The Caller procedure looks to see that the OK button was pressed by examining the dialogresult property and if it is set to OK, retrieves the property from form2 reference.


    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim oForm2 As New Form2

            oForm2.ShowDialog()
            If oForm2.DialogResult = Windows.Forms.DialogResult.OK Then
                Label1.Text = oForm2.TextboxText
            End If

        End Sub
    End Class

     

    Public Class Form2
        Private strText As String

        Public Property TextboxText() As String
            Get
                Return strText
            End Get
            Set(ByVal value As String)
                strText = value
            End Set
        End Property
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.strText = TextBox1.Text
            Me.Close()
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Me.Close()
        End Sub
    End Class




  • 2005년 7월 24일 일요일 오전 8:02
     
     
    Staring from form1

    when calling form2 you do the following

    dim fv as new form2
    fv.owner = me
    fv.show


    you show also add the following sub to form1

    Public Sub Settxtbox1(ByVal NewText As String)

    txtbox1.Text = NewText

    End Sub

    This Sub will write the returned value to the txtbox desired


    now in form2 when clicking button1 for example to return the contents of a textbox2 to txtbox1 in form1, you add the followng code to the button1 click event

    Dim fv As form1= DirectCast(Me.Owner, form1)

    fv.txtbox1(txtbox2.Text)
    me.dispose


    hope it helps

  • 2005년 7월 25일 월요일 오후 9:50
     
     
    Hello Shani,


    This help topic shows how to pass data between forms:




    Walkthrough: Passing Data between Windows Forms
    http://msdn2.microsoft.com/library/ms171925(en-us,vs.80).aspx


    hope that helps,
  • 2005년 8월 24일 수요일 오후 10:58
     
     
    Hi Spotty-
    I tried your example and it worked great.  When I tried to adjust it to my situation, I couldn't get it to work.  Obviously, I'm new to this. 

    Here's my problem:

    I have a checkedListBox with seven items in form2 and want any checked items to show up on a label in form1 when OK is pressed.

    Could you please help?
    Thanks in advance.