New Form is not shown while drag & drop a Custom Control from toolbox to Designer in WinForms in NetCore

Karkuvel Rajan Shanmugavel 20 Reputation points
2024-05-03T14:04:08.6633333+00:00

In our Control, we have opened a new form to update the content of our control in the Designer. The new Form is not shown in the NetCore but works properly in the NetFrameWork. I have replicated the same issue in a simple control.

    [ToolboxItem(true)] // This attribute makes the control visible in the Toolbox
    public class CustomButton : Button
    {
        // Constructor
        public CustomButton()
        {
            // Set default properties
            this.BackColor = Color.Blue;
            this.ForeColor = Color.White;
            this.Text = "Custom Button";

            Form FileLoadForm = new Form();
            FileLoadForm.Controls.Add(new Button());
            FileLoadForm.ShowDialog();
        }
    }

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,891 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,845 questions
{count} votes

1 answer

Sort by: Most helpful
  1. KOZ6.0 6,400 Reputation points
    2024-05-06T04:38:32.2766667+00:00

    Perhaps the window is hidden behind it.

    [ToolboxItem(true)] // This attribute makes the control visible in the Toolbox
    public class CustomButton : Button
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
    
        // Constructor
        public CustomButton() {
            // Set default properties
            this.BackColor = Color.Blue;
            this.ForeColor = Color.White;
            this.Text = "Custom Button";
    
            Form FileLoadForm = new Form();
            FileLoadForm.Controls.Add(new Button());
            FileLoadForm.HandleCreated += (s, e) => {
                SetForegroundWindow(((Control?)s)!.Handle);
            };
            FileLoadForm.ShowDialog();
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.