Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It may not be necessary to dive into "WndProc" either. This was done without it:</p> <p><img src="https://i.stack.imgur.com/YjX45.jpg" alt="This was done without &quot;WndProc&quot;"></p> <p>The Question really is how do you make a "nice looking" TextBox, because as described by j__m, you can just use ToolStripControlHost, to host a custom control in your tool strip. </p> <p>More here: <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripcontrolhost.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripcontrolhost.aspx</a></p> <p>And as documented, the control you use can be a Custom Control.</p> <p>Firstly, It's insanely tricky to make a custom TextBox Control. If you want to go:</p> <pre><code>public partial class TextBoxOwnerDraw : TextBox </code></pre> <p>You are in for HUGE trouble! But it doesn't have to be. Here is a little trick:</p> <p>If you make a custom control as a Panel, then add the TextBox to the Panel, then set the Textbox borders to None... you can achieve the result as above, and best of all, its just a regular old TextBox, so cut copy paste all works, right click works!</p> <p>Ok, here is the code for a nice looking textbox:</p> <pre><code>public partial class TextBoxOwnerDraw : Panel { private TextBox MyTextBox; private int cornerRadius = 1; private Color borderColor = Color.Black; private int borderSize = 1; private Size preferredSize = new Size(120, 25); // Use 25 for height, so it sits in the middle /// &lt;summary&gt; /// Access the textbox /// &lt;/summary&gt; public TextBox TextBox { get { return MyTextBox; } } public int CornerRadius { get { return cornerRadius; } set { cornerRadius = value; RestyleTextBox(); this.Invalidate(); } } public Color BorderColor { get { return borderColor; } set { borderColor = value; RestyleTextBox(); this.Invalidate(); } } public int BorderSize { get { return borderSize; } set { borderSize = value; RestyleTextBox(); this.Invalidate(); } } public Size PrefSize { get { return preferredSize; } set { preferredSize = value; RestyleTextBox(); this.Invalidate(); } } public TextBoxOwnerDraw() { MyTextBox = new TextBox(); this.Controls.Add(MyTextBox); RestyleTextBox(); } private void RestyleTextBox() { double TopPos = Math.Floor(((double)this.preferredSize.Height / 2) - ((double)MyTextBox.Height / 2)); MyTextBox.BackColor = Color.White; MyTextBox.BorderStyle = BorderStyle.None; MyTextBox.Multiline = false; MyTextBox.Top = (int)TopPos; MyTextBox.Left = this.BorderSize; MyTextBox.Width = preferredSize.Width - (this.BorderSize * 2); this.Height = MyTextBox.Height + (this.BorderSize * 2); // Will be ignored, but if you use elsewhere this.Width = preferredSize.Width; } protected override void OnPaint(PaintEventArgs e) { if (cornerRadius &gt; 0 &amp;&amp; borderSize &gt; 0) { Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle cRect = this.ClientRectangle; Rectangle safeRect = new Rectangle(cRect.X, cRect.Y, cRect.Width - this.BorderSize, cRect.Height - this.BorderSize); // Background color using (Brush bgBrush = new SolidBrush(MyTextBox.BackColor)) { DrawRoundRect(g, bgBrush, safeRect, (float)this.CornerRadius); } // Border using (Pen borderPen = new Pen(this.BorderColor, (float)this.BorderSize)) { DrawRoundRect(g, borderPen, safeRect, (float)this.CornerRadius); } } base.OnPaint(e); } #region Private Methods private GraphicsPath getRoundRect(int x, int y, int width, int height, float radius) { GraphicsPath gp = new GraphicsPath(); gp.AddLine(x + radius, y, x + width - (radius * 2), y); // Line gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90); // Corner (Top Right) gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2)); // Line gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Corner (Bottom Right) gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height); // Line gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90); // Corner (Bottom Left) gp.AddLine(x, y + height - (radius * 2), x, y + radius); // Line gp.AddArc(x, y, radius * 2, radius * 2, 180, 90); // Corner (Top Left) gp.CloseFigure(); return gp; } private void DrawRoundRect(Graphics g, Pen p, Rectangle rect, float radius) { GraphicsPath gp = getRoundRect(rect.X, rect.Y, rect.Width, rect.Height, radius); g.DrawPath(p, gp); gp.Dispose(); } private void DrawRoundRect(Graphics g, Pen p, int x, int y, int width, int height, float radius) { GraphicsPath gp = getRoundRect(x, y, width, height, radius); g.DrawPath(p, gp); gp.Dispose(); } private void DrawRoundRect(Graphics g, Brush b, int x, int y, int width, int height, float radius) { GraphicsPath gp = getRoundRect(x, y, width, height, radius); g.FillPath(b, gp); gp.Dispose(); } private void DrawRoundRect(Graphics g, Brush b, Rectangle rect, float radius) { GraphicsPath gp = getRoundRect(rect.X, rect.Y, rect.Width, rect.Height, radius); g.FillPath(b, gp); gp.Dispose(); } #endregion } </code></pre> <p>Now for the ToolStripControlHost</p> <pre><code>public partial class ToolStripTextBoxOwnerDraw : ToolStripControlHost { private TextBoxOwnerDraw InnerTextBox { get { return Control as TextBoxOwnerDraw; } } public ToolStripTextBoxOwnerDraw() : base(new TextBoxOwnerDraw()) { } public TextBox ToolStripTextBox { get { return InnerTextBox.TextBox; } } public int CornerRadius { get { return InnerTextBox.CornerRadius; } set { InnerTextBox.CornerRadius = value; InnerTextBox.Invalidate(); } } public Color BorderColor { get { return InnerTextBox.BorderColor; } set { InnerTextBox.BorderColor = value; InnerTextBox.Invalidate(); } } public int BorderSize { get { return InnerTextBox.BorderSize; } set { InnerTextBox.BorderSize = value; InnerTextBox.Invalidate(); } } public override Size GetPreferredSize(Size constrainingSize) { return InnerTextBox.PrefSize; } } </code></pre> <p>Then When you want to use it, just add it to the tool bar:</p> <pre><code>ToolStripTextBoxOwnerDraw tBox = new ToolStripTextBoxOwnerDraw(); this.toolStripMain.Items.Add(tBox); </code></pre> <p>or however you want to add it. If you are in Visual Studio, the preview window supports rendering this Control.</p> <p>There is only one thing to remember, when accessing the TextBox with the actual text in it, its: </p> <pre><code>tBox.ToolStripTextBox.Text; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload