Search code examples
c#.netwinformscustom-controlspanel

Inside a panel , show a pop up message with dark background


My problem is related to this How to show a pop up message with dark background

but the main problem is i want to add the fade form and the focus form both inside a panel . I have asked this question in many forums but no one answered . i am using panel to open and close forms but got suggestion to create a user control instead but no one never told me to how to design that user control and how to use that . If any one here has the time here to help me , please do so .

i have a panel in my form and there is form named form1 opened inside a panel . i want to add this fade form over this form1 so that the control of form1 should be partially visible and when this happened , i want to open a focus form of which the link i just shared . Just this i want .


Solution

  • Let me see if I follow.

    i have a panel in my form

    sample panel with three rando buttons


    there is form named form1 opened inside a panel

    public partial class MainForm : Form
    {
        .
        .
        .
        Form1 form1 { get; } = new Form1
        {
            StartPosition = FormStartPosition.Manual,
        };
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            form1.Location = panel1.PointToScreen(new Point(panel1.Right - 370, 50));
            form1.Show(this);
        }
    }
    

    form inside panel


    i want to add this fade form over this form1 so that the control of form1 should be partially visible and when this happened

    animation

    Fade Form

    class FadeForm : Form
    {
        const float TARGET_OPACITY = 0.85F;
        public FadeForm()
        {
            BackColor = Color.DarkGray;
            FormBorderStyle = FormBorderStyle.None;
            Padding = new Padding(0, 0, 0, 20);
            var linkLabel = new LinkLabel
            {
                Dock = DockStyle.Bottom,
                Text = "https://www.google.com",
                TextAlign = ContentAlignment.MiddleCenter,
                Height = 50,
            };
            linkLabel.LinkClicked += (sender, e) =>
            {
                MessageBox.Show($"Do something with {linkLabel.Text}");
            };
            Controls.Add(linkLabel);
        }
        public new async void Show(IWin32Window owner)
        {
            base.Show(owner);
            if (owner is Form parent)
            {
                localSubscribeParent();
                localTrackParent(this, EventArgs.Empty);
                    
                // Animate
                for (float f = 0; f <= TARGET_OPACITY; f += 0.03F)
                {
                    Opacity = Math.Pow(f, 2);
                    await Task.Delay(TimeSpan.FromSeconds(0.01));
                    if (!Visible) break; // If form hides during animation.
                }
                void localSubscribeParent()
                {
                    parent.Move -= localTrackParent;
                    parent.SizeChanged -= localTrackParent;
                    parent.Move += localTrackParent;
                    parent.SizeChanged += localTrackParent;
                }
                void localTrackParent(object? sender, EventArgs e) =>
                    Bounds = parent.RectangleToScreen(parent.ClientRectangle);
            }
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            e.Cancel = true;
            Hide();
        }
    }
    

    Main Form

    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            foreach (var button in panel1.Controls.OfType<Button>())
            {
                button.MouseEnter += (sender, e) =>
                {
                    FadeForm.Hide();
                    FadeForm.Show(form1);
                    switch (button.Name) 
                    {
                        case nameof(button1): FadeForm.BackColor = Color.LightBlue;  break;
                        case nameof(button2): FadeForm.BackColor = Color.LightGreen; break;
                        case nameof(button3): FadeForm.BackColor = Color.LightCoral; break;
                    }
                };
            }
            Disposed += (sender, e) =>
            {
                FadeForm.Dispose();
                form1.Dispose();
            };
        }
        Form1 form1 { get; } = new Form1
        {
            StartPosition = FormStartPosition.Manual,
        };
        FadeForm FadeForm { get; } = new FadeForm();
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            form1.Location = panel1.PointToScreen(new Point(panel1.Right - 370, 50));
            form1.Show(this);
        }
    }
    

    i want to open a focus form of which the link i just shared

    open link

    My crystal ball gets fuzzy at this point about what you want to do next but hopefully this gives you a starting point.