trouble calling a method in form 2 from a button click on form 1, vb.net

﹥>﹥吖頭↗ 提交于 2019-12-23 02:13:21

问题


I have a program for creating a time sheet and when a saved time sheet is loaded by clicking a button on form 1 the data loads to form 2 and then calls a method in form 2 to print the data to form 3. The problem is after the call Form2.Print() there is no data on form 2 if i open it but still works in that the data is printed to form 3. If I remove Form2.Print() the data is loaded on form 2 and i can then click the Print button and if i open form 2 again the data is still in the text boxes. NOTE: Ideally I would just send the data to form 2 and form 3 from the Open button click event on form 1 but the Print() method on form 2 does many things to the program other than just printing making it easier to just call it instead of replicating it in the Open click. Thank You in advance for the help. Cheers!

Form 1 Code

    Private Sub Open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Open.Click
        Dim xmldoc As XmlDocument
        Dim nodelist As XmlNodeList
        Dim node As XmlNode
        Dim objForm2 As Object = Form2 

        xmldoc = New XmlDocument()
        xmldoc.Load("C:\time.xml")
        nodelist = xmldoc.SelectNodes("/Timesheet/Job1")

        For Each node In nodelist
            Dim CustName = node.ChildNodes.Item(0).InnerText
            Form2.txtbxCustName.Text = CustName
            Dim WO = node.ChildNodes.Item(1).InnerText
            Form2.txtbxWONum.Text = WO
        Next

        objForm2.Print()
    End Sub

`

Form 2 Code

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Print()
    End Sub

    Public Sub Print()
        Form3.labelCustName.text = txtbxCustName.text
        Form3.labelWOnum.text = txtbxWOnum.text
        Me.Close()
    End Sub

回答1:


No need to cast Form2 to object then call print . from new instance of your form you will directly call it .And you every time assining new values to text box inside for each block . to achieve what you want you can do many ways Instead of texbox objects I will show with String object.

in your Form2 and Form3 class add

   Public Property xmldata as String 'use your own class or other types List  controls(textbox,.)whatever you want . 

    'You have to do inside open click
    Dim form2 as new Form2()  

If you want initialize form2 and form3 only once and use it accross program then you should add this line to prevent it from disposing when closing()

     Private Sub Form2_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
             Me.Hide()
             e.Cancel = True
     End Sub 


     form2.xmldata=yourxmldata
     form2.print()
     form2.show()'show form2

lemme now if it helped you . if not i will reedit my answer so that u understand




回答2:


Create public variable for those 2 labels ..

Add new module to your project and add this into public sCustName, sWONum as String

And change your Form2 Print() ..

Public Sub Print()
    Form3.labelCustName.text = txtbxCustName.text
    sCustName = txtbxCustName.text
    Form3.labelWOnum.text = txtbxWOnum.text
    sWONum = txtbxWOnum.text
    Me.Close()
End Sub

So if you need to put back in Form 2 then

Form2.txtbxCustName.Text = sCustName
Form2.txtbxWONum.Text = sWONum


来源:https://stackoverflow.com/questions/16638265/trouble-calling-a-method-in-form-2-from-a-button-click-on-form-1-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!