Click here to Skip to main content
15,569,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a curved shaped textbox by using the class while I am dragging the curved shaped textbox in screen it shows correctly while I am fetching the same textbox inside picture box the shape doesn't shown....pls guide

What I have tried:

using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace RoundTextBox
{
   public class ceLearningTextbox : Control
    {
        private int radius = 15;
        public TextBox textBox = new TextBox();
        private GraphicsPath shape;
        private GraphicsPath innerRect;
        private Color br;

        public ceLearningTextbox()
        {
            base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            base.SetStyle(ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.ResizeRedraw, true);
            this.textBox.Parent = this;
            this.Controls.Add(this.textBox);
            this.textBox.BorderStyle = BorderStyle.None;
            textBox.Font = this.Font;
            this.BackColor = Color.Transparent;
            this.ForeColor = Color.Black;
            this.br = Color.White;
            textBox.BackColor = this.br;
            this.Text = null;
            this.Font = new Font("Century Gothic", 12f);
            base.Size = new Size(135, 33);
            this.DoubleBuffered = true;
            textBox.KeyDown += new KeyEventHandler(textbox_keydown);
            textBox.TextChanged += new EventHandler(textbox_TextChanged);
            textBox.MouseDoubleClick += new MouseEventHandler(textbox_MouseDoubleClick);

        }

        private void textbox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                textBox.SelectAll();
            }
        }

        private void textbox_TextChanged(object sender, EventArgs e)
        {
            this.Text = textBox.Text;
        }

        private void textbox_keydown(object sender, KeyEventArgs e)
        {
            if(e.Control && (e.KeyCode == Keys.A))
            {
                textBox.SelectionStart = 0;
                textBox.SelectionLength = this.Text.Length;
            }
        }

        protected override void OnFontChanged(EventArgs e)
        {
            base.OnFontChanged(e);
            textBox.Font = this.Font;
            base.Invalidate();
        }

        protected override void OnForeColorChanged(EventArgs e)
        {
            base.OnForeColorChanged(e);
            textBox.ForeColor = this.ForeColor;
            base.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            this.shape = new MyRectangle((float)base.Width, (float)base.Height, (float)this.radius, 0f, 0f).path;
            this.innerRect = new MyRectangle(base.Width - 0.5f, base.Height - 0.5f, (float)this.radius, 0.5f, 0.5f).path;
            if(textBox.Height >= (base.Height - 4))
            {
                base.Height = textBox.Height + 4;
            }
            textBox.Location = new Point(this.radius - 5, (base.Height / 2) - (textBox.Font.Height / 2));
            textBox.Width = base.Width - ((int)(this.radius * 1.5));
            e.Graphics.SmoothingMode = ((SmoothingMode)SmoothingMode.HighQuality);
            Bitmap bitmap = new Bitmap(base.Width, base.Height);
            Graphics graphics = Graphics.FromImage((Image)bitmap);
            e.Graphics.DrawPath(Pens.Black, this.shape);
            using(SolidBrush brush=new SolidBrush(this.br))
            {
                e.Graphics.FillPath((Brush)brush, this.innerRect);

            }
            Trans.Maketransparent(this, e.Graphics);
            base.OnPaint(e);
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            textBox.Text = this.Text;
        }
        public void selectAll()
        {
            textBox.SelectAll();
        }
        public Color Br
        {
            get =>
                this.br;
            set
            {
                this.br = value;
                if(this.br != Color.Transparent)
                {
                    textBox.BackColor = this.br;
                }
                base.Invalidate();
            }
        }

        public override Color BackColor 
        { 
            get => base.BackColor; 
            set => base.BackColor = Color.Transparent; 
        }
    }
}
Posted
Updated 14-Feb-21 21:37pm
v2
Comments
Gerry Schmitz 4-Feb-21 7:05am    
How can you have a TextBox "inside" a PictureBox? It can be "in front" or "behind"; not "inside".
Member 15028582 4-Feb-21 7:11am    
Yes ur right ji....but infront also the textbox border not shown properly....
Gerry Schmitz 4-Feb-21 9:44am    
That's got nothing to do with the PictureBox. Maybe you should be looking at "warping" a plain TextBox, or putting a plain, transparent TB over something else that is "curved".

1 solution

You can draw whatever you want. See:
C#
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawLine(
        new Pen(Color.Red,2f),
        new Point(0,0),
        new Point(pictureBox1.Size.Width, pictureBox1.Size.Height ));

    e.Graphics.DrawEllipse(
        new Pen(Color.Red, 2f),
        0,0, pictureBox1.Size.Width, pictureBox1.Size.Height  );
}

Source: c# - How do I draw a circle and line in the picturebox? - Stack Overflow[^]

The same way you can draw text:
C#
using (Font myFont = new Font("Arial", 14))
{
    e.Graphics.DrawString("Hello .NET Guide!", myFont, Brushes.Green, new Point(2, 2));
}

Source: c# - How to draw text on picturebox? - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900