Vas23Vewe
Vas23Vewe

Reputation: 101

How to dark background when my form opens?

I am writing the app, and a part of it is open textbox. When the textbox is opening I want to dark background.

I have looked the solution and found it here: Creating a dark background when a new form appears

But, it does not work for me correctly.

Here is my code:

private void App_Load(object sender, EventArgs e)
        {
            this.Text = "TestApp";
            this.Size = new Size(350, 250);
            this.BackColor = Color.DarkGray;
            this.Location = new Point(50, 50);
            this.MaximizeBox = false;

            TextBox.BackColor = Color.WhiteSmoke;
            TextBox.Multiline = true;
            TextBox.Size = new Size(200, 90);

            Button.Text = "Search";
            
            Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            using (Graphics G = Graphics.FromImage(bmp))
            {
                G.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
                G.CopyFromScreen(this.PointToScreen(new Point(0, 0)), new Point(0, 0), this.ClientRectangle.Size);
                double percent = 0.60;
                Color darken = Color.FromArgb((int)(255 * percent), Color.Black);
                using (Brush brsh = new SolidBrush(darken))
                {
                    G.FillRectangle(brsh, this.ClientRectangle);
                }
            }
            
            using (Panel p = new Panel())
            {
                p.Location = new Point(0, 0);
                p.Size = this.ClientRectangle.Size;
                p.BackgroundImage = bmp;
                this.Controls.Add(p);
                p.BringToFront();

                // display your dialog somehow:
                Form frm = new Form();
                frm.StartPosition = FormStartPosition.Manual;
                frm.ShowDialog(this);
            }           
        }

I receive this:

enter image description here

Maybe, someone can point me out where is my mistake?

EDIT: I have found the solution, the question was not clear enough.

Upvotes: 0

Views: 221

Answers (1)

Harald Coppoolse
Harald Coppoolse

Reputation: 30454

When the textbox is opening I want to dark background.

So you want the textBox to be dark, not the complete form?

Almost always when you think you have to do some painting yourself, think again. It is seldom necessary do to paint. Only do this, if you don't have any standard options.

Just set Property BackGround of the text box. Use visual studio designer to do this. If you don't want to do this using the designer, do this in the constructor after InitializeComponent:

public MyForm()
{
    InitializeComponent();

    // text box dark background:
    this.textBox1.BackColor = Color.Black;
}

If you want the complete form to be black, again use visual studio designer, or add:

InitializeComponent();
this.BackColor = Color.Black;

Upvotes: 1

Related Questions