How to add scrollbars in c# form

35,280

Solution 1

You have to set both ScrollBars and WordWrap like this:

textBox1.ScrollBars = ScrollBars.Both;
textBox1.WordWrap = false;

NOTE: All the settings above are done 1 time. No need to place the code in the TextChanged event handler.

Solution 2

you don't need to write a code for this. Just change the properties of textBox. For both scroll bars, if Multiline set to True, then set ScrollBars to Both and set WordWrap to False in properties. No need for writing code at all since this is for WinForms.

Solution 3

private void textBox1_TextChanged(object sender, EventArgs e)
{
    textBox1.ScrollBars = ScrollBars.Both;     
}

ScrollBars.[Value] is an enum: Valid values are Horizontal, Vertical, None, and Both.

Share:
35,280
user2788405
Author by

user2788405

Updated on July 09, 2022

Comments

  • user2788405
    user2788405 almost 2 years

    For some reason only adding a vertical scroll bar works with my code.

    I can't seem to add BOTH a vertical and horizontal scroll bar.

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        textBox1.ScrollBars = ScrollBars.Vertical;     
    }
    
  • user2788405
    user2788405 over 10 years
    I tried the code you suggested but now I have no scroll bars. Strange ?
  • King King
    King King over 10 years
    @user2788405 have you checked out my solution? Looks like you even don't know where to put my code?
  • Mike G
    Mike G over 10 years
    @user2788405 try putting some text in there. You should also make sure that TextMode is set to MultiLine.
  • user2788405
    user2788405 over 10 years
    @KingKing appreciate your help. I did exactly what you told me too. Made a newtextbox, and copy pasted your code into my main. I imported a textfile that's too large horizontally and vertically in my textbox but I see no scroll bars. Thanks for the help
  • user2788405
    user2788405 over 10 years
    It's set to multiline
  • King King
    King King over 10 years
    @user2788405 where did you post my code? It should be somewhere like in your form constructor.
  • King King
    King King over 10 years
    @user2788405 in fact, you don't need to copy and paste the text into your textbox to see the scrollbars, the scrollbars should be visible with my code (although they look like disabled).
  • CrazedCoder
    CrazedCoder almost 9 years
    Weird, This still gives me just the Vertical Scrollbar, and continues to wrap strings to long to fit into one line.
  • Alex
    Alex almost 6 years
    Provide code to support your text. All I see is a lot of letters jumbled into one paragraph.