Muhammad Rizwan
Muhammad Rizwan

Reputation: 141

How to check a vertical ScrollBar is present in RichTextBox c#?

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.

Upvotes: 2

Views: 1493

Answers (1)

Aousaf Rashid
Aousaf Rashid

Reputation: 5738

(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
 }
}

Upvotes: 8

Related Questions