0

Hi I've migrated VB6 code to VB.Net where in VB6 one of the functionality uses modal dialogue which allows user to copy few text from parent form. But in Vb.Net ShowDialog doesn't allow user to copy anything as you guys know it just disables the parent form.

My question is, Is there a way I can enable parent form or else minimize child form to copy few text from parent form?

please don't suggest to use show instead of ShowDialog because I want to achieve this only using ShowDialog.

This VB6 Code.

Form.Show vbModal, objParent

migration wizard has below code

Form.ShowDialog
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
adahero
  • 11
  • 1
  • 6
  • That is an [absolutely essential feature](https://stackoverflow.com/a/5183623/17034), you won't get any help here what that goes wrong. It is possible, you have to pinvoke EnableWindow() to get the parent enabled again. Similar code [is here](https://social.msdn.microsoft.com/Forums/en-US/e960e52e-5914-4819-8d45-79d17a70460a/multiple-nonmodal-windows?forum=winforms), you can put the BeginInvoke() call before the ShowDialog() call. – Hans Passant Sep 06 '19 at 16:21
  • 2
    Why not just pass the text, which *may* be copied, to the popup form, and then access the text from there? Or is it "...may copy one of 50 text boxes"? Better yet, encapsulate the data of the parent form within a class object, and then pass the object to the popup form. – HardCode Sep 06 '19 at 17:22
  • 1
    If you have a Dialog Form that needs to interact with its Owner, you probably don't need a Dialog. Maybe just a Panel with tools that can perform the same actions. Some redesign of the UI is probably needed anyway, when porting an old application (right, that's exactly what you want to hear :). – Jimi Sep 06 '19 at 18:02
  • You gan do it with me.hide in child form, but if you forget to turn back to child form so child form Will always active in background – user11982798 Sep 06 '19 at 18:05

2 Answers2

1

The answer may be one of design, instead of a technical workaround to .ShowDialog(). Let's take your parent form, for example, with text that may be copied for pasting within a popup modal form. I don't know the data in your parent form, so let's call it a Widget.

Public Class Widget

     Public Property ID As Integer = 0
     Public Property TextThatMayBeCopied As String = String.Emtpy

End Class

In your parent form's code, you would load this data into a Widget object from a database, a file, whatever.

Private _widget As Widget = Nothing

Public Sub New()

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

    ' Add any initialization after the InitializeComponent() call.
    ' Assume we want the Widget with ID of 123
    _widget = MyFunction.WhichLoadsWidgetDataAndReturnsWidgetObject(123)
    DisplayData()

End Sub

Private Sub DisplayData()

    txtID.Text = _widget.ID
    txtTextThatMayBeCopied.Text = _widget.TextThatMayBeCopied

End Sub

Private Sub btnShowDialog_Click(sender As Object, e As EventArgs) Handles btnShowDialog.Click

    _widget.TextThatMayBeCopied = txtTextThatMayBeCopied.Text.Trim

    Dim f As New MyShowDialogForm(_widget)
    f.ShowDialog

End Sub

Your target form MyShowDialogForm would take in it's own constructor an object of type Widget:

Private _widget As Widget = Nothing

Public Sub New(widget As Widget)

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

    ' Add any initialization after the InitializeComponent() call.
    _widget = widget

End Sub

You can now access the data passed to form MyShowDialogForm via the _widget object, for example, in a button click event for btnCopyText, or however you need.

The key takeaway here is to use a method of exchanging data within different forms. Typically it becomes very messy code to use the Form classes themselves as the encapsulation for data. Instead, use classes for encapsulating data and moving it around your app.

HardCode
  • 6,497
  • 4
  • 31
  • 54
0

'For example we have 2 form, form1 (main) and form2 (child)

'This Form as Main
Public Class Form1
    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        'if you don't have timer in child form, you must click again this button
        'after you form back awhile to this form as main form
        Form2.ShowDialog()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
        Me.Dispose()
    End Sub
End Class

'This Form as Child
Public Class Form2
    Private Sub btnToMainAWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToMainAWhile.Click
        'you hide this current form to caller form, you must back here again, 
        'if not this form always active in background
        'or if you have timer1 here with enabled property =false here, you can add this:
        'Timer1.Interval = 10000
        'Timer1.Enabled = True
        Me.Hide()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
        Me.Dispose()
    End Sub

    'if you have timer1 and you will wait for many seconds back to main form, add this:
    'Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    '    Timer1.Enabled = False
    '    Me.ShowDialog()
    'End Sub
End Class
user11982798
  • 1,878
  • 1
  • 6
  • 8