1

I have a mainform, on user clicking a button in the mainform, I need to show a child form as popup in center of mainform. At this stage, all the free spaces(around the inner box in the attached image) on the child form should be transparent(greyed out) so that controls in main forms are visible.

How to achieve this ? I tried using Opacity on child form, but opacity is being applied to all the controls in the form.

enter image description here

Make a form's background transparent: Solution provided in this link just makes fully transparent. I want the opacity effect.

Community
  • 1
  • 1
Rocky
  • 405
  • 7
  • 17
  • is the child form independant of the mainform? or could use other controls? such as panels – BugFinder Apr 25 '16 at 13:59
  • Been a long time since I didn't do any WinForms app. Isn't there an opacity property or the like in the Property Window somewhere? Just guessing from the top of my head. May be irrelevant. – Will Marcouiller Apr 25 '16 at 14:02
  • @BugFinder child form is independent of the mainform. I cannot use it as a panel in the mainform. – Rocky Apr 25 '16 at 14:02
  • Is this a MDI application ? – Mark Hall Apr 25 '16 at 14:37
  • 1
    This os not a question on how to make the form transparent. This is how to make the form partially transparent. Think of a web gallery thickbox. That's what the op is aksing for. – Zohar Peled Apr 26 '16 at 03:40

2 Answers2

2

For your main form in a button press event or whatever triggers the child form you can use the following code:

ChildForm frm = new ChildForm();
frm.ShowDialog();//use this so that the main form depends on the child form
frm.Show(); // use this to show an independent child form

and in the Child Form you can use the Opacity property of the ChildForm to make it transparent, you can change this from the form designer too:

this.Opacity = 50; //or any other value that you like
ArturoAP
  • 432
  • 2
  • 13
  • I tried setting opacity to the form. Opacity is being applied to all the controls in the form. i.e., I want a solid background for the panel in the child form but because of the opacity, panel is also having transparent background. – Rocky Apr 25 '16 at 14:23
2

Here is how I've solved this problem:

I've created a form, and override it's OnPaintBackground method like this:

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        using (SolidBrush brush = new SolidBrush(Color.FromArgb(70, 0, 0, 0)))
        {
            e.Graphics.FillRectangle(brush, e.ClipRectangle);
        }
    }

Inside this form I'm hosting a user control that's basically a panel with a label and a two buttons (OK, Cancel). Once the user clicks a button I set this form's DialogResult to OK or Cancel (depending on the button clicked).

This form is shown from the main form as a dialog (frm.ShowDialog(this)). Also, it takes in it's constructor the main form and set it's own display rectangle to cover the main form completly, and then resize the user control to a third of the height and half of the width of the form, and centers it.

 public  FrmThickBox(Form owner, string message)
 {
    this.Owner = owner;
    this.Width = owner.Width;
    this.Height = owner.Height;
    this.Top = owner.Top;
    this.Left = owner.Left;
    this.thickBoxControl.Text = message;
    this.thickBoxControl.Size = new Size((int)this.Width / 2, (int)this.Height / 3);
    this.thickBoxControl.Top = (int)((this.Height - thickBoxControl.Height) / 2);
    this.thickBoxControl.Left = (int)((this.Width - thickBoxControl.Width) / 2);
 }

And here is how it looks like (of course, the rounded corners are an entire different story):

enter image description here

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • This is what I am looking for. Thanks ! My next requirement is to make rounded corners :-) Is it possible to share the snippets for that as well ? – Rocky Apr 26 '16 at 04:15
  • New topic - new question. I'd love to answer it as well. – Zohar Peled Apr 26 '16 at 04:41
  • I just created a new question http://stackoverflow.com/questions/36855559/c-sharp-child-form-over-main-form-having-greyed-out-effect – Rocky Apr 26 '16 at 05:04
  • @Zohar how do you get past the fact that any Windows.Forms.Form has to have BackColor property without any transparency? At least in VS 2017 seems to be so. Opacity make all controls with same opacity value, and without setting form's opacity, BackColor is always solid (non-transparent). Have been trying to use TransparencyKey without success, also overriding PaintBackground method.... – ahaaman Aug 13 '19 at 06:26
  • IIRC, add this to your form: `protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x20; return cp; } }` – Zohar Peled Aug 13 '19 at 06:32