2

I am appending text to RichTextBox. When text is more than visible area of richtextbox then automatically Vertical ScrollBar appears.

I want to check if no scrollbar is present than set Padding to 5. If scrollbar appears then padding should be 0

private void frmAno_Load(object sender, EventArgs e)
    {
        DisplayingAnomalies();
        ChangeFormSize();
    }
private void DisplayingAnomalies()
    {
        int length;
        string heading;
        switch (_lstNullSheet.Count == 0 ? "zero" :
               _lstNullSheet.Count == 1 ? "one" :
               _lstNullSheet.Count > 1 ? "more" : "floor")
        {
            case "zero":
                break;
            case "one":
                heading = "Empty Sheet";
                rtbDisplay.Text = String.Format("{0}\r\n[", heading);
                rtbDisplay.AppendText(_lstNullSheet[0] + "] Sheet in Excel has no data.\r\n\n");
                break;
            case "more":
                heading = "Empty Sheets";
                rtbDisplay.Text = String.Format("{0}\r\n",heading);
               
                foreach(var item in _lstNullSheet)
                {
                    rtbDisplay.AppendText("["+item);
                    length = rtbDisplay.Text.Length;
                    if(_lstNullSheet.Last().Equals(item))
                    {
                        rtbDisplay.AppendText("] Sheets in Excel has no data.afsdfaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\n");
                        break;
                    }
                    rtbDisplay.AppendText("], ");
                    }
                break;
            case "floor":
                break;
            default:
                break;
        }
        _sizeToChange = true;
    }
private void ChangeFormSize()
    {
        if(_sizeToChange)
        {
           this.Height = 200;
        }
//Here i want to check if scrollbar is present in richtextbox or not 
       if(rtbDisplay.Width - rtbDisplay.ClientSize.Width >= 
        SystemInformation.VerticalScrollBarWidth)
        {
        }
    }

I have added the code for appending text to richtextbox. Then I am comparing the richtextbox width with scrollbar width.

10
  • You can simply check whether RichTextBox.Width - RichTextBox.ClientSize.Width >= SystemInformation.VerticalScrollBarWidth
    – Jimi
    Aug 24, 2020 at 22:58
  • BTW, what is the Padding you're referring to? The Left/Right margins maybe? Indentation? Or is it the Form's Padding? What happens when changing the margins also removes the ScrollBar?
    – Jimi
    Aug 24, 2020 at 23:08
  • @Jimi i have a panel in which richtextbox is displayed. richtextbox dock property is fill. Now i want to padding of panel when no scrollbar is present. Aug 26, 2020 at 16:51
  • @Jimi RichTextBox.Width - RichTextBox.ClientSize.Width >= SystemInformation.VerticalScrollBarWidth this statement is false everytime. bcz RichTextBox.Width - RichTextBox.ClientSize.Width is equal to zero Aug 26, 2020 at 16:54
  • RTB.Width - RTB.ClientSize.Width = 0 when RTB.BorderStyle = None, otherwise = 4. Until you add enough text to make the vertical scrollbar appear. After that, RTB.Width - RTB.ClientSize.Width = SystemInformation.VerticalScrollBarWidth
    – Jimi
    Aug 26, 2020 at 17:41

1 Answer 1

7

(Got this from an MSDN forum)

It's quite simple actually. You have to check for WS_VSCROLL style bit in the control's window style.

[System.Runtime.InteropServices.DllImport("user32.dll")]
private extern static int GetWindowLong(IntPtr hWnd, int index);

public static bool VerticalScrollBarVisible(Control ctl) {
  int style = GetWindowLong(ctl.Handle, -16);
  return (style & 0x200000) != 0;
}

Now, call the function as follows :

var IsScrollBarVisible = VerticalScrollBarVisible(myRichTextBox);
/// Put your logic here

Edit 1

Another approach might be this: Get the size of the RichTextBox before appending and after appending the text, just compare the ClientSize value of the textbox and you can determine whether the scrollbar is visible or not, based on the width.

EDIT 2

(This edit is inspired by the comments you'll see below)

Put the WS_SCROLL check inside the ClientSizeChanged event of the textbox, however, wrap it inside an if condition, as follows :

private void textbox_ClientSizeChanged(...)
{

 if (VerticalScrollBarVisible(myRichTextbox)
 { 

  //Put your logic here, what you want to do if scrollbar is visible
 }
}
20
  • Its giving IsScrollBarVisible false even the scroll bar is present . Aug 24, 2020 at 18:40
  • @MuhammadRizwan When are you calling it ? Aug 24, 2020 at 18:41
  • After appending all the text. It gives IsScrollbarVisible false. Aug 24, 2020 at 18:46
  • 1
    Add a button to your form. First, append the text to the textbox and get the cliensize. Then, when the UI is loaded, click the button to get the new clientsize of the textbox. If you see a difference, then simply remove the button and grab the clientsize property in the TextChanged event of the textbox. Aug 26, 2020 at 19:16
  • 1
    @Aousafrashid -- I would put 50 bucks on that guess.
    – Andy
    Aug 26, 2020 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.