Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-09-13
6
Medium Priority
?
1,032 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Accepted Solution

by:
kris_per earned 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 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