none
Passing a parameter from a form to anothe form in vb.net windows form

    Question

  • I have two form

    Form1

    In form1 i have two radio buttons and three text box and two buttons

    Form2

    in form2 i have check box  list

    In form 1 i have filled one textbox with a value  and i selected one radiobutton

    then i want to fill another textbox ,for it i click on the button ,it call to form2

    then i will select  items from check list box

    then i clcik on the form close button(form titile bar of minimize,maximize,close)

    I want to get the lsit of checked item that i assigned to a sting to Form 1 of intented textbox without lost the  initial radio button selection and first textbox of value that i fiiled very first time.

    anybody can provide solution for it

    In first form

    I am calling form2  as

     

    Try
     

     

    Dim oALLPCC As New ALLPCC

    oALLPCC.Show()

     

    Catch ex As Exception

    MessageBox.Show(ex.Message, "General Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

     

    End Try
     

      And Form2 Closed event

    Private

    Sub ALLPCC_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

     

     

     

    If lstSelectedAgency.Items.Count > 0 Then

    strALLagency = Global.GetStringFromListBoxwithComa(lstSelectedAgency)

     

    Dim oDSRReport As New DSRReport(strALLagency)

    oDSRReport.Show()

     

    End If

     

    Catch ex As Exception

    MessageBox.Show(ex.Message, "General Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

     

    End Try

     

    End Sub

    Try
    AK
    Tuesday, July 05, 2011 12:15 PM

Answers

  • First tell me is DSRReport your Form1 as ALLPCC represents Form2. If It is your Form1, then why do you need to call it again in your Form_Closing of ALLPCC.

    You must have added code to show DSRReport after oALLPCC.ShowDialog() and have the same in Form_Closing of oALLPCC.

    ' DSRReport button click 
    
    dim selectedItems as String = ""
    
    Dim oALLPCC as new ALLPCC
    oALLPCC.ShowDialog()
    // When the oALLPCC is closed, the follpowing lines wipll be executed next.
    
    seletedItems = oALLPCC.getSelectedItems()
    oALLPCC = null;
    if (selectedItems.Length > 0) {
      ' USE THIS STRING WHEREEVER YOU WANT TO USE AND SET THE VALUE OF A CONTROL. eG : set text of a Label label1 :
      label1.text = selectedItems
    }
    
    
    
    IN ALLPCC form :
    dim items as string = ""
    
    sub getSelectedItems() as String
     return items;
    End Sub
    
    
    Sub ALLPCC_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
     
     
     If lstSelectedAgency.Items.Count > 0 Then 
     items = Global.GetStringFromListBoxwithComa(lstSelectedAgency)
     else
     items = ""
    end sub
    
    

    Do it as shown and your stuff will be done.

    If have any more issues, ask freely.

     


    Thanks
    • Marked as answer by Helen Zhou Friday, July 15, 2011 7:42 AM
    Wednesday, July 06, 2011 7:48 AM

All replies

  • I have two form

    Form1

    In form1 i have two radio buttons and three text box and two buttons

    Form2

    in form2 i have check box  list

    In form 1 i have filled one textbox with a value  and i selected one radiobutton

    then i want to fill another textbox ,for it i click on the button ,it call to form2

    then i will select  items from check list box

    then i clcik on the form close button(form titile bar of minimize,maximize,close)

    I want to get the lsit of checked item that i assigned to a sting to Form 1 of intented textbox without lost the  initial radio button selection and first textbox of value that i fiiled very first time.

    anybody can provide solution for it

    In first form

    I am calling form2  as

     

    Try
     

     

    Dim oALLPCC As New ALLPCC

    oALLPCC.Show()

     

    Catch ex As Exception

    MessageBox.Show(ex.Message, "General Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

     

    End Try

      And Form2 Closed event

    Private

    Sub ALLPCC_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

     

    Try

     

    If lstSelectedAgency.Items.Count > 0 Then

    strALLagency = Global.GetStringFromListBoxwithComa(lstSelectedAgency)

     

    Dim oDSRReport As New DSRReport(strALLagency)

    oDSRReport.Show()

     

    End If

     

    Catch ex As Exception

    MessageBox.Show(ex.Message, "General Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

     

    End Try

     

    End Sub

     

     


    AK
    • Merged by Helen Zhou Friday, July 08, 2011 8:05 AM duplicated
    Tuesday, July 05, 2011 11:24 AM
  • now i am getting same form with multiple times when each close event.

    Plz give a solution for it


    AK
    Tuesday, July 05, 2011 11:39 AM
  • <p>Hi AK, </p><p>  I believe you are calling Form2 from Form1. To get the list of chcked item from Form2 to Form1, you have the following ways :</p><ol></ol><p> </p><p>// Calling Form2 from FORM1
    dim selectedItems as String = ""
    
    Dim oALLPCC as new ALLPCC
    oALLPCC.ShowDialog()
    // When the oALLPCC is closed, the follpowing lines wipll be executed next.
    
    seletedItems = oALLPCC.getSelectedItems()
    oALLPCC = null;
    if (selectedItems.Length > 0) {
      Dim oDSRReport As New DSRReport(strALLagency) 
      oDSRReport.Show()
    }
    
    
    
    IN ALLPCC form :
    dim items as string = ""
    
    sub getSelectedItems() as String
      return items;
    End Sub
    
    
    Sub ALLPCC_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
     
     
     If lstSelectedAgency.Items.Count > 0 Then 
      items = Global.GetStringFromListBoxwithComa(lstSelectedAgency)
     else
      items = ""
    end sub
     </p>
    

    2. If you can't do as above then create a static variable in Form1 and in oALLPCC class while FormClosing, set the static value to Form1.ITEMS = Global.GetStringFromListBoxwithComa(lstSelectedAgency)

    3. OR Send an instance of Form1 to oALLPCC while initializing and then set the items by calling a method of Form1 in oALLPCC_FormClosing.

     

    I personally wuoly prefer and recommend 1 or 2nd option and not the 3rd one. As in 3rd, I am passing an object to another wen there is no such need to increase the size of oALLPCC just for setting a variable in the end. If 1 & 2 are not possible in your case then I would go for the 3rd one. But first try with 1st & 2nd ones.

     

    Hope this helps !

     

     


    Thanks
    Tuesday, July 05, 2011 1:42 PM
  • Have replied at your other question http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c3639a84-ef72-48ea-831e-6c7667952335

    Have alook at teh solution provided with varius ways t achieve the same.

     


    Thanks
    Tuesday, July 05, 2011 1:45 PM
  • oALLPCC = null;
      what is this
    AK
    Tuesday, July 05, 2011 1:58 PM
  • Oh sorry, its vb, so

    oALLPCC = nothing

    When the need of an instance is no more, making it null/nothing is a good practise. You have created an instance of oALLPCC, when the work is done, its no more required, so make it to NOTHING.


    Thanks
    Tuesday, July 05, 2011 2:07 PM
  • i done the same way u told by using the code

     

    DSRReport form of a button clcik event

    dim selectedItems as String = ""

    Dim oALLPCC as new ALLPCC
    oALLPCC.ShowDialog()
    ---------------------

    and   ALLPCC closed event i made the code bellow

     

    Private Sub ALLPCC_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

     

    Try

    strALLagency = Global.GetStringFromListBoxwithComa(lstSelectedAgency)

     

    Dim oALLPCC As New ALLPCC

    oALLPCC =

    Nothing

     

    If lstSelectedAgency.Items.Count > 0 Then

     

    Dim oDSRReport As New DSRReport(strALLagency)

    oDSRReport.Show()

    End

    If

     

    Catch ex As Exception

    MessageBox.Show(ex.Message, "General Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

     

    End Try

     

    End Sub

     

    -----------

    Now the problem is

    DSRReport is getting more than one time whenever i am trying to close button click with filled data. But i want to fill the data in very first form of DSRReport .no need to show the data in new instance of DSRReport . I never want to create new instance of DSRReport . I want to show the data in DSRReport  that i loaded first when i click on the menu at very first time.

    Plz give me solution for it

    Thank u

     

     


    AK
    Tuesday, July 05, 2011 2:10 PM
  • i done the same way u told by using the code

     

    DSRReport form of a button clcik event

    dim selectedItems as String = ""

    Dim oALLPCC as new ALLPCC
    oALLPCC.ShowDialog()
    ---------------------

    and   ALLPCC closed event i made the code bellow

     

    Private Sub ALLPCC_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

     

    Try
     

    strALLagency = Global.GetStringFromListBoxwithComa(lstSelectedAgency)

     

    Dim oALLPCC As New ALLPCC

    oALLPCC =

    Nothing
     

     

    If lstSelectedAgency.Items.Count > 0 Then

     

    Dim oDSRReport As New DSRReport(strALLagency)

    oDSRReport.Show()

    End

     

     

    Catch ex As Exception

    MessageBox.Show(ex.Message, "General Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

     

    End Try

     

    End Sub

     

    -----------

    Now the problem is

    DSRReport is getting more than one time whenever i am trying to close button click with filled data. But i want to fill the data in very first form of DSRReport .no need to show the data in new instance of DSRReport . I never want to create new instance of DSRReport . I want to show the data in DSRReport  that i loaded first when i click on the menu at very first time.

    Plz give me solution for it

    Thank u

    If


    AK
    Wednesday, July 06, 2011 3:23 AM
  • First tell me is DSRReport your Form1 as ALLPCC represents Form2. If It is your Form1, then why do you need to call it again in your Form_Closing of ALLPCC.

    You must have added code to show DSRReport after oALLPCC.ShowDialog() and have the same in Form_Closing of oALLPCC.

    ' DSRReport button click 
    
    dim selectedItems as String = ""
    
    Dim oALLPCC as new ALLPCC
    oALLPCC.ShowDialog()
    // When the oALLPCC is closed, the follpowing lines wipll be executed next.
    
    seletedItems = oALLPCC.getSelectedItems()
    oALLPCC = null;
    if (selectedItems.Length > 0) {
      ' USE THIS STRING WHEREEVER YOU WANT TO USE AND SET THE VALUE OF A CONTROL. eG : set text of a Label label1 :
      label1.text = selectedItems
    }
    
    
    
    IN ALLPCC form :
    dim items as string = ""
    
    sub getSelectedItems() as String
     return items;
    End Sub
    
    
    Sub ALLPCC_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
     
     
     If lstSelectedAgency.Items.Count > 0 Then 
     items = Global.GetStringFromListBoxwithComa(lstSelectedAgency)
     else
     items = ""
    end sub
    
    

    Do it as shown and your stuff will be done.

    If have any more issues, ask freely.

     


    Thanks
    • Marked as answer by Helen Zhou Friday, July 15, 2011 7:42 AM
    Wednesday, July 06, 2011 7:48 AM
  • Hello Mujeeb85a,

    This topic always been popular so if do searching you will find a lot of ready code that do the work as you want, such as :
    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/40a492c6-56ab-4d9b-8621-eb79aaa53bbe/
    http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/2ffe8a5d-2b5d-432e-b492-290552830f67
    http://archive.msdn.microsoft.com/MitjaBonca/Release/ProjectReleases.aspx?ReleaseId=5414 

    You can get a lot of useful information from them about this topic.


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 15, 2011 7:53 AM