1

I'm in need of a way to make TextBox appear like a parallelogram but i can't figure out how to do so. I currently have this code:

private void IOBox_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    Point cursor = PointToClient(Cursor.Position);
    Point[] points = { cursor, new Point(cursor.X + 50, cursor.Y), new Point(cursor.X + 30, cursor.Y - 20),
                         new Point(cursor.X - 20, cursor.Y - 20) };
    Pen pen = new Pen(SystemColors.MenuHighlight, 2);
    g.DrawLines(pen, points);
}

But apparently it's not working. Either i misplaced/misused it or i'm not doing something right. This is the method that i use to add it.

int IOCounter = 0;
private void inputOutput_Click(object sender, EventArgs e)
{
    IOBox box = new IOBox();
    box.Name = "IOBox" + IOCounter;
    IOCounter++;
    box.Location = PointToClient(Cursor.Position);
    this.Controls.Add(box);
}

Any idea how i can fix it? IOBox is a UserControl made by me which contains a TextBox. Is that rightful to do?

Nathan A
  • 11,059
  • 4
  • 47
  • 63
Asad Ali
  • 684
  • 6
  • 17
  • 1
    It's not easy. You can take a look [here](http://www.codeproject.com/Articles/17453/Textbox-with-rounded-corners). You can also consider to make your application using WPF technology that allow you to change the controls' design easily – Tinwor May 19 '14 at 17:01
  • The TextBox is a low level Windows component and cannot be easily restyled using GDI+. There is limited support for altering any standard Windows controls and in general I'd expect to see graphics artefacts with any solution that tries. As previous suggestion if you want to move away from standard WinForms controls, I think WPF is the better alternative. – PhillipH May 19 '14 at 17:12
  • Pretty much covers the same ground as [this question](http://stackoverflow.com/questions/4360301/can-a-background-image-be-set-on-a-winforms-textbox). Same answer: no. – Hans Passant May 19 '14 at 17:23
  • The thing is, i'm not good at all with WPF and it looks messy so i'm stuck with it. Do you have any good tutorial for me to start with? – Asad Ali May 19 '14 at 18:05
  • 1
    I think by choosing a UserControl you gave it a good start, probably the only clean and practical way using an actual TextBox. In what way is it not working and how should it look? Can you post an image? Also: within the Paint event all coordinates should probably be __relative to the UC__, starting with (0,0) or so and not relative to the Cursor! – TaW May 24 '14 at 17:24
  • @TaW I used @icemanind 's code and adjusted it to be a proper parallelogram. It's working quite well. I had thought about putting a `PictureBox` in the background and making the `TextBox` trasparent but it wasn't necessary; Thanks for your concern :) – Asad Ali May 24 '14 at 17:43
  • Cool! I had thought it wouldn't, judging from earlier discussions.. – TaW May 24 '14 at 18:36

1 Answers1

1

If its possible, you should make your application using WPF. WPF is designed to do exactly what you are trying to do.

However, it can be done in WinForms, though not easily. You will need to make a new class that inherits the TextBox WinForm control. Here is an example that makes a TextBox look like a circle:

public class MyTextBox : TextBox
{
    public MyTextBox() : base()
    {
        SetStyle(ControlStyles.UserPaint, true);
        Multiline = true;
        Width = 130;
        Height = 119;
    }

    public override sealed bool Multiline
    {
        get { return base.Multiline; }
        set { base.Multiline = value; }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        var buttonPath = new System.Drawing.Drawing2D.GraphicsPath();
        var newRectangle = ClientRectangle;

        newRectangle.Inflate(-10, -10);
        e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle);
        newRectangle.Inflate(1, 1);
        buttonPath.AddEllipse(newRectangle);
        Region = new System.Drawing.Region(buttonPath);

        base.OnPaintBackground(e);
    }      
}

Keep in mind that you will still have to do other things, such as clipping the text, etc. But this should get you started.

Icemanind
  • 47,519
  • 50
  • 171
  • 296