Solved

C# Windows app. Increase the height of a multiline textbox to show all text entered

Posted on 2010-09-13
6
1,016 Views
Last Modified: 2013-12-17
Hi

I am new to C# so apologies if this is a silly question.

I have a windows form with a series of textboxes for data input.  A few of them are multiline.  I need to expand the height of the multiline text box to show all the data that has been input without a scrollbar.

I will also need to move all the controls below the adjusted text box down the form to a new position to avoid overlap.

Any sample code or help would be great!

Thanks in advance!
0
Comment
Question by:greep
6 Comments
 
LVL 3

Expert Comment

by:ghayasurrehman
ID: 33660776
set textbox property textBox1.Multiline = True

just drag and drop your controls where you want
0
 
LVL 3

Expert Comment

by:ghayasurrehman
ID: 33660784
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33661037

One option is to use 'TableLayoutPanel' in the designer which will automatically push the controls down.
Add a TableLayoutPanel to the form in the designer.
Arrange the textboxes and other controls in the rows of that table.
when you increase the height of a multiline textbox, increase the height of the table row as well (like tableLayoutPanel1.RowStyles[i].Height += 30). This will automatically push all the controls in the rows below down.

Another option is to programatically change the Location of the controls as shown in the code below.

public void MoveControlsBelow(Control control, int moveHeight)

        {

            foreach (Control c in this.Controls)

            {

                if (c != control)

                {

                    if (c.Location.Y >= control.Location.Y)

                    {

                        c.Location = new Point(c.Location.X, c.Location.Y + moveHeight);

                    }

                }

            }

        }

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 33661117

I am not sure if you are alreading changing the height of the multiline textbox to fit all the lines of the text; If not, try with the code below:


// Below code will keep increasing the height of the textbox as

// required as user types in the textbox and will push other 

// controls down.



private void textBox1_TextChanged(object sender, EventArgs e)

        {

            TextBox textBox = sender as TextBox;

            if (textBox != null)

            {

                int prevHeight = textBox.Height;

                int newHeight = (int)( (textBox.Lines.Length+1) * (textBox.Font.GetHeight() + 3));

                if (newHeight > prevHeight)

                {

                    textBox.Height = newHeight;



                    int heightChange = textBox.Height - prevHeight;

                    MoveControlsBelow(textBox, heightChange);

                }

            }

        }







public void MoveControlsBelow(Control control, int moveHeight)

        {

            foreach (Control c in this.Controls)

            {

                if (c != control)

                {

                    if (c.Location.Y >= control.Location.Y)

                    {

                        c.Location = new Point(c.Location.X, c.Location.Y + moveHeight);

                    }

                }

            }

        }

Open in new window

0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33665192
I agree with Kris per's solution. That is the best answer.

Regards,
VSS
0
 

Author Closing Comment

by:greep
ID: 33669971
Thanks!  Much appreciated!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 24
VB.net/WinForms: adjust Screen Resolution 6 42
Visual Studio 2015 Source Code Control 10 37
Reset asp.net login password 4 34
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now