Question 2 Forms Problem..

capedech

Well-known member
Joined
Oct 29, 2008
Messages
62
Programming Experience
Beginner
Hi,
I have 2 forms : "Form1" and "Form2"
Form1 have DataGridView (Dgv1), and ContextMenuStrip in it.
DataGridView have 2 Columns : "Code" and "Name"
ContextMenusStrip can do : "Add", "Edit", and "Delete"

Form 2 have 2 TextBoxes : TxtCode, TxtName.
And 2 Buttons : BtnOK, BtnCancel.

Code in Form1 :
VB.NET:
If I click "Add" on ContextMenuStrip, then :
Dim frForm2 as New Form2
With frForm2
  .ShowDialog = Cancel Then Exit Sub
  Dgv1.Rows.Add(0, 1)
  Dgv1.Rows(0).Cells("Code").Value = .TxtCode.Text.Trim
  Dgv1.Rows(0).Cells("Name").Value = .TxtName.Text.Trim
End  With

Code in Form2 :
VB.NET:
If I click "BtnOK", then :
Me.DialogResult = OK
Me.Hide or Me.Close (I forget)

If I click "BtnCancel", then :
Me.DialogResult = Cancel
Me.Hide or Me.Close (I forget)

At that point, I can do "Add", but I can't do "Edit" and "Delete".
How to open a form that already being Closed or Hided ?
If I input the 1st row : Code = Code1, Name = Jimmy, and Press OK, the 1st row at Dgv1 will be : Code1, and Jimmy.
I'm thinking to search the Form2 based on matching the Code at Dgv1 and Form2.TxtCode.Text.

I was thinking using For Each, but I don't know how is the coding.
Please help me, guys.

Thank you very much.

Regards,
 
capedech, it looks like you are making a popup screen for adding and editing, since you are making an instance just close it(poss dispose it), when you add/edit again you will be generating another instance. You can use the same form, maybe use 2 buttons for add/edit with diff code for handling which ever one is clicked. The delete will be easier as that can stay on the main form (row.delete). How are you passing the data b/t forms?
 
capedech, it looks like you are making a popup screen for adding and editing, since you are making an instance just close it(poss dispose it), when you add/edit again you will be generating another instance. You can use the same form, maybe use 2 buttons for add/edit with diff code for handling which ever one is clicked. The delete will be easier as that can stay on the main form (row.delete). How are you passing the data b/t forms?
Hi newguy,
I know it will be much easier if I used 1 form only, but my program is much more complicated than what I'm showing u. I need 2 forms for it.
I am thinking using For Each but I don't know how.
Maybe :
VB.NET:
Dim frForm2 as Form2
Dim FrArr as Form
For Each FrArr In [Don't know]
 If FrArr = Form2 Then
  frForm2 = CType (Form2, FrArr)
  If frForm2.TxtCode.Text.Trim = Dgv1.SelectedRows(0).Cells("Code").Value Then
   frForm2.ShowDialog
   {Coding}{Coding}
   Exit For
  End If
 End If
Next

Maybe like that, but I'm not sure how -.-'..
 
It does sound like you want a popup form for doing edits. You can achieve what you want with only one form and handle the events.

But I did a quick little mock up for 2 forms. See if it meets your needs.

Form1:
VB.NET:
Public Class Form1

    Private Sub ContextMenuClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAdd.Click, mnuDelete.Click, mnuEdit.Click

        Dim GridRow(dgvForm1.Rows(0).Cells.Count - 1) As String
        Dim MenuItemClicked = DirectCast(sender, ToolStripMenuItem).Name.Substring(3).ToUpper()

        If MenuItemClicked = "ADD" Then
            'add
            Dim frm2 As Form = New Form2(GridRow, MenuItemClicked)

            If frm2.ShowDialog() = Windows.Forms.DialogResult.OK Then
                'update 
                'add row to dgv
                dgvForm1.Rows.Add(GridRow)

            End If
        Else
            'edit or delete
            If dgvForm1.SelectedRows.Count > 0 Then
                Dim RowSelectedIndex = dgvForm1.Rows.IndexOf(dgvForm1.SelectedRows(0))

                If MenuItemClicked = "EDIT" Then
                    ' edit the selected row
                    GridRow(0) = dgvForm1.Rows(RowSelectedIndex).Cells("colCode").ToString()
                    GridRow(1) = dgvForm1.Rows(RowSelectedIndex).Cells("colName").ToString()

                    Dim frm2 As Form = New Form2(GridRow, "edit")
                    If frm2.ShowDialog() = Windows.Forms.DialogResult.OK Then

                        'update
                        dgvForm1.Rows(RowSelectedIndex).Cells("colCode").Value = GridRow(0)
                        dgvForm1.Rows(RowSelectedIndex).Cells("colName").Value = GridRow(1)
                    End If

                Else
                    'deletes the selected row
                    dgvForm1.Rows.RemoveAt(RowSelectedIndex)

                End If 'end check for edit or delete
            End If 'end check for selected row
        End If 'end check for menuitemclicked

    End Sub
End Class

Form2:
VB.NET:
Public Class Form2

    Dim PassedValues() As String
    Dim HowFormWasLoaded As String


    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub New(ByRef RowToChange() As String, ByVal HowToLoadForm As String)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        PassedValues = RowToChange
        HowFormWasLoaded = HowToLoadForm
    End Sub

    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtCode.Focus()

        'set this as the default return type
        'Me.DialogResult = Windows.Forms.DialogResult.Cancel

        Select Case HowFormWasLoaded.ToUpper()
            Case "ADD"
                txtCode.Text = ""
                txtName.Text = ""
            Case "EDIT"
                txtCode.Text = PassedValues(0).ToString()
                txtName.Text = PassedValues(1).ToString()

        End Select

    End Sub

    Private Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
        Select Case DirectCast(sender, Button).Tag.ToString.ToUpper
            Case "OK"
                'check for validity of values
                Dim isOk As Boolean = True

                If isOk Then
                    'values ok add them to the datagridview
                    PassedValues(0) = txtCode.Text
                    PassedValues(1) = txtName.Text
                Else

                    'values not entered ok
                    Exit Sub

                End If

                'return dialog result
                Me.DialogResult = Windows.Forms.DialogResult.OK


            Case "CANCEL"

                Me.DialogResult = Windows.Forms.DialogResult.Cancel

        End Select

        'close the form no matter which button was pressed.
        Me.Close()

    End Sub
End Class
 
@demausdauth
Thanks for the code bro.
It is very indeed nice coding.

But the code that u wrote already have been thought by me.
It's my back up coding, it is my last resort.

Actually, what I really want is, is there a way to collect all forms from an application, then select Form2 from them.

Thank you very much and I'm so soryy for ur trouble.

Regards,
 
I think this is what you're looking for, however if you're looking for a way to pass the values back from the second form, I did show that with my code.

This will let you loop through the openforms, I am not sure if it sees ones that are hidden, I would think so though.
VB.NET:
 Dim frForm2 As New Form2

        For Each frmToCheck As Form In My.Application.OpenForms
            If System.Object.Equals(frmToCheck.GetType, frForm2.GetType) Then
                frForm2 = DirectCast(frmToCheck, Form2)

            End If

        Next
 
@demausdauth
Bro, I've tried ur coding.
It looks like it can't search hidden forms.
I think I'm gonna use my last resort coding -.-'.
But it's ok. As long as it performs its duty.

Thanks a lot man ^.^.

Regards,

Peace Out.
 
Back
Top