Create a Popup Input Box with C#

So I decided I wanted to keep the interface of my Overseer project pretty clean, but wanted to get simple input before creating certain objects. For example, a tab name when a user clicks the new tab button.

Here is a sample of what I’m talking about.

Here is the function I created to do this. You’ll need to add a using statement to your project.

using System.Drawing.Drawing2D;

public DialogResult PopupInput(Control ctrl, int border, int length, ref string output)

//handle alignment
Point ctrlpt = this.PointToScreen(ctrl.Location);
ctrlpt.Y += 24;
ctrlpt.X += 4;

TextBox input = new TextBox { Height=20, Width=length, Top= border/2,Left=border/2};
input.BorderStyle = BorderStyle.FixedSingle;
//######## SetColor to your preference
input.BackColor = Color.Azure;

Button btnok = new Button {DialogResult = System.Windows.Forms.DialogResult.OK, Top=25};
Button btncn = new Button { DialogResult = System.Windows.Forms.DialogResult.Cancel, Top = 25 };

Form frm = new Form { ControlBox = false, AcceptButton = btnok, CancelButton = btncn, StartPosition = FormStartPosition.Manual, Location = ctrlpt };
frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//######## SetColor to your preference
frm.BackColor = Color.Navy;

RectangleF rec = new RectangleF(0, 0, (length + border), (20 + border));
GraphicsPath GP = new GraphicsPath(); //GetRoundedRect(rec, 4.0F);
float diameter = 8.0F;
SizeF sizef = new SizeF(diameter,diameter);
RectangleF arc = new RectangleF(rec.Location, sizef);
GP.AddArc(arc, 180, 90);
arc.X = rec.Right - diameter;
GP.AddArc(arc, 270, 90);
arc.Y = rec.Bottom - diameter;
GP.AddArc(arc, 0, 90);
arc.X = rec.Left;

frm.Region = new Region(GP);
frm.Controls.AddRange(new Control[] {input,btncn,btnok});
DialogResult rst = frm.ShowDialog();
output = input.Text;
return rst;

If you’d like to make a sample app like the one above create a basic windows forms project, toss the above method in there with a button and a textbox on the form and add this code to the on click of the button.

string var="";

if (PopupInput(button1, 4, 75, ref var) == System.Windows.Forms.DialogResult.OK)
textBox1.Text = var;

NOTE: I’m not an awesome coder so any advice would be appreciated. I only post this because I wasn’t able to find something like it on the net.

EDIT: I forgot to add, as you can see by default the box aligns to the bottom of the passed in control. It would be easy to align it to another location, or to add a param that would let you pick. I wanted to keep the code as short as possible, but if anyone wants to see that, let me know.


About jrich

I am the Solutions Architect for Apex Learning in Seattle WA. I've been working with computers since I was 13. Started programming when I was 14. Had my first IT job as tech support at an ISP at the age of 15 and became a network admin at the age of 17. Since then I've worked at a variety of small to mid size companies supporting, maintaining and developing all aspects of IT. Mostly working with Windows based networks but have recently been working with Solaris system as well. I created this blog mostly as a place for me to take my own notes, but also share things that I had a hard time finding the info for.

Posted on August 31, 2011, in C# and tagged , . Bookmark the permalink. 2 Comments.

  1. Just what I needed. Easy to copy and paste. Just works. Thx :)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: