George Michael
George Michael

Reputation: 43

Change panel border color at runtime

My problem is that i am trying to change a panel border color upon the activation of a textbox placed inside, any help will be much appreciated

Upvotes: 0

Views: 198

Answers (2)

George Michael
George Michael

Reputation: 43

I end up using the the enter and leave event of the text box as follow.

In the enter event

this.searchPanel.BorderStyle=BorderStyle.None;
        this.searchPanel.Refresh();
        ControlPaint.DrawBorder(
            graphics: this.searchPanel.CreateGraphics(),
            bounds: this.searchPanel.ClientRectangle,
            color: System.Drawing.Color.FromArgb(0,120,215),
            style: ButtonBorderStyle.Solid);

And in the leave event

this.searchPanel.BorderStyle = BorderStyle.FixedSingle;
this.searchPanel.Refresh();

Before enter image description here Afterenter image description here

Upvotes: 0

IFrank
IFrank

Reputation: 513

You can use the event Click of your TextBox (use the Property window from Form designer).

Then add the code:

    private void textbox1_Click(object sender, EventArgs e)
    {
        ControlPaint.DrawBorder(panel1.CreateGraphics(), panel1.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);
    }

Obviously you can change the trigger event and adjust the color on your needs.

Upvotes: 1

Related Questions