0

enter image description here

Hi Guys,

I got a form with a datagridview in it as shown in the picture. I my app I use this as dialogue window for tax category selection. The problem is I need to use the same dialogue on different forms. I tried to set public variables to send data between forms. When I double click on datagrid row, form closes and text box in another form gets the data. My issue is every time when I close the form the textbox in different forms changes as the public variables changes. Please see my code.

Is there any way to find from which form the Tax category dialogue form is opened, and to send data to that particular form only? Thanks is advance.

Public Class Tax_cata_selector_frm

    Private Sub Tax_cata_selector_frm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        
    Public Property sent_Cat_Id As Integer
    Public Property sent_cata_name As String
    Public Property sent_SGST_rate As Decimal
    Public Property sent_SGST_cess_rate As Decimal
    Public Property sent_CGST_rate As Decimal
    Public Property sent_CGST_cess_rate As Decimal
    Public Property sent_IGST_rate As Decimal
    Public Property sent_IGST_cess_rate As Decimal

    Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        
        Dim i, j As Integer
        i = DataGridView1.CurrentRow.Index
        sent_Cat_Id = Convert.ToInt32(DataGridView1.Item(0, i).Value)
        sent_cata_name = Convert.ToString(DataGridView1.Item(1, i).Value)
        sent_SGST_rate = Convert.ToDecimal(DataGridView1.Item(2, i).Value)
        sent_SGST_cess_rate = Convert.ToDecimal(DataGridView1.Item(3, i).Value)
        sent_CGST_rate = Convert.ToDecimal(DataGridView1.Item(4, i).Value)
        sent_CGST_cess_rate = Convert.ToDecimal(DataGridView1.Item(5, i).Value)
        sent_IGST_rate = Convert.ToDecimal(DataGridView1.Item(6, i).Value)
        sent_IGST_cess_rate = Convert.ToDecimal(DataGridView1.Item(7, i).Value)

        'this is the first form that gets data when this form is closed
        tax_cata_frm.cata_id_txt.Text = sent_Cat_Id
        tax_cata_frm.cata_name_txt.Text = sent_cata_name
        tax_cata_frm.CGST_rate_NUD.Value = sent_CGST_rate
        tax_cata_frm.CGST_cess_rate_NUD.Value = sent_CGST_cess_rate
        tax_cata_frm.SGST_rate_NUD.Value = sent_SGST_rate
        tax_cata_frm.SGST_cess_rate_NUD.Value = sent_SGST_cess_rate
        tax_cata_frm.IGST_rate_NUD.Value = sent_IGST_rate
        tax_cata_frm.IGST_cess_rate_NUD.Value = sent_IGST_cess_rate

        'this is the second form that gets data when this form is closed
        Add_product_frm.Tax_cata_dis_combo.SelectedValue = sent_Cat_Id
        Me.Close()
    End Sub

   
End Class
Ayoob Khan
  • 1,536
  • 2
  • 15
  • 13
  • Possible duplicate of [How can I pass values from one form to another?](http://stackoverflow.com/questions/818930/how-can-i-pass-values-from-one-form-to-another) – T.S. Mar 19 '16 at 03:45

2 Answers2

0

You can try this: Your dialog only shows the data and lets the user select a row. Once the row is selected, you fill you public propoerties and set the DialogResult.

Public Class Tax_cata_selector_frm

    Private Sub Tax_cata_selector_frm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        
    Public Property sent_Cat_Id As Integer
    Public Property sent_cata_name As String
    Public Property sent_SGST_rate As Decimal
    Public Property sent_SGST_cess_rate As Decimal
    Public Property sent_CGST_rate As Decimal
    Public Property sent_CGST_cess_rate As Decimal
    Public Property sent_IGST_rate As Decimal
    Public Property sent_IGST_cess_rate As Decimal

    Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        
        Dim i, j As Integer
        i = DataGridView1.CurrentRow.Index
        sent_Cat_Id = Convert.ToInt32(DataGridView1.Item(0, i).Value)
        sent_cata_name = Convert.ToString(DataGridView1.Item(1, i).Value)
        sent_SGST_rate = Convert.ToDecimal(DataGridView1.Item(2, i).Value)
        sent_SGST_cess_rate = Convert.ToDecimal(DataGridView1.Item(3, i).Value)
        sent_CGST_rate = Convert.ToDecimal(DataGridView1.Item(4, i).Value)
        sent_CGST_cess_rate = Convert.ToDecimal(DataGridView1.Item(5, i).Value)
        sent_IGST_rate = Convert.ToDecimal(DataGridView1.Item(6, i).Value)
        sent_IGST_cess_rate = Convert.ToDecimal(DataGridView1.Item(7, i).Value)
        DialogResult = DialogResult.OK
        Me.Close()
    End Sub

   
End Class

The form that opens your selector now has to check if a row was selected and then set its form elements according to the public properties:

dim dlg as new Tax_cata_selector_frm()
dlg.ShowDialog()

if dlg.DialogResult = DialogResult.OK Then
  me.cata_id_txt.Text = dlg.sent_Cat_Id
  '... repeat for all form elements you want to set
end if

You would have to place this code in all places you call you selection dialog.

schudel
  • 1,235
  • 2
  • 11
  • 18
0

A way that worked for me in the past was to pass the calling form as a parameter like this :-

Public Class Tax_cata_selector_frm(callingForm as Form)

then in your code I edit your code to this so that you have an if statement to figure out which form did the calling

If callingForm Is tax_cata_frm Then
        'this is the first form that gets data when this form is closed
        tax_cata_frm.cata_id_txt.Text = sent_Cat_Id
        tax_cata_frm.cata_name_txt.Text = sent_cata_name
        tax_cata_frm.CGST_rate_NUD.Value = sent_CGST_rate
        tax_cata_frm.CGST_cess_rate_NUD.Value = sent_CGST_cess_rate
        tax_cata_frm.SGST_rate_NUD.Value = sent_SGST_rate
        tax_cata_frm.SGST_cess_rate_NUD.Value = sent_SGST_cess_rate
        tax_cata_frm.IGST_rate_NUD.Value = sent_IGST_rate
        tax_cata_frm.IGST_cess_rate_NUD.Value = sent_IGST_cess_rate
Else If callingForm Is Add_product_frm Then
        'this is the second form that gets data when this form is closed
        Add_product_frm.Tax_cata_dis_combo.SelectedValue = sent_Cat_Id
End If

Finally, in each of your calling forms change the code that calls the dialog to include the parameter Me

David Wilson
  • 4,369
  • 3
  • 18
  • 31