如何检查 RichTextBox c# 中是否存在垂直滚动条?
How to check a vertical ScrollBar is present in RichTextBox c#?
我正在将文本附加到 RichTextBox。当文本超过 richtextbox 的可见区域时,会自动出现 Vertical ScrollBar。
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)
{
}
}
我已经添加了将文本附加到 richtextbox 的代码。然后我将 richtextbox 宽度与滚动条宽度进行比较。
(从 MSDN 论坛获取)
其实很简单。您必须检查控件 window 样式中的 WS_VSCROLL
样式位。
[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;
}
现在,调用函数如下:
var IsScrollBarVisible = VerticalScrollBarVisible(myRichTextBox);
/// Put your logic here
编辑 1
另一种方法可能是这样的:在附加文本之前和之后获取 RichTextBox
的大小,只需比较文本框的 ClientSize
值,就可以判断滚动条是否可见与否,根据宽度。
编辑 2
(此编辑的灵感来自您将在下面看到的评论)
将 WS_SCROLL
检查放在文本框的 ClientSizeChanged
事件中,但是,将其包装在 if
条件中,如下所示:
private void textbox_ClientSizeChanged(...)
{
if (VerticalScrollBarVisible(myRichTextbox)
{
//Put your logic here, what you want to do if scrollbar is visible
}
}
我正在将文本附加到 RichTextBox。当文本超过 richtextbox 的可见区域时,会自动出现 Vertical ScrollBar。
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)
{
}
}
我已经添加了将文本附加到 richtextbox 的代码。然后我将 richtextbox 宽度与滚动条宽度进行比较。
(从 MSDN 论坛获取)
其实很简单。您必须检查控件 window 样式中的 WS_VSCROLL
样式位。
[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;
}
现在,调用函数如下:
var IsScrollBarVisible = VerticalScrollBarVisible(myRichTextBox);
/// Put your logic here
编辑 1
另一种方法可能是这样的:在附加文本之前和之后获取 RichTextBox
的大小,只需比较文本框的 ClientSize
值,就可以判断滚动条是否可见与否,根据宽度。
编辑 2
(此编辑的灵感来自您将在下面看到的评论)
将 WS_SCROLL
检查放在文本框的 ClientSizeChanged
事件中,但是,将其包装在 if
条件中,如下所示:
private void textbox_ClientSizeChanged(...)
{
if (VerticalScrollBarVisible(myRichTextbox)
{
//Put your logic here, what you want to do if scrollbar is visible
}
}