Click here to Skip to main content
15,998,673 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How can I create a text box with drop down ability?

i need a single TextBox to expand when the user clicks inside it to begin typing

When I click or enter single text box, a multi line text box should appear under text box
like combo box for instance.

I am looking for something like combo box in C# and when I press button beside text box, a multi line text box should appear under that.
I know multi line text box and auto complete text box.

You can find what I need in Visual Studio in the properties of a text box in text field.
This attribute is useful for forms that do not have enough space.

You can type in multi line text box and after that multi line text box closes and you see first line of it in text box.

is there any component?
Posted
Updated 4-Oct-11 2:56am
v5
Comments
Madhav Hatwalne 26-Sep-11 16:01pm    
Hey please specify you want to work with forms or web?
headfon 2-Oct-11 16:05pm    
i want it in forms
BillWoodruff 26-Sep-11 21:54pm    
Are you saying you want a single TextBox to expand when the user clicks inside it to begin typing, or are you saying you want another TextBox to appear beneath the first one ?

Please clarify if you are using WinForms, ASP.NET, etc.
headfon 2-Oct-11 16:05pm    
yes the first one is exactly what i need
i need a single TextBox to expand when the user clicks inside it to begin typing
BobJanova 4-Oct-11 10:47am    
This is not the same as the initial problem. Resizing on entry/exit is easy (handle GotFocus/LostFocus events).

Hello,
here is my solution :

I have created simple form application with only textBox1 on it,
the important thing is that the text box can change its dimensions in pixels using : textBox1.Width and textBox.Height initially it is 300 x 200 pixels

Also size of the text box will be changed by mouse click to open and mouse double click to close

code for MainForm.cs:

C#
/*
 * Created by SharpDevelop.
 * User: Peric Zeljko
 * Date: 6.10.2011
 * Time: 14:10
 *
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Test_app
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        public MainForm()
        {
            //
            // The InitializeComponent() call is required for 
            //  Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Initially Set Height to only one line ,
            //        watch out for font size in text box it is set to 8
            //         Width is 300
            //
            textBox1.Multiline = false;
            textBox1.Width = 300;
            textBox1.Height = 20;
            textBox1.Refresh();
        }

        void TextBox1MouseClick(object sender, MouseEventArgs e)
        {
            // TODO: Implement TextBox1MouseClick
            // When you click with mouse on text box it opens
            // by changing its Height
            textBox1.Multiline = true;
            textBox1.Width = 300;
            textBox1.Height = 200;
            textBox1.Refresh();
        }

        void TextBox1MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // TODO: Implement TextBox1MouseDoubleClick
            // When you double click with mouse on text box it closes
            // by changing its Height
            textBox1.Multiline = false;
            textBox1.Multiline = true;
            textBox1.Width = 300;
            textBox1.Height = 20;
            textBox1.Refresh();
        }
    }
}



Right now it is possible to enter multiline text into only one line visible,
separating text lines with simply enter, and when you click to open all lines are there.Allso when you enter text lines into text box , and double click
the text box it closes, allso the first line is visible.

code for Program.cs:


XML
/*
 * Created by SharpDevelop.
 * User: Peric Zeljko
 * Date: 6.10.2011
 * Time: 14:10
 *
 */
using System;
using System.Windows.Forms;

namespace Test_app
{
    /// <summary>
    /// Class with program entry point.
    /// </summary>
    internal sealed class Program
    {
        /// <summary>
        /// Program entry point.
        /// </summary>
        [STAThread]
        private static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

    }
}



and code for MainForm.Designer.cs


XML
/*
 * Created by SharpDevelop.
 * User: Peric Zeljko
 * Date: 6.10.2011
 * Time: 14:10
 *
 */
namespace Test_app
{
    partial class MainForm
    {
        /// <summary>
        /// Designer variable used to keep track of non-visual components.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Disposes resources used by the form.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                if (components != null) {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// This method is required for Windows Forms designer support.
        /// Do not change the method contents inside the source code editor. The Forms designer might
        /// not be able to load this method if it was changed manually.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(3, 12);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(300, 200);
            this.textBox1.TabIndex = 2;
            this.textBox1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.TextBox1MouseDoubleClick);
            this.textBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.TextBox1MouseClick);
            //
            // MainForm
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(348, 244);
            this.Controls.Add(this.textBox1);
            this.Name = "MainForm";
            this.Text = "Test app";
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        public System.Windows.Forms.TextBox textBox1;

    }
}



I hope that this code would be enough to resolve your problem.

All the best,
Peric Zeljko
<removed>@yahoo.com
 
Share this answer
 
v3
Comments
headfon 8-Oct-11 7:49am    
thank you so much this is exactly what i need
I am not sure exactly what you are looking for but I am going to guess.
If you are looking for an auto complete like when you type in the google search text box you can use the following;

AJAX TOOL KIT[^]

If you just want the text box to extend to multiline you will have to set the property to multi rows and set the number of rows. You can do this with javascript or witht the code behind or both.

If this isn't what you are looking for update your post with more info.
 
Share this answer
 
Comments
Simon Bang Terkildsen 2-Oct-11 14:16pm    
OP wrote:

Thank you for your reply

but im looking for something like combo box in c# and when i press button beside
textbox, multiline textbox appear under that

i know multiline textbox and auto complete textbox

you can find what i need in visual studio in properties of textbox in text field

this attribute usefull for forms that doesnt have more space

you can type in multiline textbox and after that multiline textbox close and you see first line of it in textbox
headfon 2-Oct-11 15:59pm    
can u explain more please? im new in c#
do you have any example?

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