Search code examples
c#winformstext-size

Increase and decrease text size in a textbox using buttons


I have a form with a textbox and 2 buttons. I need the 2 buttons to increase and decrease the text size when clicked. Is there a way I can do this?


Solution

  • Assuming you are using winforms
    Create two buttons named: btnFontSizeUp and btnFontSizeDown use the following code on click events:

    btnFontSizeUp on click:

    float currentSize;
    
    currentSize = textboxName.Font.Size;
    currentSize += 2.0F;
    textboxName.Font = new Font(textboxName.Font.Name, currentSize, 
    textboxName.Font.Style, textboxName.Font.Unit);
    

    btnFontSizeDown on click:

    float currentSize;
    
    currentSize = textboxName.Font.Size;
    currentSize -= 2.0F;
    textboxName.Font = new Font(textboxName.Font.Name, currentSize, 
    textboxName.Font.Style, textboxName.Font.Unit);