locked
How to hide Form by Esc then show form in .Net? RRS feed

  • Question

  • Hi,

    I created a form without a close button (x), I want when press esc it will be hidden and after some event is triggered it will show again. This is the sequence of steps I have taken

    First, to save myform display time, I called the line before code run:

    Dim myform as Classmyform = new classmyform

    and in the ClassMyForm_new event 

    Public Sub New()
            InitializeComponent()
    End sub
    And when the event on the 2nd Class is triggered,I called it : myform.showdialog()

    It displayed as I expected, but after I pressed esc to hide form and when the event triggered the second time, I now encountered this error:

    'Form showDialog tried to set an ineligible form as its owner. Forms cannot own themselves or their owners.

    This's code to hide the form(not me):

      Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        If keyData = Keys.Escape Then
            Me.Hide()
            Return True
        End If
    
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

    Also in the form_load event, I have put a procedure to fill a combobox data every time the form show comes back.

    I don't know if that was the reason or because I pressed esc so the form canceled itself. How should I solve this problem?

    Thanks a lot!

    • Edited by Khanh dang Thursday, April 30, 2020 6:22 AM
    Wednesday, April 29, 2020 11:58 PM

Answers

  • Hello, it seems that this problem is because I inherited the Form from another library, if I reset the inherit Form, everything works fine.
    Thursday, April 30, 2020 7:19 AM

All replies

  • First thing to try is myForm.ShowDialog(Me)

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, April 30, 2020 1:33 AM
  • Thanks @Karen,

    however this event is from another class, so if I set myform.showdialog(myform) , the same error still occurs.
    I will edit the post to present more.

    Edit: I tried myform.showdialog(me) , and I got the error System.InvalidCastException: 'Unable to cast object of type' Class2Form 'to type' System.Windows.Forms.IWin32Window '.'




    • Edited by Khanh dang Thursday, April 30, 2020 6:34 AM
    Thursday, April 30, 2020 6:12 AM
  • Hello, it seems that this problem is because I inherited the Form from another library, if I reset the inherit Form, everything works fine.
    Thursday, April 30, 2020 7:19 AM
  • Hi AU Gridlock,

    It seems that your problem has been solved. If so, please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, April 30, 2020 8:30 AM